Shader uniforms written in CSS only

Twelve cards are drawn by one WebGL canvas through one fragment shader. The shader's uniforms are CSS custom properties: uniform float uWave reads --u-wave from each card. Each card carries the CSS rule that sets its uniforms, in a <style> element that is displayed on the card. The rules use transitions, keyframes, typed attr(), sibling-index(), random(), if(), style queries, progress(), @function and a view timeline. No card has script of its own. Tap into a rule and change a number.

 

Show the code uniforms from custom properties, one read per frame
  1. The cards. Each holds its own rule in a displayed, editable <style> element.
  2. The keyframes and the function that the rules name.
  3. The fragment shader. Every float and vec4 uniform it declares becomes a custom property.
  4. The binding. Parse the declarations, register the properties, read computed values, set the uniforms.
  5. Once a frame. Read every card's values and ask for a paint only when one changed.
  6. On paint. Upload the cards whose picture changed and draw each with its own values.

How it works

lib/css-uniforms.js reads the shader source, finds each uniform float and uniform vec4, and registers a custom property for it with CSS.registerProperty(): a <number> for a float, a <color> for a vec4. A registered property has a type, so the browser can interpolate it in a transition, an animation or a scroll timeline. Each card is a drawable element with its own texture. For each card the page reads the computed values with getComputedStyle() and sets them as uniforms before drawing that card's quad.

A change to a custom property doesn't change the card's picture, so the browser raises no paint event for it. The page reads all values once per animation frame while the canvas is on screen, compares the strings with the previous frame, and calls requestPaint() only when one differs. The line under the canvas shows what that read costs.

The rules on the cards are live <style> elements with display: block and contenteditable. Editing one changes the page's style sheet, and the next frame's read picks up the new value.

Live checks

These run on their own fixtures in the corner of the window.

Findings