Lesson 44 of 50
cssCSS Houdini – The Paint API
10 mins
Course progress88%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 44 of 50
css10 mins
Keep these ideas in mind as you work through the lesson.
Houdini opens the browser’s rendering engine to developers, allowing you to write custom CSS renderers in JavaScript.
/* register the worklet */
if ('paintWorklet' in CSS) {
CSS.paintWorklet.addModule('bg-paint.js');
}
bg-paint.js
registerPaint('bg', class {
static get inputProperties() { return ['--color']; }
paint(ctx, size, props) {
ctx.fillStyle = props.get('--color').toString();
ctx.fillRect(0, 0, size.width, size.height);
}
});
.box {
--color:#ff6600;
background-image:paint(bg);
width:200px; height:200px;
}
The box now has a programmatically generated background that can react to CSS variables.
.box {
background:#ff6600; /* fallback for unsupported browsers */
background-image:paint(bg); /* Houdini paint */
}
@property to animate a custom property.@property --angle {
syntax:'<angle>'; inherits:true; initial-value:0deg;
}
.box {
--angle:45deg;
background-image:paint(gradient);
transition:--angle 2s linear;
}
--angle and let the paint worklet draw a rotating gradient.Since Houdini worklets only affect visuals, they do not impact screen readers. Ensure you still provide a fallback colour for high‑contrast mode.