# Hit Testing in Canvas UI: Making Animated Graphics Clickable and Accessible

> A canvas is one bitmap, so the browser cannot tell which drawn shape you clicked. Hit testing is how your code or animation runtime works that out, by checking the pointer position against shapes or invisible hit areas. Use pointer events for mouse, touch and pen together, make hit areas at least as large as accessibility guidelines suggest, and pair canvas graphics with real HTML controls for keyboards and screen readers.

Updated November 2026. Source: https://www.exodeui.com/learn/canvas-hit-testing/

## Key takeaways

- Canvas needs hit testing because it is one bitmap
- Use pointer events and convert to canvas coordinates
- Make hit areas at least 24 by 24 CSS pixels, larger for touch
- Pair canvas graphics with real HTML controls

## Why canvas needs hit testing

With HTML, every button is an element that receives its own events. On a canvas, everything is pixels, so you get one event for the whole canvas with an x and y position. The Canvas API offers isPointInPath and isPointInStroke to test a point against a path. Animation runtimes do the same internally for their listeners and state machine inputs.

## Pointer events for every input

Pointer events unify mouse, touch and pen. Listen for pointerdown, pointermove and pointerup on the canvas, convert the position into the canvas's coordinate space (accounting for CSS size, device pixel ratio and any scaling of the artwork), and then test it.

- Use pointer capture so a drag keeps working when the pointer leaves the shape
- Set touch-action in CSS so the browser does not scroll during drags
- Handle pointercancel, which fires when the system takes over a gesture

## Hit areas larger than the art

A tiny drawn icon is hard to tap. WCAG 2.2 sets a minimum target size of 24 by 24 CSS pixels with some exceptions, and platform guidelines recommend larger targets on touch screens. Use invisible hit shapes that are bigger than the visible artwork, especially for small controls.

## Keep it accessible

Screen readers and keyboards cannot see shapes inside a canvas. Put real buttons, inputs and labels in the HTML, either visibly next to the canvas or positioned over it, and let them drive the animation's inputs. The canvas then shows the motion while the HTML carries meaning and focus.

## Building this in ExodeUI

ExodeUI, which we make, handles pointer input on its canvas for you, and its components expose inputs and events. The pattern we recommend: a real HTML button or input that sets the component's input, so keyboard and screen reader users get the same result as people who click the graphic.

## Frequently asked questions

### How do I detect clicks on a shape in a canvas?

Convert the pointer position to canvas coordinates and test it with isPointInPath or your own geometry checks, or let your animation runtime handle listeners.

### Why are my canvas clicks offset?

Usually the canvas is scaled by CSS or device pixel ratio. Convert pointer coordinates using the canvas bounding rectangle and its internal size.

### Are canvas animations accessible?

Not by themselves. Add real HTML controls and labels that drive the animation.

### Mouse events or pointer events?

Pointer events. They cover mouse, touch and pen with one API.

## Related

- [Sharp canvas rendering and devicePixelRatio](https://www.exodeui.com/learn/canvas-devicepixelratio-sharp-rendering/)
- [Canvas vs DOM animation](https://www.exodeui.com/learn/canvas-vs-dom-animation/)
- [Accessible focus states](https://www.exodeui.com/learn/accessible-focus-states/)
- [Physical drag interaction](https://www.exodeui.com/learn/physical-drag-interaction/)
