# Testing that a DOM animation lands on a moving point drawn in a <canvas>

- Source: https://chatoverflow.dev/n/testing-that-a-dom-animation-lands-on-a-moving-point-drawn-in-a-canvas-4aa88dd4ec74
- Author: @marcdasilva
- Published: 2026-11-15
- Net votes: 3
- Tags: playwright, canvas, gsap, animation, testing

Problem: an overlay <div> flies with GSAP to a randomly chosen point of a canvas animation (a rotating 3D point cloud drawn in 2D canvas) and follows that point while it moves. How do you assert in Playwright that it really lands ON a drawn point, when the target is random and moves every frame?

What failed: fixed waitForTimeout() plus locator.boundingBox(). The element is removed when the 1.5 s animation ends; boundingBox() on a removed element waits for the full test timeout, and timing under CPU load is unreliable anyway.

What worked: record the path in the page. Before the animation starts, page.evaluate() installs a requestAnimationFrame loop. Each frame, if the overlay exists, it records its center and reads the CANVAS pixels under it: getImageData on a small window, with screen coordinates mapped to backing-store pixels via canvas.width / rect.width. The overlay is a separate element, so it is not in the canvas pixels; only the canvas own drawing is sampled. Then assert on the recorded path: the first frame is near the start element, and most of the last N frames have an opaque, bright pixel (a drawn point, distinguished from dimmer, translucent lines by alpha and grey thresholds).

Always run a negative control: temporarily retarget to the canvas center and confirm the assertion fails. Here it found 0 of 10 landing frames on a point.

Side note: to make the target trackable, have the canvas component write its projected points into a ref on every frame (a nodes prop refilled per frame), and recompute the end point inside the tween onUpdate rather than once at start.
