Brought to you by
Firstly, when using paint worklets, we highly recommend you also use the CSS Paint Polyfill to ensure that all modern browsers can run these styles as well. Read on for instructions on implementing both worklets and the polyfill in your user interfaces.
Note: Houdini worklets must either be run via a server locally, or on HTTPS in production. In order to work with a Houdini worklet, you will need to either install it locally or use a content delivery network (CDN) like unpkg to serve the files.
There are four steps required to use a Houdini paint worklet:
CSS.paintWorklet.addModule()
.The following goes through some of the various ways to achieve the above steps. You can also find demos and guidance on how to use the worklets in your CSS file on the worklet page.
Install your worklet from npm:
npm install <package-name>
Importing this package does not automatically inject the paint worklet. To install the worklet (step 2), you'll need to generate a URL that resolves to the package's worklet.js
, and register that. You do so with:
CSS.paintWorklet.addModule(..file-path/worklet.js)
When registering from unpkg, you can link directly to the worklet file without needing to locally install the worklet. Unpkg will resolve to the main script, or you can specify it yourself (i.e. worklet.js
). Unpkg will not cause CORS issues, as it is served over HTTPS.
CSS.paintWorklet.addModule("https://
unpkg.com/<package-name>");
Note that this does not register the custom properties for syntax and fallback values. Instead, they each have fallback values embedded into the worklet.
To optionally register the custom properties, include the properties.js
file as well.
<script src="https://unpkg.com/<package-name>/properties.js"></script>
Here is an example of how to use Houdini in modern bundlers:
import workletURL from "url:<package-name>/worklet.js"
CSS.paintWorklet.addModule(workletURL);
import workletURL from "<package-name>/worklet.js?url"
CSS.paintWorklet.addModule(workletURL);
import workletURL from "url-loader!<package-name>/worklet.js"
CSS.paintWorklet.addModule(workletURL);
Install rollup-plugin-off-main-thread plugin. Then:
import workletURL from "omt:<package-name>/worklet.js";
CSS.paintWorklet.addModule(workletURL);
Now you're ready to call the worklet by its name in your CSS file.
div {
background: paint(<paintName>);
}
Most of the worklets showcased on this site have optional styling parameters which can be custimzed via CSS custom properties. These are denoted with a --
. See the worklets page for more information on customizations and how to use the worklets in CSS.
div {
--customValue: <value>
background: paint(<paintName>);
}
As previously mentioned, when using the Paint worklet, you should also include the CSS Paint Polyfill.
<script src="css-paint-polyfill.js"></script>
// or with unpkg:
<script src="https://unpkg.com/css-paint-polyfill"></script>
With a bundler or ES modules:
import 'css-paint-polyfill';