diths
v0.1.0
Published
Pure TypeScript dithering library for browsers (vanilla & React), Bun, and Node — built with tsdown.
Maintainers
Readme
diths
Pure TypeScript dithering library with deterministic, pixel-exact output across browsers (vanilla & React), Bun, and Node. Zero runtime dependencies, lazy-loaded codecs, built with tsdown.
Install
npm install diths
# or
bun add dithsQuick Start
Vanilla Browser
import { dither, generate } from "diths";
// Dither an <img>, <canvas>, File/Blob, ImageData, or URL
const out = await dither(document.querySelector("img")!, "risograph");
// Draw directly onto a canvas (no PNG round trip)
out.drawTo(document.querySelector("canvas")!, { scale: 2 });
// Or export to PNG, GIF, SVG, WebP, JPEG, BMP, TIFF, Blob, or data URL
const blob = await out.toBlob("png");
const svg = await out.toSVG({ svgShape: "circle" });
// Generate procedural dither art with no input image
const orb = await generate("sphere", { preset: "mac-classic", canvasWidth: 640, canvasHeight: 640 });React (diths/react)
react is an optional peer dependency (>=18) — vanilla browser, Bun, and Node users never pull React.
import { DitherCanvas, DitherImage, useDither, useGenerate } from "diths/react";
// Direct canvas rendering (fast interactive updates)
export function PhotoPreview({ file }: { file: File }) {
return <DitherCanvas src={file} options={{ preset: "risograph", maxDim: 640 }} scale={2} />;
}
// Encoded <img> rendering with fallback
export function ProceduralBadge() {
return (
<DitherImage
pattern="sphere"
options={{ preset: "gameboy", canvasWidth: 240, canvasHeight: 240 }}
format="png"
alt="Game Boy sphere"
fallback={<span>Rendering…</span>}
/>
);
}
// Hooks for custom UIs (with automatic race-condition cancellation & stable options)
export function Inspector({ src }: { src: string }) {
const { result, loading, error } = useDither(src, { algorithm: "atkinson", palette: "bw" });
if (loading) return <p>Dithering…</p>;
if (error) return <p>{error.message}</p>;
return <p>Colors: {result?.palette.join(", ")}</p>;
}Bun & Node
import { animate, compare, diff, dither, inspect } from "diths";
import { writeFile } from "node:fs/promises";
// Dither a local file and save as PNG
const out = await dither("photo.jpg", { preset: "risograph", maxDim: 960 });
await writeFile("out.png", await out.toPNG());
// Render a seamless looping procedural GIF, APNG, or Animated WebP (smallest)
const loop = await animate("sphere", { preset: "mac-classic", canvasWidth: 320, canvasHeight: 320, frames: 24, fps: 16 });
await writeFile("loop.webp", await loop.toWebP());
await writeFile("loop.apng", await loop.toAPNG());
await writeFile("loop.gif", await loop.toGIF());
// Build a labeled contact sheet comparing presets
const sheet = await compare("photo.jpg", { presets: ["mac-classic", "risograph", "gameboy", "manga"] });
await writeFile("sheet.png", await sheet.toPNG());
// Diff two images and generate a highlight heatmap
const delta = await diff(before, after);
await writeFile("diff.png", await delta.highlight().toPNG());
// Analyze an image and get preset recommendations
const info = await inspect("photo.jpg");Web Worker (diths/worker)
Keep large images off the main thread in browsers:
// dither.worker.ts
import { serveWorker } from "diths/worker";
serveWorker();import { connectWorker } from "diths/worker";
const worker = connectWorker(new Worker(new URL("./dither.worker.ts", import.meta.url), { type: "module" }));
const out = await worker.dither(document.querySelector("img")!, "manga");
// Works with React too: <DitherCanvas src={file} options="manga" worker={worker} />Entry Points
| Import | Purpose |
| --- | --- |
| diths | Core library (dither, generate, animate, compare, diff, inspect, recipe, catalogs, codecs, worker helpers) |
| diths/react | React hooks (useDither, useGenerate) and components (<DitherCanvas />, <DitherImage />) |
| diths/worker | Web Worker bridge (connectWorker, serveWorker) |
What's Inside
- 24 Dithering Algorithms: Floyd-Steinberg, Atkinson, Jarvis-Judice-Ninke, Stucki, Burkes, Sierra, Sierra Two-Row, Sierra Lite, Ostromoukhov, Bayer 2×2 / 4×4 / 8×8 / 16×16, Halftone, Clustered Dot, Diagonal / Horizontal / Vertical Lines, Crosshatch, Blue Noise, Interleaved Gradient Noise (IGN), Riemersma, Random, Threshold.
- 29 Built-in Palettes + custom hex arrays + Oklab adaptive palette extraction (
autoPalette). - 16 Curated Presets:
mac-classic,risograph,gameboy,manga,woodcut,editorial-print,crt-amber,blueprint,halftone-zine,lo-fi-photo, and more. - 10 Procedural Generators:
linear-gradient,radial-gradient,mesh,sphere,waves,noise,rings,plasma,checkerboard,inflate. - Pure-TS Codecs (Lazy-Loaded): PNG (with optional Zopfli + grayscale optimization), APNG (encode & decode), GIF (still & animated), WebP (lossless still & animated VP8L encode & decode), JPEG, BMP, TIFF, and compact vector SVG.
