interface
Paintworkletglobalscope — CSS reference
The specialized global scope for CSS paint modules exposes registerPaint. A registered class paints into a provided rendering context using the requested size and declared inputs. It does not access the document DOM. The accompanying module uses inputProperties and a solid rectangle.
Formal Syntax & Structure
css syntaxcss
registerPaint("bm-tile", class { paint(ctx, size) { /* draw */ } })Working Example
Example Snippetcss
<!doctype html><html lang="en"><meta charset="utf-8"><title>PaintWorkletGlobalScope</title><style>body{margin:8px;font:16px/20px sans-serif;color:black;background:white}#a{background-color:rgb(0,128,128);width:160px;height:80px;--tile-color:#2563eb}</style><body><div id="a">Painted sample</div><button id="run" type="button">Try paint worklet</button><output id="result" aria-live="polite"></output><script>const target=document.getElementById('a');const output=document.getElementById('result');async function demo(){if(!globalThis.CSS?.paintWorklet){output.textContent='Fallback: paint worklet unavailable';return getComputedStyle(target).backgroundColor;}const url=URL.createObjectURL(new Blob(["registerPaint(\"bm-tile\", class {\n static get inputProperties() { return [\"--tile-color\"]; }\n paint(ctx, size, properties) {\n ctx.fillStyle = properties.get(\"--tile-color\").toString().trim() || \"#008080\";\n ctx.fillRect(0, 0, size.width, size.height);\n }\n});"],{type:'text/javascript'}));try{await CSS.paintWorklet.addModule(url);target.style.backgroundImage='paint(bm-tile)';output.textContent='Module registered';return getComputedStyle(target).backgroundImage;}catch(error){output.textContent='Fallback: module could not load';return getComputedStyle(target).backgroundColor;}finally{URL.revokeObjectURL(url);}}document.getElementById('run').onclick=demo;</script></body></html>