Live view transitions
Shatter, fold, burn or morph between two states. A view transition animates screenshots. Here both states are DOM for the whole transition, so a clock keeps ticking on a flying shard, and you can stop half way and press a button on either side.
Welcome back
This card is a slotted <section>. Type here, then start a slow transition and keep typing.
Dashboard
A note
Show the code live transitions
- Each child of
<live-transition>is one state. Ordinary HTML.data-morphnames the parts that the morph effect pairs up. - Switch states with
show(), orbegin()and setprogressto hold part way.effectis the name picked in the row of buttons. - Add your own effect: a GLSL function. Both states arrive as live textures.
- Inside the element: each state becomes a texture. This is the WebGL route; on WebGPU the call is
device.queue.drawElementImageToTexture({ source }, { texture }).gl.texElementSubImage2D(…, element)copies the element's current rendering into a texture. It runs in the canvaspaintevent, and only for states the browser repainted. - Inside the element: keep clicks landing on what you see. WebGL draws don't move an element's hit box, so
canvas.updateElementGeometry(el, { canvasTransform })tells the browser where the piece under the pointer was drawn. - The whole element.
Write your own transition shader
Define vec4 transition(vec2 uv). You get getFromColor(uv), getToColor(uv), progress from 0 to 1,
ratio (width ÷ height) and origin (where you clicked). It's the gl-transitions convention, so a shader written for that works here.
Both states are live textures, so whatever you write is drawn from the DOM as it is this frame.
Use it
<script type="module" src="/lib/live-transition.js"></script>
<live-transition effect="shatter" duration="900">
<section>…first state…</section>
<section>…second state…</section>
</live-transition>
<script>
const lt = document.querySelector('live-transition');
await lt.show(1); // play to the end
await lt.begin(0, { effect: 'burn', origin: { x: 40, y: 40 } });
lt.progress = 0.4; // hold it there, both states interactive
await lt.play();
</script>
Give elements in two states the same data-morph name and the morph effect flies one into the other.
New mesh effects are one function: LiveTransition.effects.name = (progress, { W, H, quad, tri }) => ….
Shader effects are a string: LiveTransition.shaders.name = { glsl: 'vec4 transition(vec2 uv) { … }' }.
Without HTML-in-canvas the element shows one child at a time and falls back to document.startViewTransition.
Live checks
These run in your browser when this section is on screen, against the small rig below. They don't touch the demo above.
Findings
- It uses WebGPU, then WebGL. Every built-in effect is written in both WGSL and GLSL. The element uses WebGPU when the browser has a real adapter that passes a self-test and accepts the first frame, and WebGL otherwise; the line under the controls says which and why, and
backend="webgpu|webgl"names one by hand. A shader effect given only as GLSL (like Yours) moves the element to WebGL. - Both states take input and repaint during the transition. Whenever the browser repaints a state (the
paintevent lists which), it's uploaded again withtexElementSubImage2D, so CSS animations, typing, checkbox ticks and selection highlights appear on shards, folds and embers as they happen. - The mesh is also used for hit testing. WebGL draws don't move an element's hit box, so the element keeps the last frame's triangles. On pointer move it finds the triangle under the pointer, works out the affine matrix that maps that piece of the state to the screen, and hands it to
updateElementGeometry. You can press a button that is riding a shard. - One element has one hit box. A shattered state is 100+ pieces but the element can only be in one place, so routing follows the pointer. Two pointers on two shards would disagree. Perspective is approximated per triangle.
- Burn is routed per pixel. The shader's noise is also kept on the CPU, so the pointer goes to the new state where the fire has passed and the old one where it has not.
- Your own shader samples both states. A shader effect is a
transition(uv)function withgetFromColorandgetToColor. The two textures are the two states as the browser painted them this frame, so a shader written for still images runs over an interface that is still changing. A shader has no geometry, so it can bring a smallpick()function to say which state owns each point; without one the states swap at half way. - Morph needs no extra snapshots. Paired elements are sub-rectangles of the two state textures. The hole they leave is filled by smearing the nearest pixels outside it, which is right for flat backgrounds and visible on busy ones.
- A state that was never painted has no picture. A
visibility: hiddenstate can't be uploaded, so the element waits two paints before the first frame. Anything in the top layer (modal dialogs, popovers) isn't part of the state's picture either.