Overview

9 Trigonometry for web developers

Trigonometry is presented as a practical frontend tool rather than abstract math, with sine, cosine, and tangent used to describe angles and positions on a circle. The chapter emphasizes the unit circle, the meaning of radians versus degrees, and how to convert between them so web developers can work comfortably across CSS and JavaScript. It also introduces inverse trig functions, especially atan2, as a reliable way to recover angles from coordinates and determine direction in all quadrants.

Building on those foundations, the chapter shows how trig can drive circular layouts and motion. Menu items can be arranged around a circle by calculating x and y coordinates from a center point, radius, and evenly spaced angles, either in JavaScript or with CSS calculations. The same formulas can animate elements around circular paths, create spinning loaders, and produce pendulum-like motion, with trig-controlled values like angle, radius, and speed shaping the look and timing of the effect.

The final part extends trig into wave-based design and direction-finding interactions. By varying amplitude, frequency, time, and phase shift, developers can place letters along a wave or animate text so it ripples dynamically. The chapter closes with directional calculations, where atan2 is used to compute the angle between two points so an arrow, pointer, or other element can rotate toward the cursor or a target, demonstrating how trig supports both decorative motion and responsive, orientation-aware UI behavior.

A right triangle and its sides relative to the angle theta (θ)
The graph of sin(θ) for −2π ≤ θ ≤ 2π
The graph of tan(θ) for −2π ≤ θ ≤ 2π
 Arranging menu items in a circle using JavaScript
Arranging letters along a wave path using sin
 Using Math.atan2() to get the arrow to follow the cursor

Summary

  • Trigonometry deals with the relationships that exist within a triangle, particularly a right-angled triangle, where one of the angles is 90 degrees.
  • The sine is the opposite over the hypotenuse, so it’s a measure of the vertical component of the angle.
  • The cosine is the adjacent over the hypotenuse, so it’s a measure of the horizontal component of the angle.
  • The tangent is the opposite over the adjacent, so it’s a measure of the steepness of the angle.
  • The values of cos(θ) and sin(θ) oscillate smoothly within the range [−1, 1], so that when you graph sin(θ) or cos(θ), the result is a regular, repeating wave pattern.
  • To convert from degrees to radians, use degrees * (Math.PI / 180). To convert from radians to degrees, use radians * (180 / Math.PI).
  • There are seven main trig functions—sin(), cos(), tan(), asin(), acos(), atan(), and atan2()—available in both CSS and JavaScript. The JavaScript versions are methods of the Math object (Math.sin(), Math.cos(), etc.).
  • You can use sin and cos to arrange page elements along a circle. For a circle with radius r centered at the point (centerX, centerY), for any angle, you calculate the x- and y-coordinates of that point on the circle using x = centerX + r * cos(angle) and y = centerY + r * sin(angle).
  • The trig functions are useful for arranging page elements along a curved path. Multiply cos or sin to modify the amplitude of the curve, which is the maximum vertical offset from the horizontal axis. Multiply the angle to modify the frequency, which is a measure of how fast the wave cycles.
  • When working with directional calculations, use atan2 to ensure the angle is correct in all quadrants.

FAQ

What are sine, cosine, and tangent used for in web development?Sine, cosine, and tangent help you calculate angles, positions, slopes, and motion effects in frontend work. They’re commonly used for circular layouts, wave-like text effects, direction calculations, and smooth animations.
How do sine and cosine relate to a point on the unit circle?On the unit circle, a point at angle θ has coordinates x = cos(θ) and y = sin(θ). This makes cosine the horizontal component and sine the vertical component of the point.
Why is tangent considered the “steepness” of an angle?Tangent is defined as tan(θ) = opposite / adjacent = sin(θ) / cos(θ). Because it compares vertical change to horizontal change, it represents the slope or steepness of the angle.
Why is tan(90°) or tan(270°) undefined?Tangent is sin(θ) / cos(θ), and at 90° and 270° the cosine value becomes 0. Division by zero is undefined, so tangent is undefined at those angles.
What is the difference between degrees and radians?Degrees measure angles in 360 parts per full circle, while radians measure angles based on the circle’s radius. A full circle is 360deg or 2π radians, and JavaScript trig functions use radians.
How do you convert degrees to radians in JavaScript?Use radians = degrees * (Math.PI / 180). For example, 45 degrees becomes Math.PI / 4 radians.
What does Math.atan2(y, x) do?Math.atan2(y, x) returns the angle of the point (x, y) relative to the positive x-axis. It correctly handles all four quadrants, unlike a plain atan calculation.
How can trig be used to place elements around a circle?Use x = centerX + r * cos(angle) and y = centerY + r * sin(angle). These formulas let you position menu items, spinners, or other elements evenly along a circular path.
How do you create a wave effect with text using sine?Split the text into individual letters, then offset each letter vertically with something like offset = a * sin(f * θ) or a * sin(ft + φ). The amplitude controls wave height, and frequency controls how tightly the wave repeats.
Why does the cursor-facing arrow sometimes show a negative angle?Because browser coordinates start at the top-left, with y increasing downward. If the cursor is above and left of the element, both deltas are negative, so the computed angle can be negative.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Math for Web Design ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Math for Web Design ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Math for Web Design ebook for free