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
- The cards. Each holds its own rule in a displayed, editable
<style>element. - The keyframes and the function that the rules name.
- The fragment shader. Every
floatandvec4uniform it declares becomes a custom property. - The binding. Parse the declarations, register the properties, read computed values, set the uniforms.
- Once a frame. Read every card's values and ask for a paint only when one changed.
- 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
- In Chromium 157 every CSS feature on the cards produced a number or a colour that
getComputedStyle()returns resolved: typedattr(),sibling-index(),random(),if()withstyle(), a style range query,progress(),@function, and values in the middle of a transition, an animation or a view timeline. - Changing only a custom property raises no
paintevent, also when the property is registered and animating. The canvas has to poll. Reading 12 properties on 12 cards, with animations running on all of them, took about 0.9 ms per frame in the test browser, which includes resolving the animated values. - A registered
<color>property in a transition is returned asrgb()orrgba()with interpolated components, so a colour uniform can be animated by CSS. random()keeps its value for an element across reads, so a seed doesn't flicker. It changes on reload.- A
<style>element withdisplay: blockinside a drawn element is part of its picture, can be edited there withcontenteditable, and its rules apply while it's edited. - A view timeline on a drawn card runs from the card's layout position in the page, so a card can form as it scrolls into the window.
- Not verified: Chrome 159 on my phone, and the cost of the per-frame read there. The status line reports it.