Thinking with portals: split live HTML across Blue and Orange portals
Inspired by Valve's Portal (2007) and building on /portal/ and /dom-pinball/, I wanted to know if one live HTML card can sit halfway through a Blue portal while its other half sticks out of an angled Orange portal on one 2D canvas, keeping its momentum and working controls on both sides. Drag either card by its top bar, drag or rotate the portals, or tap a preset below.
Drag a card's dark header bar to pull or fling it through a portal. Drag a portal rim to move it, or grab its round end knob to rotate its angle.
Show the code half-plane clip splitting, rigid portal transform, hit-box draw order and fallback
- Two live
drawableHTML cards inside one 2D canvas. - Half-plane clipping, portal-to-portal rigid transform and velocity rotation.
- Drawing one element twice per frame with the pointer-side half drawn second.
- Portal crossing swap, momentum conservation and wall bounce.
- Plain HTML fallback on
contextlostand recovery oncontextrestored. - Fixed-palette card styles so both halves stay readable in light and dark themes.
How it works
Both cards live inside one <canvas id="portal-canvas" content="drawable" layoutsubtree>.
Each portal has a centre position, a half-length and a unit normal vector pointing into the chamber.
When a card's oriented bounding box straddles the Blue portal plane, I don't clone the DOM node.
Instead, the paint pass draws the same element twice in one frame with five-argument drawElementImage(el, -w / 2, -h / 2, w, h).
Pass A calls ctx.clip() on the half-plane in front of the entry portal and draws the card at its entry pose.
Pass B computes the rigid portal transform from entry to exit: it translates by -entry, rotates by exitNormal - entryNormal + Math.PI, applies the optional exit scale, translates to exit, clips to the half-plane in front of the exit portal, and draws the same element again.
Because the two clipped half-planes meet along the exact portal line, the card slices across the two angled portals with no gap and no overlap.
One element in HTML-in-canvas only has one hit-test region at a time (a limit first seen on /portal/).
On a 2D canvas, the last drawElementImage() call for an element sets that hit box.
So I track the pointer on pointermove and pointerdown, check which half is under or nearer the pointer, and draw that half second while toggling a data-hit-tick attribute to clear Chromium's hit-test cache.
Portal sightline previews are drawn before the main elements so they never steal hit testing from the cards.
When a moving card's centre crosses behind the entry portal plane, its position, angle and velocity swap onto the exit portal using Valve's Portal momentum rule ("speedy thing goes in, speedy thing comes out"): speed stays the same while the velocity vector rotates by exitNormal - entryNormal + Math.PI.
Unticking Preserve momentum inside the live card cuts exit speed to 35%, and changing Cube mode inside the second tile changes its bounce and gravity weight directly.
The loop pauses off screen via IntersectionObserver, respects prefers-reduced-motion: reduce, and moves both cards into plain HTML if contextlost fires.
Live checks
The six checks wait for a tap so the chamber starts with nothing else running.
Each check creates its own isolated fixture on the test stage, reads pixels through a plain drawImage() copy canvas with willReadFrequently: true, retires its fixture via window.__gfx?.retire() and leaves the chamber above untouched.
Findings
Not verified in Chromium or on an Android phone yet. I built this experiment on 2026-11-22 crediting Valve's Portal (2007) for the Blue and Orange portal transform and momentum conservation rule, /portal/ for double-drawing one element with pointer-tracked hit boxes, and /dom-pinball/ for running live HTML form controls inside a 2D physics loop.
- One element only has one hit box, so draw order decides which half you can click. Calling
drawElementImage()twice in one frame paints both clipped halves, and the second call sets the hit-test region. Drawing the half nearest the pointer second (and togglingdata-hit-tickwhen that half flips) lets you click the button sticking out of Blue and type into the input sticking out of Orange without cloning the element. Check 3 tests this directly withelementFromPoint(). - Half-plane
ctx.clip()splits live form controls cleanly across arbitrary exit angles. Because the exit pass appliesexitNormal - entryNormal + Math.PIbeforedrawElementImage(), the same DOM element can enter horizontally on one wall and emerge rotated 90 degrees or upside down from another wall. Checks 1 and 2 verify both halves' pixels on a plain copy canvas. - Sightline previews must paint before the real elements. Calling
drawElementImage()inside a portal's elliptical sightline preview after the cards would hand the element's hit box to the tiny preview. Drawing sightlines behind the cards keeps the final hit box on the active split half. - Zero-hover touch taps on the inactive half rely on synchronous retargeting. On a phone there is no
pointermovebefore a tap, so capture-phasepointerdownflips the active half and redraws immediately (falling back torequestPaint()if the browser rejects an out-of-paint draw). Whether a single tap activates a button on the previously inactive half without a second tap needs testing on Chrome for Android. - Not verified: Chromium 157 headless check results, Android 412 px touch dragging of cards and portal knobs, single-tap hit-box switching on the inactive half without hover, keyboard caret rendering when an
<input>is cut directly across a portal line, and frame rate during infinite-fall loops with sightlines on.