Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 12.3 — Hit-Testing: Clicking Things on a Canvas
Data Visualization · Example 2 of 3

Colour picking with a hidden canvas

The dashed canvas is the hidden one, shown so you can see it: its colours are ids 1 to 4, so it looks black. The alpha check in pick is explained in Break it.

A canvas draws pixels and keeps no shapes, so it cannot tell you what was clicked. You find the mark under the pointer yourself, with the browser's shape test, a hidden colour canvas, a quadtree or a Delaunay diagram.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 4 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write stationAtPoint(stations, x, y, reach) using a quadtree built inside it, returning the station or null.

02

In the second example, make the hover text say edge when the pixel is partly transparent, nothing when empty, and the name only when alpha is 255.

Explain it without notes

01

Why can you not add a click handler to one dot on a canvas?

02

Compare isPointInPath and colour picking: when would you choose each?

Colour picking with a hidden canvas

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

on Quay only: Quay
where Quay and Market overlap: Market
on Chapel: Chapel
empty space: null
outside the canvas: null