Shader CSS
Add fx="ripple" to an element and it's drawn through a fragment shader. The element is still an element. Its buttons and fields work, and its text can be selected and is in the accessibility tree. CSS custom properties
are the shader's uniforms, so transitions and :hover animate it.
Ripple
Click or tap anywhere on this card. Rings spread from each press.
<div fx="ripple">
<form fx="glass">
Heat haze
Rising noise bends the element. This link still works.
<div fx="haze">
<form fx="crt">
Dissolve
A CSS transition on --fx-progress burns this card away and back.
.burn { transition: --fx-progress 900ms }
Page curl
Drag the slider to peel the corner. The value goes straight into a CSS variable.
style="--fx-progress: 0.28"
Show the code fx="…" on any element
- Add
fx="name"to an element. Three of the cards above. Nothing else about them changes. - Drive the shader from CSS.
--fx-progressis a registered custom property, so a transition animates the shader uniform. - Load the library; set a variable from script if you want.
$isgetElementById. - Inside the library: the element is wrapped in a canvas. A custom element with this shadow root takes the element's place, so it becomes a child of a drawable canvas without its markup changing.
- Inside the library: the element becomes a WebGL texture.
gl.texElementSubImage2D(…, element), into storage of exactly the element's image size. - Inside the library: draw one quad through the effect's fragment shader.
- Inside the library: keep clicks on the element.
- The whole library. The effects themselves are in /lib/effects.js.
<script type="module" src="/lib/shader-fx.js"></script>
<button fx="ripple">Still a button</button>
<style>
.card { transition: --fx-progress 700ms; }
.card:hover { --fx-progress: 1; }
</style>
<article class="card" fx="dissolve">Hover to burn</article>
Write your own
An effect is one function, written twice: in GLSL for WebGL and in WGSL for WebGPU. p is in CSS pixels from the element's top-left; tex(p) samples the element. Edit the one that is running (the line under the editors says which): the card updates as you type.
Your shader
Some text, a link and a field.
defineEffect('custom', { animated: true, glsl, wgsl })
Show the code your own effect
- Use the name in
fx. - Register the function under that name, in GLSL and in WGSL.
glsl.valueandwgsl.valueare the text in the editors: avec4 fx(vec2 p)and afn fx(p: vec2f) -> vec4fthat sample the element withtex(p). Leavewgslout and the element is drawn with WebGL. CallingdefineEffectagain replaces it. - A built-in effect for comparison.
Use fx on your own page
The library is one file, /lib/fx.js, with no dependencies. It needs no build step. Load it as a module from this site, or download it and serve your own copy. It wraps every element that has an fx attribute, including elements added later.
<script type="module" src="https://html-in-canvas.page/lib/fx.js"></script>
<button fx="ripple">Still a button</button>
Built-in names: ripple, glass, haze, crt, dissolve, curl, tilt and none. In a browser without HTML-in-canvas the library does nothing and the page stays plain HTML. This stand-alone page loads only that file; view its source for a complete example.
Add your own effect
An effect is a function that returns the colour of one pixel. Write it in GLSL for WebGL and in WGSL for WebGPU, then register both under one name.
- Write the function. The GLSL signature is
vec4 fx(vec2 p)and the WGSL signature isfn fx(p: vec2f) -> vec4f.pis the pixel's position in CSS pixels from the element's top-left corner.tex(p)returns the element's colour at a position, with premultiplied alpha, and transparent outside the element. Return a premultiplied colour. - Register it with
defineEffect(name, { glsl, wgsl }). Calling it again with the same name replaces the effect, and elements that use it are redrawn.<script type="module"> import { defineEffect } from 'https://html-in-canvas.page/lib/fx.js'; defineEffect('wobble', { glsl: `vec4 fx(vec2 p) { float dx = sin(p.y * 0.12 + u_time * 4.0) * 5.0 * u_amount; return tex(p + vec2(dx, 0.0)); }`, wgsl: `fn fx(p: vec2f) -> vec4f { let dx = sin(p.y * 0.12 + u.time * 4.0) * 5.0 * u.amount; return tex(p + vec2f(dx, 0.0)); }`, bleed: 8, }); </script> - Or declare it in markup, with no script of your own. Use one
<script>element per language, each withdata-effect="name". Thetypemust be one the browser doesn't run; a type containingwgslis read as WGSL and any other as GLSL.<script type="text/glsl" data-effect="stripes"> vec4 fx(vec2 p) { vec4 c = tex(p); float stripe = step(0.5, fract((p.x + p.y) / 16.0)); c.rgb *= 1.0 - 0.3 * stripe * u_amount; return c; } </script> <script type="text/wgsl" data-effect="stripes"> fn fx(p: vec2f) -> vec4f { let c = tex(p); let stripe = step(0.5, fract((p.x + p.y) / 16.0)); return vec4f(c.rgb * (1.0 - 0.3 * stripe * u.amount), c.a); } </script>fx="#id"reads a single<script id>, andfx="url(wobble.frag)"orfx="url(wobble.wgsl)"fetches a file. Both forms hold one language only. - Use the name, and set the inputs from CSS. The four custom properties are registered as numbers, so transitions, animations and
:hoverchange the shader input.<div fx="wobble" class="card">…</div> <style> .card { --fx-amount: 0.4; transition: --fx-amount 300ms; } .card:hover { --fx-amount: 1.5; } </style>
Inputs
The same values are available in both languages. GLSL has them as separate uniforms and WGSL as fields of one struct, u.
| GLSL | WGSL | Value |
|---|---|---|
p | p | Position of the pixel in CSS px from the element's top-left. It's negative or larger than the size inside the bleed margin. |
tex(p) | tex(p) | The element's colour at p, premultiplied. Transparent outside the element. |
u_size | u.size | Element width and height in CSS px. |
u_time | u.time | Seconds since the library loaded. Stays 0 when the visitor asks for reduced motion. |
u_progress | u.progress | --fx-progress on the element. Default 0. |
u_amount | u.amount | --fx-amount. Default 1. |
u_xy | u.xy | --fx-x and --fx-y. Default 0, 0. |
u_pointer | u.pointer | Pointer position in the same px as p. (-10000, -10000) when no pointer is over the element. |
u_hover | u.hover | 0 to 1. Eases to 1 while a pointer is over the element and back to 0 after. |
u_clicks[i] | u.clicks[i] | The last four presses, i from 0 to 3: .xy is the position and .z the age in seconds. .z is -1 for an unused entry. A press is dropped after 2.6 seconds. |
unproject(p) | unproject(p) | For an effect with a geometry function: the point of the element drawn at screen position p. |
hash(p)noise(p)fbm(p) | the same | Value noise helpers that return 0 to 1. |
Options
| Option | Type | Meaning |
|---|---|---|
glsl | string | The WebGL version of the function. |
wgsl | string | The WebGPU version of the function. |
animated | boolean | Redraw every frame. Defaults to true when the source uses u_time or u.time. An effect that isn't animated is redrawn when the element repaints or an input changes. |
bleed | number | CSS px of extra canvas on every side of the element. Set it when the effect draws outside the element's box, for example a displacement or a glow. Default 0. fx-bleed="12" on an element overrides it. |
geometry | function | ({ width, height, pointer, hover, progress, amount, x, y }) => DOMMatrix. Where the element appears on screen. The matrix is given to updateElementGeometry(), so clicks follow the picture, and the shader gets its inverse as unproject(p). The built-in tilt uses it. Without it the element is hit tested at its layout box. |
Which route draws it, and errors
- Both languages: the element is drawn with WebGPU where the browser has a usable WebGPU device, and with WebGL elsewhere.
- GLSL only: the element is always drawn with WebGL.
- WGSL only: the element is shaded on WebGPU. Where the browser falls back to WebGL, it's drawn without the effect.
- A function that doesn't compile: the element is drawn without the effect, and the compiler's message is logged to the console once. It's also on the wrapper:
element.closest('fx-surface').error. The wrapper'sbackendproperty is'webgpu','direct'or'shared', andgpuNotesays why WebGPU isn't in use. fx-backend="webgpu"orfx-backend="gl"on the element picks a route by hand, which helps when the two versions of a function need comparing.- One copy per page. A second copy of the library on the same page logs a warning and does nothing. Import
defineEffectfrom the same URL as the<script>tag.
Show the code a page that uses only /lib/fx.js
- Load the one file.
- Declare an effect in markup, in both languages.
- Define an effect from script, in both languages.
- Put the names in
fx. - Drive an input from CSS.
- Inside the library: how a name finds its
<script data-effect>elements. - The whole page.
How it works
- The attribute wraps the element. A MutationObserver finds
[fx]and wraps the element in<fx-surface>, which slots it into a canvas the same size. Remove the attribute and it's unwrapped. - WebGPU first. When the page has a usable WebGPU device (a real adapter that passes a self-test) and the effect has WGSL source, the canvas is WebGPU:
queue.drawElementImageToTexture()uploads the DOM and one render pass draws it. Otherwise the surface uses WebGL, and the line under the cards says why.fx-backend="webgpu|gl"names a route by hand. A custom effect needs awgslstring beside itsglslto run on WebGPU; the editor above has both. WGSL that doesn't compile is the effect's error, not WebGPU's: the element is drawn unshaded and stays on WebGPU. - Direct backend: the canvas is WebGL and
texElementSubImage2D()uploads the DOM straight into a texture, only when the DOM repaints. It draws one quad with one fragment shader. - Shared backend: browsers allow about 16 WebGL contexts per page, so after the first eight surfaces the rest use a 2D canvas:
drawElementImage(), copy into one shared WebGL context, shade, draw back. That removes the limit on surfaces, at the cost of two extra copies per frame. - CSS sets the shader inputs.
--fx-progressand--fx-amountare registered as numbers, so they transition and animate. The surface samples the computed value each frame and redraws only when a uniform changed. - Input stays at the layout box. The element's hit-test geometry is its layout box. Displacement is visual only, like a CSS filter. With reduced motion on, time-driven effects hold still.
Live checks
| Result | Experiment | Detail |
|---|