
ittybitty
Grain
Static film grain that adds tooth to flat fills and hides gradient banding.
This browser cannot run WebGL2, so this is a still image. The code below works wherever WebGL2 is available.
Code
The usage always matches the preview, layers and performance settings included. No build step or package.
Copy embed gives one block to paste into any page or site builder, no setup. For your own code: add grain.js, ittybitty-shader.js, runtime.js next to this page. Highlighted values follow the controls.
<!-- Quickest: one element. Size it with CSS; it fills its box. -->
<script type="module" src="./ittybitty-shader.js"></script>
<ittybitty-shader effect="grain" style="position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none"></ittybitty-shader>
<!-- Or with JavaScript, for full control: -->
<div style="position: relative">
<!-- your content -->
<canvas id="grain" aria-hidden="true" style="position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none"></canvas>
</div>
<script type="module">
import { grain } from './grain.js';
const canvas = document.getElementById('grain');
const effect = grain(canvas, {
type: 'film',
amount: 0.35,
size: 1.2,
colour: false,
blend: 'soft-light',
// Performance
dpr: 2,
adaptive: true,
});
// Later: effect.set(…), effect.pause(), effect.resume(), effect.destroy()
</script>
Details
A transparent canvas of fine, soft grain that blends onto whatever sits beneath it. It draws once and again only when you change it, so it costs nothing while the page is open.
- Payload
- 4.3 KB brotli for grain.js and ittybitty-shader.js and runtime.js, measured at build time. See the method.
- Resolution
- Device pixel ratio capped at 2, so grain stays crisp. Size is in CSS pixels at any density.
- Frame rate
- None. One frame on mount and one per change.
- When unseen
- Stops rendering when the canvas leaves the viewport or the tab is hidden, and resumes where it left off.
- Reduced motion
- Nothing to change: grain never moves.
- Layers
- Up to five effects fuse into one shader and one draw call. Filters such as dithering and halftone process the layers beneath them.
- Browsers
- Any browser with WebGL2: Chrome and Edge 56+, Firefox 51+, Safari 15+.
- Licence
- MIT. Copy it, change it, ship it.