9 Trigonometry for web developers
Trigonometry turns out to be a practical tool for web developers rather than just an abstract math topic. The chapter introduces the core ideas behind sine, cosine, and tangent using the right triangle and the unit circle, showing how these functions describe positions, slopes, and repeating wave patterns. It also explains the angle units used in web work, especially degrees and radians, and shows how to convert between them so CSS and JavaScript trig functions can be used correctly together.
With those basics in place, the chapter demonstrates how trig powers circular layouts and motion. Using cosine and sine, developers can place items evenly around a circle, build circular menus, and animate elements along orbit-like paths. The same ideas can be applied in CSS or JavaScript, depending on whether more control or simpler implementation is needed, and they also extend to spinners and pendulum-like motion by updating angles over time.
The chapter then shifts to wave and direction-based effects. It shows how sine waves can position letters or other elements in a wave pattern, and how amplitude, frequency, and phase shift make those waves dynamic and visually interesting. Finally, it explains how atan2 helps compute the direction from one point to another, which is useful for rotating arrows, pointers, or other interface elements toward targets such as the mouse cursor, with special emphasis on why atan2 is more reliable than simpler inverse tangent calculations across all quadrants.
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(), and so on).
- 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 design?
Sine, cosine, and tangent are used to calculate angles, positions, slopes, and motion paths. In web design, they help create circular layouts, wave effects, directional rotations, and smooth animations.
How do sine and cosine relate to a point on the unit circle?
For a point on the unit circle at angle θ, the x-coordinate is cos(θ) and the y-coordinate is sin(θ). This is the foundation for placing elements around a circle.
Why does tangent become undefined at 90° and 270°?
Tangent is defined as sin(θ) / cos(θ). At 90° and 270°, cosine equals 0, so the ratio is undefined and the tangent graph has asymptotes there.
How do you convert degrees to radians in JavaScript?
Use the formula radians = degrees * (Math.PI / 180). JavaScript trig functions like Math.sin() and Math.cos() expect radians, not degrees.
What is Math.atan2() used for?
Math.atan2(y, x) returns the angle between the positive x-axis and the point (x, y). It is especially useful for directional calculations because it works correctly in all four quadrants.
How can trig be used to create a circular menu?
You can place each menu item using x = centerX + r * cos(angle) and y = centerY + r * sin(angle). By stepping the angle evenly for each item, the menu elements are arranged in a circle.
How do you animate an element moving in a circle?
Update the angle over time, then recalculate the element’s x and y position using cosine and sine. Repeating this with requestAnimationFrame() creates a smooth circular orbit.
How can sine create wave effects in text?
Sine can be used to offset each letter vertically with offset = a * sin(f * θ). By adjusting amplitude and frequency, you can create static or animated wave text effects.
Why is cosine often used for pendulum motion?
A pendulum’s angle over time follows a cosine-like oscillation. Using a cosine equation lets you simulate a back-and-forth swinging motion for elements like signs or clocks.
Why might an angle returned by atan2() be negative in the browser?
Browser coordinates start at the top-left, with y increasing downward. Because of that coordinate system, points above and to the left can produce negative angle values from atan2().
Math for Web Design ebook for free