13 Interactivity and gesture-based UI math
This chapter covers
- Handling mouse and touch events
- Crafting custom slider controls
- Building scroll-based animations
The static web is dead. Modern surfers now expect web experiences that are defined by interactivity. They want elements that react to their cursor, interfaces they can drag and swipe, and animations that start or change as they scroll. Adding this rich layer of user interaction to your pages adds a touch of magic to your site, but there's nothing magical about the implementation. It's made possible by capturing user input, whether it's a click, a touch, or a scroll, and using math to translate that input into a visual change.
13.1 Mouse and touch event calculations
Every time a user moves their mouse, drags a finger along a touchpad, or touches their screen, the browser generates events packed to the brim with numerical data. The most fundamental pieces of data are the coordinates: the x and y positions of the pointer or touch point. Understanding how to capture and use these coordinates is the first step toward building any interactive feature.