The idiomatic way to use the CSS Paint API seems to be:
- Create a
xyz.js
file- Populate with a class containing a
paint(ctx, geom, properties)
function - Call
registerPaint
, passing the class as an argument
- Populate with a class containing a
- Call
CSS.paintWorklet.addModule('xyz.js')
from yourindex.html
- Apply the paint worklet in CSS like
background-image: paint(myPaintWorklet);
More details on that here: https://developers.google.com/web/updates/2018/01/paintapi
But having to load a separate .js file is a performance hit.
Is there a way to inline the PaintWorklet so that a separate .js file is not needed?
One way to do it is using Data URLs. Demo: (works for me atleast on chrome 73). Example taken from here
Another method would be to make a
Blob
and pass the blob URL to theaddModule
function. This looks less hackish. Demo: