Dolly-zoom camera stretch
I wanted to know if one ElementGL canvas can move a camera backward while zooming its lens from 18 mm to 135 mm so a foreground HTML card holds its exact 1:1 width while a background HTML layer and 3D corridor expand behind it. Drag the focal slider, scrub sideways across the stage, switch shots inside the foreground card or toggle Zoom only to compare.
Show the code Dolly-zoom camera math, ElementGL upload, GLSL corridor and DoF shader
- Two drawable HTML layers in one canvas. The background parallax layer (streetlights, horizon slats and distance badges) sits ahead of the foreground hero slate card inside
<canvas content="drawable" layoutsubtree>. - Constant subject scale and projective hit-testing. Setting camera distance
d = d0 * (f / f0)keepsf / dconstant atz = 0, while scalingbg-layerbyf / (d + 5.40)and syncingupdateElementGeometry()viageometryUnit(). - 3D corridor raycaster, telephoto DoF and optical vignette. Pass 0 raycasts the active shot tunnel (
Vertigo,JawsorCorridor), Pass 1 applies focal-length bokeh to#bg-layer, and Pass 2 composites the sharp hero card. hasPaintedgate, premultiplied alpha and fallbacks. SetsUNPACK_PREMULTIPLY_ALPHA_WEBGL = true, callsstage.begin()beforestage.upload()insidepaint, and falls back to 5-argumentdrawElementImage()or plain HTML.
How it works
Everything runs on one WebGL 2 context created with new ElementGL(canvas, ...) from /lib/element-gl.js, with gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true) enabled on startup and restore so transparent gaps in the background HTML layer stay clear. Inside the canvas sit two drawable HTML layers: a background scene card at z = -5.40 m (with streetlights, horizon slats and distance badges) and a foreground subject card at z = 0 (with a live <input>, three shot buttons, a scale <output> and a take button).
In pinhole optics, a lens of focal length f gives a field-of-view tangent proportional to 1 / f, so keeping field-of-view width times camera distance constant means moving the camera to d = d0 * (f / f0). At 18 mm, the camera pulls in to 0.86 m; at 135 mm, it dollies back to 6.48 m. Because f / d doesn't change at z = 0, the foreground card holds its exact 1.00x CSS width across the whole range, while the background plane at z = -5.40 m grows from 0.45x to 1.77x (a 3.95x stretch). Switching to Zoom only locks d = 2.40 m so both cards zoom together from 0.36x to 2.70x.
After each draw, syncDollyGeometry() calls geometryUnit() once per resize or zoom change, writes each card's projected transform through canvas.updateElementGeometry() and flips dataset.hitTick so clicks and text selection inside the foreground card stay aligned at every focal length. In the shader, Pass 0 raycasts the receding 3D geometry for Vertigo, Jaws or Corridor with focal-length depth fog and corner vignette, Pass 1 softens the background DOM texture with a 9-tap circular blur as focal length rises, and Pass 2 draws the foreground card sharp. If WebGL 2 texElementSubImage2D() isn't available, the 2D fallback draws the tunnel and both layers with 5-argument drawElementImage(); on webglcontextlost or missing API support, both elements move into plain HTML below the hidden stage.
Live checks
These checks run on isolated fixtures inside testStage() and wait for a tap so opening the page doesn't allocate extra WebGL contexts or move focus.
| Status | Check | Detail |
|---|
Findings
- Measured in
HeadlessChrome/160.0.0.0on 2026-11-27 (412 px wide, DPR 2.625, withCanvasDrawElementenabled): all five live checks pass with 0 console errors, 1.096 MP backing store (8.77 MB initial, 12.91 MB peak during checks) and 8x anisotropic mipmaps on oneElementGLcontext. - In
Keep 1:1 widthmode from 18 mm to 135 mm,heroCard.getBoundingClientRect().widthstayed locked at252.00 px(scaleDelta = 0.00 px) whilebgLayerstretched from278.07 pxto1099.64 px(bgDelta = 821.6 px,3.95x). InZoom onlymode, the foreground card zoomed7.50xacross the same range (reaching680.40 pxat 135 mm). - Real CDP clicks inside the canvas hit the
Jawsshot selector button,Cue take #1(updating toCue take #2) and#subject-input(inserting text at the clicked caret). Launching with--disable-blink-features=CanvasDrawElementhid the stage viewport and moved both layers cleanly into#plain-hostwith 0 overflow. - Running
/lib/a11y-audit.json the page passed 8 checks (calm,blank,dom,hit,ghost,names,sharp,motion) with 0 failures;focusandzoomstayed warnings becauseElementGL.#syncSize()doesn't multiply canvas backing pixels bycurrentCSSZoomon its own. npm run check:jank,node tools/draw-size/audit.mjs, the standalone 2DdrawElementImage()fallback and real Chrome 159 on Android hardware are not verified yet.