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.

Test card 01Drag
Portals crossed: 0
Tile 02Drag
Crossings: 0 · Companion
Test card 01 is split across Blue and Orange. Click controls on either half.

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
  1. Two live drawable HTML cards inside one 2D canvas.
  2. Half-plane clipping, portal-to-portal rigid transform and velocity rotation.
  3. Drawing one element twice per frame with the pointer-side half drawn second.
  4. Portal crossing swap, momentum conservation and wall bounce.
  5. Plain HTML fallback on contextlost and recovery on contextrestored.
  6. 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.