Five follow-ups to the Chrome APIs plan

Five small experiments that the Chrome APIs plan left open, on one stage. Pick one. Semantic zoom swaps a tile that is too small to read for a one-sentence stand-in. Column pages turns the columns of a multi-column element like pages. Text rectangles compares the selection rectangles of canvas text with those of a DOM range. Float buffer draws a panel into a 16-bit float WebGL buffer with a bloom pass. Effect from CSS picks the shader effect of each card with a custom property. The stage starts on a 2D canvas. The WebGL context is made when one of the last three is first chosen.

This browser can't draw elements into a canvas, so the five experiments aren't shown.

Paint event

The canvas gets a paint event when a child changes. The handler draws each child, so a page with no animation draws nothing between changes.

Hit testing

A child is clicked where it was last drawn. A 2D draw records the place by itself, and a WebGL or WebGPU draw reports it with updateElementGeometry().

Float canvas

A 16-bit float canvas keeps CSS colour values above 1.0 from a drawn element. An HDR screen shows them brighter than white.

Readback

Reading float pixels back from a shown canvas lost the canvas on my phone. The pages read from a copy, or only when asked.

Textures

WebGL and WebGPU receive an element as an 8-bit picture. A shader decides which pixels go above 1.0.

Custom properties

A change to a custom property alone raises no paint event. A page that drives a shader from CSS reads the computed values once a frame.

AV fi office עברית Wave 12:45

Columns as pages

This element has five columns and a fixed height. The text runs down the first column and continues at the top of the next, which is what a book does at the foot of a page.

The element is 1,250 pixels wide and the stage shows 250 of them. The canvas uploads the element once as one picture and draws one fifth of it on a quad. The other four fifths are the other pages.

When you ask for the next page, the canvas draws two quads from the same picture. The page underneath is flat. The page on top goes through a shader that rolls it over a cylinder, and the cylinder moves from the right edge to the left.

What stays HTML

The text is still text. It can be selected, a screen reader reads it in order, and this link can be followed. After each draw the canvas tells the browser where the picture is, shifted left by the width of the pages before the one on show.

Only the page that is shown lines up with its picture. A tap lands on the text of that page, and the other pages are outside the canvas.

What the browser does

The browser breaks the text into columns. Nothing in the script measures a line or decides where a page ends. Change the font size or the words and the page breaks move by themselves.

The multi-column scroller with its column pseudo-elements and scroll markers is the other way to do this. The live checks measure what a canvas gets from that route.

Limits

The picture of five pages is one texture, 3,281 pixels wide on my phone. Ten pages would pass the largest texture many phones allow, so a longer book needs one element for each handful of pages.

The last page ends here.

Build 482 7
Card one--fx:
Card two--fx:
Card three--fx:

 

Tap a tile to zoom in on it. Tap it again to zoom out.
1 of 5
Or select part of the top line.
These rules are live. Edit them: the effect names are none, wave, split and lines.
Show the code five paint routines on two canvases, one shader
  1. Semantic zoom. Each tile and its stand-in are drawn at the same place, with the alpha set by the size of the tile's text on screen.
  2. The Summarizer call that replaces the stand-in text.
  3. Column pages: the element. Five columns in one element.
  4. Column pages: the draw. Two quads from one texture while a page turns.
  5. The fragment shader. Four modes: plain, page turn, raise above 1.0, effect chosen in CSS.
  6. Text rectangles. Range.getClientRects() on the drawn element and TextMetrics.getSelectionRects() on canvas text.
  7. Float buffer. The panel goes into a float texture, then the bloom chain.
  8. The float drawing buffer and extended tone mapping.
  9. When the float buffer is asked for.
  10. The bloom passes.
  11. Effect from CSS: the rules.
  12. Effect from CSS: the read. A registered <custom-ident> becomes a number for the shader.

How it works

Semantic zoom. Six tiles and six stand-ins are children of a 2D canvas. The canvas draws each tile at its place in a 3 by 2 grid, scaled by the zoom. Body text is 14 pixels, so under a zoom of about 0.6 it is below 8.5 pixels on screen. Between 0.5 and 0.72 the tile fades out and its stand-in fades in at the same rectangle. The stand-in holds the first sentence of the tile until "Use the Summarizer API" is pressed, which asks Summarizer for a headline of each tile. Both are elements, so a tap on either one reaches a click handler at any zoom.

Column pages. The article has columns: 5, a fixed height and no gap, and is 1,250 pixels wide. Its picture is uploaded once as a texture. A page is one fifth of the texture. During a turn the shader maps the top page onto a cylinder that rolls from right to left and shows the back of the paper where it has come over. After each draw updateElementGeometry() places the element so that the shown page lines up with its picture.

Text rectangles. The top line is a DOM element drawn with drawElementImage(), and the blue boxes are Range.getClientRects() for the chosen characters. The bottom line is the same string drawn with fillText() in the same font, and the orange boxes are TextMetrics.getSelectionRects(), which is in Chrome 160 and behind the experimental features flag before it. The status line gives both lists and the largest difference.

Float buffer. texElementSubImage2D() gives WebGL an 8-bit picture, so no value above 1.0 arrives. The shader multiplies the badge, the button and the full-white pixels of the strip, and draws into an RGBA16F texture. A bright pass keeps what is above 1.0, four blur passes at a quarter of the size spread it, and the composite adds it back. The canvas's own buffer is asked to be RGBA16F with drawingBufferStorage(), in Display P3, with drawingBufferToneMapping({ mode: 'extended' }) where that exists. The page asks for the float buffer at the start only on a screen that matches (dynamic-range: high). If the stage is empty with the float buffer, choose the 8-bit one: the bloom is the same and nothing passes white.

Effect from CSS. --fx is registered as a <custom-ident>. Once a frame the page reads it from each card with getComputedStyle() and looks the name up in a table of effect numbers, next to the number and colour uniforms that lib/css-uniforms.js reads. The binding is in this page's own code. lib/shader-fx.js wasn't changed, because it's shared by other pages.

Live checks

These wait for a tap, because two of them use the page's WebGL context and read pixels back from it. The 2D checks run on their own small canvases in the corner of the window. The WebGL checks draw a 40 pixel fixture into the float texture, not the demo.

Findings