The CSS Paint API, also known as the Paint API or CSS Painting API, is a powerful new web API that allows developers to generate images directly within CSS properties. This opens up a world of possibilities for dynamic, performant, and visually rich styling that was previously only achievable with JavaScript manipulation or complex SVG/canvas techniques.
At its core, the Paint API lets you define custom drawing functions that can be used as values for CSS properties like background-image, border-image, or even custom properties. This leverages the browser's rendering engine for high-performance graphics, freeing up the main thread for more critical tasks.
The Core Concepts: Paint Worklets
The primary mechanism for using the Paint API is through Paint Worklets. These are small JavaScript modules that run in a separate thread, isolated from the main page's JavaScript. This isolation ensures that the painting process doesn't block the UI and that it can be efficiently repainted as needed.
Registering a Paint Worklet
To use a Paint Worklet, you first need to register it with the browser. This is done using CSS.paintWorklet.addModule(), passing the URL to your JavaScript file.
if (CSS.paintWorklet) {
CSS.paintWorklet.addModule('path/to/your/paint-worklet.js');
} else {
console.warn('CSS Paint API is not supported in this browser.');
}
Defining a Paint Worklet
A Paint Worklet is a JavaScript class that must define two key static methods:
inputProperties: A static getter that returns an array of CSS custom property names the worklet depends on. These properties will be passed to the worklet's paint function.paint(ctx, geom, properties, args): The main drawing function.ctx: The Canvas 2D rendering context.geom: An object containing geometry information, such as the paintable element's size.properties: An object containing the values of the CSS custom properties defined ininputProperties.args: An optional argument for additional data.
Here's a simple example of a Paint Worklet that draws a gradient:
// path/to/your/paint-worklet.js
class GradientPaint {
static get inputProperties() {
return ['--gradient-color-stop1', '--gradient-color-stop2'];
}
paint(ctx, geom, properties) {
const color1 = properties.get('--gradient-color-stop1');
const color2 = properties.get('--gradient-color-stop2');
const gradient = ctx.createLinearGradient(0, 0, geom.width, 0);
gradient.addColorStop(0, color1);
gradient.addColorStop(1, color2);
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, geom.width, geom.height);
}
}
registerPaint('my-gradient', GradientPaint);
Using Paint Worklets in CSS
Once registered, you can use your custom paint worklet like any other CSS value. You'll typically set the custom properties that your worklet expects.
.my-element {
--gradient-color-stop1: #ff7e5f;
--gradient-color-stop2: #feb47b;
background-image: paint(my-gradient);
width: 300px;
height: 150px;
border-radius: 8px;
}
Live Example: Gradient Background
This element uses the my-gradient paint worklet defined above.
Beyond Simple Gradients: Advanced Use Cases
The Paint API's true power lies in its ability to draw complex patterns, intricate textures, and even performative animations without direct JavaScript DOM manipulation.
1. Pattern Generation
You can create repeatable patterns, like dots, stripes, or custom geometric shapes, to be used as backgrounds.
2. Custom Borders and Effects
Imagine custom dashed borders with varying stroke widths, or a glowing effect applied purely through CSS painting.
3. Animating Visuals
By updating custom properties that the Paint Worklet reads, you can trigger repaints and create smooth, performant animations directly within CSS. This is ideal for progress bars, loading indicators, or dynamic visualizations.
Live Example: Animated Dots
This demonstrates a simple animated pattern using the Paint API. Watch the dots move!
Rendering Animated Dots...
Browser Support and Considerations
The CSS Paint API is supported in modern browsers like Chrome, Firefox, and Edge. Safari has experimental support. Always include fallback mechanisms or check for support using if (CSS.paintWorklet) { ... }.
Performance: While designed for performance, complex computations within your Paint Worklet can still impact rendering. Profile your worklets if you encounter performance issues.
Security: Paint Worklets run in a sandboxed environment, preventing direct access to the DOM or sensitive browser APIs.
Conclusion
The CSS Paint API is a groundbreaking addition to the web platform, offering developers unprecedented control over visual styling. By enabling the creation of dynamic, custom graphics directly within CSS, it pushes the boundaries of what's possible with web design, leading to more expressive, efficient, and visually stunning user interfaces. Dive in, experiment, and discover the endless creative potential of the Paint API!
Further Reading: