DOM pinball made from live form controls
After building the four element games on /play/, I wanted to push live controls inside a 2D physics loop. Older DOM physics toys like Google Gravity and Matter.js DOM experiments sync CSS transforms onto elements and fight stacking order when adding trails or shadows. Here every bumper, tiltable ramp, diverter gate, bonus trap, scoreboard and flipper is a real HTML control inside one drawable canvas, so ball impacts mutate the DOM and your taps alter the physics mid-flight.
Tap controls on the table directly, or use Z / ArrowLeft and / / ArrowRight for the flippers and Space to launch.
Show the code controls, rotated drawing, physics collisions and fallback
- Ten live HTML controls inside one drawable canvas.
- Draw shadows, trails and rotated elements with five-argument
drawElementImage(). - Sub-stepped ball physics that mutate bumper classes,
<details open>and ramp slopes. - Context loss and restoration with a plain HTML fallback.
- Arcade control styles and focus rings.
How it works
Every obstacle on the playfield is a real child element of <canvas id="pinball-canvas" content="drawable" layoutsubtree>.
One 2D canvas paints the table background, rail shadows, velocity trails, shockwave rings and the ten controls in a single pass.
On each frame, the paint handler resets the transform, scales to the backing store via a device-pixel-content-box ResizeObserver, and calls five-argument drawElementImage(el, x, y, el.offsetWidth, el.offsetHeight) so high-DPI phones draw every element at its exact CSS size.
The two ramps are <input type="range"> controls drawn inside ctx.translate() and ctx.rotate().
Because drawElementImage() transforms the hit box along with the pixels, dragging either slider thumb changes its value and rotates both the drawn ramp and the physics segment in real time.
Whenever a ramp angle or flipper angle changes, the script toggles a data-moved attribute on that control to clear Chromium's cached hit-test region.
Collisions write back to the DOM.
A bumper hit increments its counter, toggles a .hit CSS class and updates the <output> scoreboard.
Rolling into the saucer sets open = true on the <details> element, expanding its laid-out height to reveal +250 before ejecting the ball.
Unticking the gate checkbox retracts the right-lane diverter bar so balls drop straight through the express lane.
Because those mutations change the elements' paint records, they raise real paint events even while the physics loop is paused.
The animation loop runs only while the table is in view (tracked with IntersectionObserver) and starts paused if prefers-reduced-motion: reduce is set, so you can launch on tap.
If the canvas fires contextlost (or the API isn't available), the page moves the controls into a plain HTML container beside the hidden canvas, and puts them back if contextrestored fires.
Live checks
The checks wait for a tap so the playfield starts with nothing else running.
Each check builds its own small fixture on the test stage, copies pixels into a plain 2D canvas with drawImage() before reading them, retires its fixture canvas via window.__gfx?.retire(), and leaves the table above untouched.
Findings
Not verified in Chromium or on an Android phone yet. I built this experiment on 2026-11-22 following the hit-testing work on /play/ and older DOM-physics toys (Google Gravity, Matter.js DOM wrappers) to see how far one drawable 2D canvas can carry live form controls inside a moving game.
- Single-canvas layering avoids CSS transform and z-index workarounds. Ball trails, bumper shockwaves, drop shadows and rotated form controls share one 2D paint pass, while the controls keep native keyboard focus and form state.
- Bumper hits and saucer traps are live DOM mutations. Adding
.hitto a bumper button or settingdetails.open = truechanges the element's CSS or height and triggers a realpaintevent. Run check 1 and check 4 above to measure this in your browser. - Rotated range sliders and pivoted flippers depend on transformed hit boxes. Check 2 and check 5 verify with
elementFromPoint()whether Chromium routes pointer input onto the rotated track and flipper blade after togglingdata-moved. - Touch separation relies on per-element
touch-action. The canvas setstouch-action: pan-yso swiping empty playfield scrolls the page on a phone, while the ramp sliders and flippers settouch-action: none. Not verified on real touch hardware. - Not verified: Chromium 157 headless pass rates, Chrome on Android touch dragging on rotated
<input type="range">tracks, frame rate during three-ball multi-ball with shockwaves, screen reader announcements for the live<output>scoreboard, and Web Audio latency on a phone.