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.

Show the code fx="…" on any element
  1. Add fx="name" to an element. Three of the cards above. Nothing else about them changes.
  2. Drive the shader from CSS. --fx-progress is a registered custom property, so a transition animates the shader uniform.
  3. Load the library; set a variable from script if you want. $ is getElementById.
  4. 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.
  5. Inside the library: the element becomes a WebGL texture. gl.texElementSubImage2D(…, element), into storage of exactly the element's image size.
  6. Inside the library: draw one quad through the effect's fragment shader.
  7. Inside the library: keep clicks on the element.
  8. 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
  1. Use the name in fx.
  2. Register the function under that name, in GLSL and in WGSL. glsl.value and wgsl.value are the text in the editors: a vec4 fx(vec2 p) and a fn fx(p: vec2f) -> vec4f that sample the element with tex(p). Leave wgsl out and the element is drawn with WebGL. Calling defineEffect again replaces it.
  3. 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.

  1. Write the function. The GLSL signature is vec4 fx(vec2 p) and the WGSL signature is fn fx(p: vec2f) -> vec4f. p is 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.
  2. 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>
  3. Or declare it in markup, with no script of your own. Use one <script> element per language, each with data-effect="name". The type must be one the browser doesn't run; a type containing wgsl is 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>, and fx="url(wobble.frag)" or fx="url(wobble.wgsl)" fetches a file. Both forms hold one language only.
  4. Use the name, and set the inputs from CSS. The four custom properties are registered as numbers, so transitions, animations and :hover change 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.

GLSLWGSLValue
ppPosition 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_sizeu.sizeElement width and height in CSS px.
u_timeu.timeSeconds since the library loaded. Stays 0 when the visitor asks for reduced motion.
u_progressu.progress--fx-progress on the element. Default 0.
u_amountu.amount--fx-amount. Default 1.
u_xyu.xy--fx-x and --fx-y. Default 0, 0.
u_pointeru.pointerPointer position in the same px as p. (-10000, -10000) when no pointer is over the element.
u_hoveru.hover0 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 sameValue noise helpers that return 0 to 1.

Options

OptionTypeMeaning
glslstringThe WebGL version of the function.
wgslstringThe WebGPU version of the function.
animatedbooleanRedraw 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.
bleednumberCSS 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.
geometryfunction({ 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

Show the code a page that uses only /lib/fx.js
  1. Load the one file.
  2. Declare an effect in markup, in both languages.
  3. Define an effect from script, in both languages.
  4. Put the names in fx.
  5. Drive an input from CSS.
  6. Inside the library: how a name finds its <script data-effect> elements.
  7. The whole page.

How it works

Live checks

ResultExperimentDetail