Skip to content
Back to Concepts
CSS UniverseTrack 9: Houdini APIsPaint API Worklet
Houdini API Trilogy · Concept 39Deep Engine Hook

CSS Houdini Paint API: Custom Paint Worklets

Stop waiting years for browser makers to implement new CSS background styles. With the Houdini Paint API, you can write JavaScript Worklets that hook directly into the browser's native rasterization pipeline to draw custom 2D canvas backgrounds via background: paint(myWorklet)!

1. Mental Model: Why Name It "Houdini"?

Harry Houdini was the world's greatest escape artist. For decades, web developers were locked in a cage: if CSS didn't have a feature (like smooth squircle corners or rippling water backgrounds), developers had to wait 5 to 10 years for the W3C committee and browser teams to implement it. Houdini "escapes the cage" by letting you write code that executes directly inside the browser's C++ render engine!

Regular Canvas vs Paint Worklet

An HTML <canvas> element blocks the main JavaScript thread and needs DOM positioning. A Houdini Paint Worklet runs off the main thread in a background worker directly during the browser's Paint phase!

Responsive to CSS Variables

You feed input CSS variables (--bubble-color: #8b5cf6) to the worklet. When the variable transitions, the worklet re-executes automatically!

2. The 3-Step Houdini Paint Architecture

/* 1. In my-paint-worklet.js */
registerPaint('circlePattern', class {
static get inputProperties() { return ['--bubble-color']; }
paint(ctx, geometry, properties) {
const color = properties.get('--bubble-color').toString();
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(geometry.width / 2, geometry.height / 2, 30, 0, 2 * Math.PI);
ctx.fill();
}
});
/* 2. In Main JavaScript */
CSS.paintWorklet.addModule('my-paint-worklet.js');
/* 3. In Pure CSS */
.cool-card {
--bubble-color: #8b5cf6;
background: paint(circlePattern);
}

3. Interactive Studio: Live Houdini Paint Worklet Simulator

#8b5cf6
--bubble-radius30px
CSS Paint Invocation
--bubble-color: #8b5cf6;
--bubble-radius: 30px;
background: paint(circlePattern);
Houdini Paint Worklet

Procedural CSS Canvas

Vector graphics rendered directly by the browser paint stage without image assets.

First-Year Student Traps: Houdini Paint API

  • 1.Zero DOM or Storage Access in Worklets: A Paint Worklet runs in a sandboxed, multi-threaded environment. You cannot access window, document, or localStorage inside the worklet script! Everything must be passed through CSS custom properties.
  • 2.Safari Progressive Enhancement: Chromium browsers support Paint Worklets fully, but Safari support is still experimental. Always provide a fallback background: background: #8b5cf6; background: paint(circlePattern);.
Next Masterclass · Houdini Part 2

CSS Houdini Typed OM: Type-Safe Styles

Eliminate string concatenation bugs and performance parsing overhead with CSS.px() and attributeStyleMap.

Continue to Concept 40