baked.design

How to put a web page under water

The Wishing Pond, Snowfall and Margin Dragon are real web pages you can type into while they ripple, freeze and burn. Here's how they work, one small step at a time. Every demo below is live.

Checking your browser…

Put the page inside a canvas

Normally a canvas only draws shapes and pictures. Chrome's new HTML-in-Canvas lets you put real HTML inside one and paint it yourself. The browser still lays it out, so it keeps working like normal HTML, but nothing shows until you draw it.

Type in the field. The reflection types too, because it's the same pixels drawn twice.

<canvas id="stage" layoutsubtree content="drawable">
  <form id="card" drawable>
    <input placeholder="Your name">
    <button>Say hi</button>
  </form>
</canvas>

<script>
  const ctx = stage.getContext("2d");
  stage.onpaint = () => {
    ctx.reset();
    ctx.drawElementImage(card, 0, 0);
  };
</script>

Bend the pixels

Once the page is pixels, you can hand them to WebGL and do anything a shader can do. Here every pixel is read from a slightly wavy spot. CSS can move and tilt a page, but it can't do this.

60%
// copy the 2D canvas into a WebGL texture after each paint
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,
  gl.UNSIGNED_BYTE, stage);

// fragment shader: read each pixel from a wavy spot
vec2 q = uv;
q.x += sin(uv.y * 18. + time) * .012 * wobble;
q.y += sin(uv.x * 13. + time) * .01 * wobble;
gl_FragColor = texture2D(page, q);

Make it react

The wobble gets interesting when it comes from you. Under the card is a grid of water heights. Moving your pointer drops pebbles, the waves spread, and the slope of the water bends the page. That's the whole Wishing Pond.

Drag across the water, then toss a coin. The button still works under the water.

// a grid of water heights; your pointer drops pebbles
heights[i] += 40;

// each frame, every cell becomes the average of its
// neighbours minus where it was: waves spread and fade
next[i] = (left + right + up + down) / 2 - prev[i];
next[i] *= 0.975;

// in the shader, the slope of the water bends the page
vec2 slope = vec2(right - left, up - down);
gl_FragColor = texture2D(page, uv + slope * .18);

Let the words make way

This is how text swims around the Margin Dragon, and it needs no canvas tricks at all. The dragon is a chain of circles along its spine. Every frame, each line of text works out which parts the circles leave free and fills them with as many words as fit. The rest of the story pours on into the next line.

Scribes have always known that a margin is not empty. It is where a book keeps its animals. They are drawn small so that no one will notice them, and drawn in red so that they will be noticed anyway. Most of them stay where they were put. A few, on long nights, when the lamp is low and the reader has not gone to bed, decide that the page is a field, and the words are only grass. They wander through it, and the lines part to let them pass, closing again behind them like water behind a boat.

Move across the page. Every word is still real text.

// every frame, each line gets the room the creature leaves
for (let y = top; y + lineHeight <= bottom; y += lineHeight) {
  let free = [[left, right]];
  for (const c of body) {               // circles along its spine
    const mid = y + lineHeight / 2;
    const dy = Math.max(0, Math.abs(c.y - mid) - lineHeight / 2);
    if (dy >= c.r) continue;
    const hw = Math.sqrt(c.r * c.r - dy * dy);
    free = cut(free, c.x - hw, c.x + hw);
  }
  for (const [a, b] of free) placeWordsThatFit(a, b, y);
}

Burn the page

The dragon's fire uses the same trick as the water. Under the page sits a grid of how burnt each spot is. The shader reads it and, as the number climbs, browns the paper, chars it, rims it with glowing embers, then burns right through. While the edge glows the burn creeps outward, then it cools and the page mends.

Click anywhere on the paper to set it alight. Then try typing.

// a grid of how burnt each spot is; a click lights one,
// and a glowing edge creeps outward until it cools
burn[i] += heat * falloff;

// in the shader, how burnt a pixel is picks its layer
float b = texture2D(burnMap, uv).r + (noise(uv * 30.) - .5) * .3;
vec3 c = mix(page, page * vec3(.8, .6, .45), smoothstep(.05, .35, b)); // toast
c = mix(c, vec3(.07), smoothstep(.5, .57, b));                         // char
float rim = smoothstep(.56, .6, b) * (1. - smoothstep(.62, .66, b));
c += rim * glow * vec3(1., .5, .12) * 2.5;                             // embers
c = mix(c, vec3(.02), smoothstep(.63, .69, b));                        // the hole

Ship it to everyone

HTML-in-Canvas is still an experiment, so normal Chrome only turns it on for sites that ask. Asking is free and takes two minutes.

  1. Register your sitePick HTML-in-Canvas on Chrome Origin Trials and enter your domain. You get a token.2 min
  2. Add the tokenOne meta tag in the page head, or an Origin-Trial header.1 min
  3. Check before you drawTest for drawElementImage and show the plain page where it's missing.5 min
  4. Test it for realOpen it in normal desktop Chrome. Safari, Firefox and phones get the plain page.1 min
<meta http-equiv="origin-trial" content="YOUR_TOKEN">

<script>
  const works = "drawElementImage" in CanvasRenderingContext2D.prototype;
  if (!works) {
    // Safari, Firefox and phones: show the page as it is
  }
</script>

Questions

Can people still click and type?

Yes. The real HTML is still there, and the browser maps clicks from the drawing back to it. Small wobbles are fine. Big ones can make a click land a little off.

Does it work on iPhone?

Not yet. It's Chrome on desktop for now. Always keep a plain version, and record a video for social, since most people scroll on phones.

Is it slow?

The page is only redrawn when it changes. The water runs on the graphics card, so it's light. Keep ripple grids small, around 200 cells across.

Is it accessible?

Screen readers and keyboards use the real HTML, so they work as normal. Respect reduced motion: calm the effect down or switch it off.

When does the trial end?

March 1, 2027. Chrome may ship it for everyone before then, or change it. Build so the plain page always works.

See the full ones

Wishing Pond: a form at the bottom of a koi pond. Type a wish, toss a coin, watch your words sink.

Snowfall: a form standing in the snow. Snow settles on its fields and letters, and you can brush it off.

Margin Dragon: a watercolour dragon swims through a page of real text, and its fire burns holes in it.

More things on Fun