Escher Droste spiral
I wanted to know if an analytic complex-logarithm Droste spiral and a ping-pong mirror tunnel could wrap one live editable HTML card across 10+ octaves of scale, and whether gl.generateMipmap() and anisotropic filtering stay artefact-free on a texElementSubImage2D() texture.
Type in the input, switch the card's colour theme, drag the canvas to zoom or twist, and toggle between bilinear, trilinear mipmaps and anisotropic filtering to compare legibility across the inner turns.
Drag canvas to zoom and rotate · tap controls on the outer frame
Show the code framed cutout card, complex-log Droste shader, mipmap upload, ping-pong tunnel and hit sync
- The framed card with a transparent cutout. Four grid rails carry the live input, theme buttons, ticking clock and 2 px test patterns while
#droste-cutoutstays empty and lets pointer drags pass through to the canvas. - The conformal Escher Droste fragment shader. Maps screen coordinates through a perspective homography and complex logarithm, wraps each octave to the measured cutout ratio, and passes the analytic Jacobian to
textureGrad()so mipmaps and anisotropy stay free of branch-cut seams. - The ping-pong mirror tunnel pass. Composites the live framed card over a shrunk, rotated and colour-shifted copy of the previous framebuffer so every keystroke or button tap echoes down the tunnel.
- Uploading with
texElementSubImage2D()and building mipmaps. Callsgl.generateMipmap(gl.TEXTURE_2D)right after upload and switches betweenLINEAR,LINEAR_MIPMAP_LINEARandEXT_texture_filter_anisotropic. - Keeping the outer frame clickable. Updates
canvas.updateElementGeometry()with the outermost card transform and togglescard.dataset.hitTickso Chromium refreshes its hit-test cache.
How it works
M. C. Escher's 1956 lithograph Print Gallery hides a conformal logarithmic spiral inside a rectangular frame, worked out analytically by Hendrik Lenstra and Bart de Smit in 2003.
I wanted to test the same mapping on a live HTML form drawn by texElementSubImage2D(), because a Droste spiral packs ten or more octaves of minification into one frame and immediately exposes whether WebGL 2 mipmaps and anisotropic filtering behave on a live DOM texture.
One ElementGL instance from /lib/element-gl.js owns the page's single WebGL 2 context.
The source card (#droste-card) is a square 3 by 3 CSS grid whose centre cell (#droste-cutout) has a transparent background and pointer-events: none.
Each time the card paints, stage.upload(card) copies its current picture into an RGBA8 texture via gl.texElementSubImage2D(), and when mipmaps are enabled the page calls gl.generateMipmap(gl.TEXTURE_2D) straight afterwards.
WebGL 2 permits mipmap chains on non-power-of-two textures, which matters here because a responsive HTML card at device scale 2.625 almost never lands on a power-of-two pixel size.
In Conformal Droste mode, the fragment shader measures the card's actual cutout ratio r1 = cutout.offsetWidth / card.offsetWidth, sets the log-period L = ln(1 / r1), and maps complex coordinates z through w' = (1 + i * strands * L / (2 * pi)) * (ln(z) - offset).
Because crossing the atan2 branch cut shifts w' by an exact integer multiple of L and a full 2 * pi turn, wrapping the Chebyshev norm max(|x|, |y|) into [r1, 1] produces no seam.
Calling plain texture() across that wrap boundary still spikes hardware screen-space derivatives and leaves a 1 px blurry line, so the shader computes the smooth derivative dFdx(z) and dFdy(z) before the logarithm, multiplies by the analytic complex Jacobian c * zTile / z, and samples with textureGrad().
Tilting the plane in perspective turns the isotropic spiral footprint into an elongated ellipse, which lets you see the difference between LINEAR_MIPMAP_LINEAR and EXT_texture_filter_anisotropic on the 2 px grid and typography.
In Infinite Mirror Tunnel mode, the same stage.gl context drives two small RGBA8 ping-pong framebuffers (capped at 768 px on a side).
Pass 1 samples the previous framebuffer shrunk, rotated and subtly hue-shifted, then composites the upright live card over it so the transparent cutout reveals an endless stack of earlier frames.
Every keystroke, theme switch or clock tick triggers a short burst of feedback passes so the change ripples down the hall of mirrors before settling to rest.
After layout or view updates, the page calls canvas.updateElementGeometry(card, { canvasTransform }) and increments card.dataset.hitTick so clicks and typing land on the outermost frame.
An IntersectionObserver pauses the 1 Hz clock and any auto-zoom loop when the stage leaves the viewport.
If WebGL 2 isn't available, a 2D canvas fallback draws concentric nested copies using five-argument drawElementImage(); if HTML-in-canvas isn't enabled or the WebGL context is lost, #droste-card moves out of the canvas so the controls stay usable as plain HTML.
Live checks
These checks wait for a tap so the demo opens with one GPU context. Each check builds its own small fixture on the test stage and retires its canvas when finished.
Findings
- Not verified in a live browser yet: none of the live checks or visual modes have been run in Chromium 157 or Chrome 159 for Android. Open this page with HTML-in-canvas enabled and run the checks above to record results on your device.
- Sampling a Droste wrap with
textureGrad()and the analytic Jacobian0.5 * c * (zTile / z) * dFdx(z)avoids the 1 px coarse-mipmap seam that hardwaredFdx(uv)produces at theatan2branch cut and octave wrap edge. - A pure conformal Droste transform has an isotropic 1:1 texture footprint everywhere, so
EXT_texture_filter_anisotropiconly diverges visibly from trilinearLINEAR_MIPMAP_LINEARonce you add perspective tilt (the 52 deg preset exposes foreshortened turns right away). - Setting
pointer-events: noneon#droste-cardand#droste-cutoutwhile keepingpointer-events: autoon the four frame rails lets drags inside the centre window zoom and rotate the spiral while taps on the outer frame hit the live input and theme buttons. - Both the analytic Droste shader and the two-FBO ping-pong mirror tunnel share one
ElementGLWebGL 2 context, keeping the page at one GPU context and well inside the 4 megapixel and 96 MB mobile canvas limits.