class CheckerboardPainter {
paint(ctx, geom, properties) {
// The global object here is a PaintWorkletGlobalScope
// Methods and properties can be accessed directly
// as global features or prefixed using self
const dpr = self.devicePixelRatio;
// Use `ctx` as if it was a normal canvas
const colors = ["red", "green", "blue"];
const size = 32;
for (let y = 0; y < geom.height / size; y++) {
for (let x = 0; x < geom.width / size; x++) {
const color = colors[(x + y) % colors.length];
ctx.beginPath();
ctx.fillStyle = color;
ctx.rect(x * size, y * size, size, size);
ctx.fill();
}
}
}
}
// Register our class under a specific name
registerPaint("checkerboard", CheckerboardPainter);PaintWorkletGlobalScope / notebook
PaintWorkletGlobalScope
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
if ("paintWorklet" in CSS) {
CSS.paintWorklet.addModule("checkerboard.js");
}textarea {
background-image: url("checkerboard.png"); /* Fallback */
background-image: paint(checkerboard);
}@supports (background: paint(id)) {
textarea {
background-image: paint(checkerboard);
}
}