liveBook · Manning

Hit testing

As explained in Math for Web Design

Hit testing is the technique of determining whether a point or pointer location lies within a graphical object's boundaries.

Hit testing determines whether a pointer location lies inside a shape drawn on a canvas. It turns coordinate math into hover interactions, tooltips, and data-rich visual feedback. section 14.1 section 14.4

Choose the boundary calculation that matches the shape:

  • Test a rectangle by checking whether the pointer’s x-coordinate lies between its left and right edges and its y-coordinate lies between its top and bottom edges. section 14.1.2
  • Test a donut slice by checking radial distance first, then comparing the pointer angle with the slice’s start and end angles. section 14.1.3

Interactive bar chart

Store each bar’s x, y, width, height, and data while drawing it. On pointer movement, compare the canvas-relative pointer coordinates with those boundaries; when a bar matches, display its label and value in a tooltip.

How Math for Web Design covers it

How deep it goes
Explains coordinate conversion, rectangular bounds, radial distance, angles, and trigonometric calculations without extending into broader event-system design.
Examples
Uses an interactive visitor bar chart and a seawater-composition donut chart with hover feedback.
What it assumes
Assumes basic JavaScript, canvas drawing, pointer events, coordinates, and elementary geometry while introducing the needed hit-detection math.
What you can do after
Apply it via JavaScript and canvas to detect pointer interaction with rectangular and arc-based visualizations.
How it connects
Connects canvas, coordinate systems, geometric shapes, bar charts, donut charts, polar coordinates, trigonometry, and the Pythagorean theorem.

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.