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.

SUBJECT · Z = 0 1.00x · 50 mm
d = 2.40 m
starting...

Show the code Dolly-zoom camera math, ElementGL upload, GLSL corridor and DoF shader
  1. 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>.
  2. Constant subject scale and projective hit-testing. Setting camera distance d = d0 * (f / f0) keeps f / d constant at z = 0, while scaling bg-layer by f / (d + 5.40) and syncing updateElementGeometry() via geometryUnit().
  3. 3D corridor raycaster, telephoto DoF and optical vignette. Pass 0 raycasts the active shot tunnel (Vertigo, Jaws or Corridor), Pass 1 applies focal-length bokeh to #bg-layer, and Pass 2 composites the sharp hero card.
  4. hasPainted gate, premultiplied alpha and fallbacks. Sets UNPACK_PREMULTIPLY_ALPHA_WEBGL = true, calls stage.begin() before stage.upload() inside paint, and falls back to 5-argument drawElementImage() 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.

StatusCheckDetail

Findings