یه API قدرتمند که بهت اجازه میده مستقیماً با CSS نقاشی بکشی! 🎨
۵ مرحلهای که تصاویر نشون میدن:
۱️⃣ ساخت Paint Worklet
class DotsPainter {
paint(ctx, geom, props) {
ctx.fillStyle = 'pink';
for (let y = 0; y < geom.height; y += 20) {
for (let x = 0; x < geom.width; x += 20) {
ctx.beginPath();
ctx.arc(x, y, 5, 0, 2 * Math.PI);
ctx.fill();
}
}
}
}
registerPaint('dots', DotsPainter)یه کلاس جاوااسکریپت مینویسی که روی canvas رسم میکنه.
۲️⃣ ثبت Worklet
CSS.paintWorklet.addModule('paint.js');فایل جاوااسکریپت رو به مرورگر معرفی میکنی.
۳️⃣ استفاده در CSS
.box {
background: paint(dots);
}حالا میتونی مثل
background معمولی استفاده کنی!۴️⃣ شخصیسازی با متغیرها
ctx.fillStyle = props.get('--dot-color') || 'pink';.box {
background: paint(dots);
--dot-color: blue;
}رنگ رو از CSS تغییر بده!
۵️⃣ نتیجه
• ✅ بدون عکس
• ✅ بدون پلاگین
• ✅ فقط CSS خالص
• ✅ روی compositor thread اجرا میشه (سریع)
💎 @Htmlcss_channels | #HTML #css #JavaScript