🔧 How to create your own Paint and use it using javascript and CSS (Houdini API)?
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
You can create your own Paint and use it in css in the following way:
TypeScript
/* checkboardWorklet.ts */
import {
DOMString,
PaintFunction,
PaintGeometry,
createPaint
} from "houdini-toolkit";
const paintName: DOMString = "checkerboard";
const paintFunction: PaintFunction = (
ctx: CanvasRenderingContext2D,
geom: PaintGeometry,
properties: StylePropertyMapReadOnly
) => {
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();
}
}
};
createPaint(paintName, paintFunction);
Javascript
/* checkboardWorklet.js */
import { createPaint } from "houdini-toolkit";
createPaint("checkerboard", (ctx, geom, properties) => {
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();
}
}
});
HTML
<script>
CSS.paintWorklet.addModule("checkboardWorklet.js");
</script>
CSS
li {
background-image: paint(checkerboard);
}
In this example, paint is registered in the paintWorklet in the CSS object. Paint API (registerPaint, PaintWorkletGlobalScope, devicePixelRatio).
Links to examples in Github - https://github.com/houdini-toolkit/examples/tree/main/paint
...
🔧 The Future of CSS: CSS Houdini
📈 41.26 Punkte
🔧 Programmierung
🔧 State of CSS Houdini
📈 32.84 Punkte
🔧 Programmierung
🔧 How to Be #Extra with CSS Houdini
📈 32.84 Punkte
🔧 Programmierung
🔧 How to Be #Extra with CSS Houdini
📈 32.84 Punkte
🔧 Programmierung
🎥 Extending CSS with Houdini
📈 32.84 Punkte
🎥 Video | Youtube
🔧 Extending CSS with Houdini
📈 32.84 Punkte
🔧 Programmierung
🔧 A Tiny Peep into CSS Houdini
📈 32.84 Punkte
🔧 Programmierung
🔧 CSS Houdini
📈 32.84 Punkte
🔧 Programmierung
🔧 CSS Paint API
📈 27.35 Punkte
🔧 Programmierung