Overview

13 Interactivity and gesture-based UI math

Modern interactive web experiences are built by translating user input into visual changes with math. The chapter explains that mouse, touch, and scroll actions all produce numerical data that can be measured, compared, and converted into motion, size, color, or position. It opens with the idea that interactivity is not magical at all: it is just event data plus calculations that respond to what the user is doing.

The first major focus is pointer and touch input. It shows how to read cursor coordinates from mouse events, how different coordinate systems serve different purposes, and how to convert between viewport-based and page-based positions using scroll offsets. It also covers touch gestures by examining touch points on touch-enabled devices, using distance calculations to detect pinches and spreads, and then applying those measurements to interactive features such as zooming a photo.

The chapter then moves into controls and motion driven by geometry. It demonstrates how to build custom linear sliders by mapping pointer position to a numeric range, clamping values to boundaries, and updating handles and labels in real time. It also shows how rotary dials use trigonometry, especially angle calculations from Math.atan2(), to turn pointer movement into rotation and value changes. Finally, it explains scroll-based animations by normalizing scroll position into progress values, using that progress to drive widths, colors, and text, and introducing IntersectionObserver as a more efficient way to animate elements based on visibility without constantly handling every scroll event.

 A tooltip that follows the user’s pointer
 An interactive color picker that sets the page background color based on where the user clicks within the element
 Custom linear sliders for setting brightness, saturation, and contrast values
 A custom rotary control for setting volume values
A reading progress indicator
Dynamic UI changes based on an intersection observer

Summary

  • The event.clientX and event.clientY properties provide a pointer’s coordinates relative to the viewport’s top-left corner.
  • You can convert viewport coordinates to an element’s local coordinates by subtracting the left and top values from element.getBoundingClientRect().
  • The distance between two points can be found using the Pythagorean theorem, and the direction can be found using Math.atan2().
  • For a slider control, you map the current drag progress to the slider value using the formula minValue + progress * (maxValue - minValue).
  • The user’s scroll progress can be calculated as a normalized value from 0 to 1 using the formula scrollY / (scrollHeight - innerHeight).
  • The IntersectionObserver API is an efficient way to trigger actions when an element enters or leaves the viewport.
  • The intersectionRatio property from an IntersectionObserver tells you what percentage of a target element is currently visible.

FAQ

What coordinate system do mouse events use with clientX and clientY?clientX and clientY give the pointer position relative to the viewport, with (0, 0) at the top-left of the visible browser area.
How do you convert client coordinates to page coordinates?Add the current scroll offsets: pageX = clientX + window.scrollX and pageY = clientY + window.scrollY.
Why would you subtract getBoundingClientRect().left and .top from client coordinates?Because getBoundingClientRect() returns an element’s position relative to the viewport, so subtracting left and top gives coordinates relative to that element.
What is the main idea behind detecting pinch and spread gestures on touch devices?Track the distance between two touch points over time. If the distance decreases, it’s a pinch; if it increases, it’s a spread.
Which math function is used to measure the distance between two touch points?The Pythagorean theorem, implemented with Math.sqrt(dx * dx + dy * dy).
How do custom linear sliders map pointer position to a value range?Measure the pointer’s offset within the slider track, clamp it to the track bounds, convert it to a 0-to-1 progress ratio, then map that ratio to the desired min-to-max value range.
Why does a rotary knob use Math.atan2(y, x)?Math.atan2() converts the mouse position relative to the knob’s center into an angle, which can then be turned into a control value.
Why is the angle in the rotary control adjusted by adding 90 degrees?Because Math.atan2() returns 0 degrees pointing to the right, but a knob usually wants 0 degrees to point straight up.
How do you calculate scroll progress for a reading progress bar?Use scrollProgress = window.scrollY / (document.documentElement.scrollHeight - window.innerHeight).
What does intersectionRatio mean in an IntersectionObserver callback?It is the fraction of the target element that is visible, ranging from 0.0 for not visible to 1.0 for fully visible.

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