liveBook · Manning

Math.atan2

As explained in Math for Web Design

Math.atan2 is a JavaScript function that returns the angle in radians corresponding to the direction from the origin to a point while preserving the signs of both coordinates.

Math.atan2(y, x) turns a direction into an angle. Give it the vertical and horizontal distances from a starting point to a target, and it returns that direction in radians relative to the positive x-axis. It is the useful choice for a pointer, arrow, dial, or canvas object that must face a changing position. section 9.1.4 section 9.4

Start by subtracting coordinates: deltaX = x2 − x1 and deltaY = y2 − y1. Then call Math.atan2(deltaY, deltaX). The argument order matters: y comes first, x second. Convert the result for CSS rotation with radians * (180 / Math.PI), then use rotate(angleDeg). section 9.4 section 9.6

function getAngle(x1, y1, x2, y2) {
    const deltaY = y2 - y1;
    const deltaX = x2 - x1;
    const angleRadians = Math.atan2(deltaY, deltaX);
    const angleDegrees = angleRadians * (180 / Math.PI);

    return angleDegrees;
}

This helper derives an element-facing angle from a source point and a target point, returning degrees ready for a CSS rotate() transform. section 9.4

Unlike Math.atan(y / x), atan2 keeps both input signs. That preserves the correct quadrant: a target down and left of the source has deltas of −1 and −1, producing −135° rather than the misleading 45° from a ratio alone. Its returned range spans −180° to +180°. section 9.1.4 section 9.4

Use Math.atan2 when an interaction needs direction from a center or source point:

  • Rotate an arrow, eye, camera, sprite, or dragged element toward the cursor or another element. section 9.4
  • Drive a rotary control by converting pointer coordinates relative to the knob center into an angle, then into a constrained value. section 13.2.2
  • Add geometric hover tilt by combining its angle with distance-based intensity and sin/cos rotations. section 10.2.3
  • Identify a donut-chart slice by pairing the pointer angle with a distance check against the inner and outer radii. section 14.1.3

Point an element at the cursor

Find the element’s center with getBoundingClientRect(), subtract that center from event.clientX and event.clientY, and pass the local deltas to Math.atan2. Convert radians to degrees before assigning transform: rotate(...). If a dial’s visual zero should point upward rather than rightward, adjust the computed angle by 90 degrees. section 9.4 section 13.4

Browser coordinates increase downward on the y-axis, so web directions appear clockwise: right is 0°, down is 90°, and down-right is 45°. Math.atan2 provides the vector direction; distance remains a separate calculation, commonly made with the Pythagorean theorem. section 10.1 section 13.5

How Math for Web Design covers it

How deep it goes
Explains coordinate deltas, quadrant preservation, radian conversion, and CSS rotation through worked UI implementations.
Examples
Uses a cursor-following arrow, geometric hover effect, custom rotary knob, and interactive donut chart.
What it assumes
Introduces inverse trigonometry from ratios and uses basic coordinates, vectors, and CSS transforms.
What you can do after
Apply it via JavaScript Math.atan2() to point UI elements and classify pointer directions around a center.
How it connects
Connects vectors, coordinate deltas, radians, CSS rotate(), the Pythagorean theorem, and polar coordinates.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math for Web Design. The section links point to the parts of the book it draws on.