DEV Community

Cover image for DIV Points at Other DIV
Zevan Rosser
Zevan Rosser

Posted on • Edited on

DIV Points at Other DIV

Moving your mouse around the page, you’ll notice the red div always points at the black div.

I remember learning this from nooflat.nu by Jamie Macdonald back in 2001-2. I spun up my custom made experimental browser to take a look… also downloaded the source…

Excuse the music I was too lazy to open iMovie and add something decent… Here is the key part of the source where I learned about atan2 for the first time 😀

//SOURCE FOR EDUCATIONAL PURPOSES, ETC.
fscommand("allowscale","false")


// FUNCTION TO CALCULATE ANGLE FROM ONE OBJECT TO ANOTHER
function calcangle (me,targetclip) {

    // FIRST COMPUTE THE DISTANCES FROM THE MOVIECLIP THE FUNCTION
    // IS CALLED FROM  TO THE TARGET CLIP:
    var deltax = me._x-targetclip._x;
    var deltay = me._y-targetclip._y;

    // NEXT USE THESE DISTANCES TO CALCULATE THE ANGLE BETWEEN THEM:
    angle = Math.atan2(deltay, deltax);

    // FINALLY CONVERT THE ANGLE FROM RADIANS TO DEGREES AND THEN RETURN THE RESULT:
    angle /= (Math.pi/180);
    return angle;
}
Enter fullscreen mode Exit fullscreen mode

Brings back great memories…

See more stuff like this over @ Snippet Zone

Top comments (0)