
ittybitty
Dither Image
Any image, redrawn in a few tones of ordered dither.
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 dither-image.js, ittybitty-shader.js, runtime.js, glyphs.js, texture.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="dither-image" style="width: 100%; aspect-ratio: 16 / 10; image-rendering: pixelated"></ittybitty-shader>
<!-- Or with JavaScript, for full control: -->
<canvas id="dither-image" aria-hidden="true" style="width: 100%; aspect-ratio: 16 / 10; display: block; image-rendering: pixelated"></canvas>
<script type="module">
import { ditherImage } from './dither-image.js';
const canvas = document.getElementById('dither-image');
const effect = ditherImage(canvas, {
showBackground: true,
image: '/examples/still-life.webp',
pixel: 2,
levels: 2,
contrast: 1.35,
brightness: 0.08,
pattern: 'bayer',
fit: 'cover',
palette: [[0, '#1d1c26'], [1, '#efe9dc']],
// Performance
dpr: 2,
adaptive: true,
});
// Later: effect.set(…), effect.pause(), effect.resume(), effect.destroy()
</script>
Details
Pixelates an image, quantises it to a few levels and dithers between them through your palette. It draws once per change and never animates, so a page full of dithered images costs nothing at rest. The example is our own raymarched still life.
- Payload
- 6.1 KB brotli for dither-image.js and ittybitty-shader.js and runtime.js and glyphs.js and texture.js, measured at build time. See the method.
- Resolution
- Device pixel ratio capped at 2, so cells stay square and sharp.
- Frame rate
- None. One frame 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: it 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.