Two-sided 3D hardcover book with live HTML pages
I wanted to know if one WebGL 2 canvas could curl both sides of a live HTML leaf across a shaded book spine, while links, checkboxes, notes and text selection still work on the open spread. Drag any outer page corner to turn a leaf, hold a turn mid-air with the slider, or drag the silk ribbon in the gutter.
Starting book canvas…
Show the code the six pages, the cylindrical curl shader, the mesh, hit testing and drag
- The six drawable pages. Each page sits directly inside one
<canvas content="drawable" layoutsubtree>as adrawablesection. - The cylindrical curl shader. One WebGL 2 program bends the leaf round a rolling cylinder, picks front or horizontally mirrored back via
gl_FrontFacing, and shades the spine gutter and roll crest. - The 28 by 18 mesh on ElementGL. The page opens one WebGL 2 context through
new ElementGL()and builds the subdivided leaf mesh and flat spread quad onstage.gl. - Uploading, drawing and syncing hit boxes. Every
paintevent uploads changed pages withstage.upload(), draws the spread and curling leaf, and updatesupdateElementGeometry()plusinertanddataset.hitTick. If WebGL 2 isn't available, a 2D folded-slice route draws with 5-argument and 9-argumentdrawElementImage(). - Corner drag and silk ribbon. Dragging past 40% completes the page turn; releasing earlier springs back. Dragging the ribbon snaps the bookmark to the open spread.
How it works
- All six pages live inside one
<canvas content="drawable" layoutsubtree>as directdrawablechildren: cover (page 1), field notes and checklist (pages 2 and 3), compass plate and margin notes (pages 4 and 5), and colophon (page 6). They form three physical two-sided leaves: 1/2, 3/4 and 5/6. - I opened one WebGL 2 context with
new ElementGL(canvas, …)from/lib/element-gl.jsand compiled two shader programs onstage.gl: a flat book shader for the hardcover boards, page stack, ribbon and stationary spread (with spine-gutter ambient occlusion and a soft cast shadow from the curling leaf), and a cylindrical curl shader for the moving leaf. - The cylindrical geometry follows W. David MacDonald and Calin-Andrei Burloiu's rolling-cylinder model, adapted so the spine edge stays pinned to
x = 0. Grabbing a top or bottom corner tilts the cylinder axis during the first half of the turn and straightens it back toward the spine as the leaf lands. This builds on the single-corner HTML page-peel shader in /view-source/ and classic page-curl UIs such as Apple Books and Turn.js. - The moving leaf is a 28 by 18 triangle mesh. Its fragment shader checks
gl_FrontFacing(adjusted when turning backward from left to right) to sample the front HTML texture on the near face and the back HTML texture with1.0 - vUv.xon the curled underside. When the leaf lands flat on the left, text on the back page reads left to right. - On every settled spread, the script calls
canvas.updateElementGeometry()with the flat pixel rectangle of each visible page, togglesdataset.hitTickto clear Chromium's hit-test cache, and marks hidden pagesinertwithpointer-events: nonewhile leaving their geometry inside the canvas so Chromium keeps their pictures painted for the next curl. - Below 580 CSS pixels wide (such as a 412 px Android phone), the book switches to a single-page layout with the spine on the left edge, keeping the backing store below 4 megapixels and touch targets large.
- If WebGL 2 or
texElementSubImage2D()is missing, the book draws a 2D folded-slice spread with 5-argument and 9-argumentdrawElementImage(). If HTML-in-canvas isn't available orwebglcontextlostfires, the six pages move into plain HTML untilwebglcontextrestoredbrings the canvas back.
Live checks
Each check builds its own small fixture on the hidden test stage and retires its canvas when done. The visible book above isn't touched.
| Result | Experiment | Detail |
|---|
Findings
Live browser runs in Chromium 157 and Chrome 159 on Android are not verified yet. Press "Run these 7 checks" above to measure your browser.
On this device: turn a page or drag the curl slider to record canvas size, paint count and active route here.
- Not verified yet: real rendering, two-sided texture uploads, back-page mirroring,
updateElementGeometry()hit testing, 2D folded-slice fallback and frame rate in Chromium 157 and Chrome 159 on Android (412 px wide, scale 2.625). - Design limit while held mid-curl:
updateElementGeometry()takes one flat matrix per element, not a curved mesh. Controls stay interactive on the settled spread (and on the stationary flat side), while the curved paper itself isn't hit-tested along the cylinder. - Corner margins: four 68 by 68 px corner grab zones sit above the canvas with
touch-action: noneso corner drags don't scroll the page on touch screens. Page padding keeps buttons, inputs and text away from those four corners. - Credits: builds on /view-source/ for the single-corner HTML page-peel shader, Apple Books and Turn.js for page-curl interaction, and W. David MacDonald and Calin-Andrei Burloiu for the cylindrical page-curl geometry.