Named effects and shader slots

Add effect="tilt" to <canvas-slot> and you don't write a render loop or any canvas code. Each slot becomes a GPU texture and is drawn through the fragment shader that the effect names. The content is still DOM. Move the pointer over the cards, click the blue one, type in the field.

No JavaScript here

This card leans toward your pointer because its parent says effect="tilt". Clicks land where the tilted card is drawn.

This one says effect="ripple". Click or tap it.

“Select this sentence. It's text in the page, drawn through a shader.”

<script type="module" src="/lib/canvas-slot.js"></script>

<canvas-slot effect="tilt">
  <article slot="note">…</article>
  <form slot="form" effect="ripple">…</form>
  <blockquote slot="quote">…</blockquote>
</canvas-slot>
Show the code effect="tilt", no render loop
  1. Name an effect on the <canvas-slot>, or on one slot.
  2. Load the element. That is the whole script.
  3. Drive it from CSS. Transitions and keyframes on --fx-progress, --fx-x and --fx-y (used by the cards further down).
  4. Or from script. The attribute can change at any time, and the properties are ordinary CSS variables.
  5. Inside: each slot becomes a WebGL texture.
  6. Inside, with backend="webgpu": the same thing for a GPU texture. device.queue.drawElementImageToTexture({ source: el }, { texture }).
  7. Inside: after drawing, tell the browser where the slot went.
  8. The whole renderer.

Pick an effect, drive it from CSS

The attribute can change at any time. --fx-progress, --fx-amount, --fx-x and --fx-y are registered custom properties, so sliders, transitions, keyframes and :hover all drive the shader.

Admit one

Change the effect above, then drag the sliders. The value goes straight into a CSS variable on this element.

Hover to burn

A CSS transition on --fx-progress. On a phone, use the button.

[slot] { transition: --fx-progress 800ms; }
canvas-slot:hover [slot] { --fx-progress: 1; }
Keyframes tilt this card. The link is clickable wherever it swings to.
@keyframes sway {
  from { --fx-x: -0.9 } to { --fx-x: 0.9 }
}

Shader slots: point the attribute at a fragment shader

effect also takes #id of a <script> or url(file.frag). The shader is one function: p is in CSS pixels from the slot's top-left and tex(p) samples the slot. It's a CSS filter that you write yourself. Edit the one on the left.

effect="#wobble"

Real text and a real link.

effect="url(scanlines.frag)"

This shader is a file next to the page.

<script type="x-shader/x-fragment" id="wobble">
  vec4 fx(vec2 p) { return tex(p + vec2(sin(p.y * 0.08 + u_time * 3.0) * 6.0, 0.0)); }
</script>

<canvas-slot effect="#wobble">…</canvas-slot>
<canvas-slot effect="url(scanlines.frag)">…</canvas-slot>
Show the code your own shader
  1. Point effect at a <script> by id, or at a file. The shader is one function.
  2. The file.
  3. Or register it by name, and both effect="shared-tint" and fx="shared-tint" can use it.

Effects are shared with the fx attribute

<canvas-slot effect> and the fx attribute share one list of effects, one shader contract and the same CSS properties. Use fx to shade one element where it already sits in your layout; use <canvas-slot> when several pieces share a canvas and you want to place them. An effect defined once works in both.

<canvas-slot effect="shared-tint">

Defined once with defineEffect().

<div fx="shared-tint">

The same effect on a plain element.

import { defineEffect } from '/lib/effects.js';
defineEffect('shared-tint', { glsl: `vec4 fx(vec2 p) { … }` });

WebGPU backend

Every effect slot on this page uses WebGPU by itself when the page has a usable device (a real adapter that passes a self-test) and each of its effects has WGSL source; the switch under the first demo names a route by hand and says why WebGPU isn't in use. backend="webgpu", as below, asks for it outright. It draws each slot with queue.drawElementImageToTexture() and a WGSL version of the effect (fn fx(p: vec2f) -> vec4f). Built-in WGSL: tilt, ripple, dissolve, glass, haze, crt, page-curl. If WebGPU can't start, rejects the first frame or loses the device, the element falls back to WebGL by itself.

backend="webgpu"

Click for ripples.

How it works

Live checks

ResultExperimentDetail