Skip to main content
Home

Built and signed on GitHub Actions

Black and White dithering for ImageData (e.g. HTML's canvas element)

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
a week ago (0.0.3)

canvas-dither

Black and White dithering for the canvas element

Differences of this fork

This is a fork of NielsLeenheer/CanvasDither which is published on NPM as canvas-dither.

  • Developed in TypeScript instead of JavaScript
  • No longer returns the ImageData, because the input parameter is mutated. Returning the same object gave me the false assumption that the functions would creat a copy of the ImageData.
  • Exports individual functions instead of a class object to allow for tree shaking.
  • Fixed typo in JSDocs: "blank and white" became "black and white"
  • Added some basic unit tests
  • Published on JSR.io instead of npm
  • Developed using Deno instead of NodeJs
  • Changed the default branch from "master" to "main"

Usage

First, install the package using npm:

npx jsr add @jaller94/canvas-dither

For other package managers, read the instructions on https://jsr.io/@jaller94/canvas-dither.

Then, require the package and use it like so:

import * as Dither from "@jaller94/canvas-dither";

// Assume we have an existing canvas element with a 2D context
// Retrieve the image data of the canvas
let image = context.getImageData(0, 0, canvas.width, canvas.height);

// Dither the data using the Atkinson algoritm
Dither.atkinson(image);

// Place the image data back on the canvas
context.putImageData(image, 0, 0);

This package contains the following algorithms:

Threshold

A simple threshold which will make all pixels with a luminance over the threshold white and under the threshold black.

Dither.threshold(imageData, threshold);

Bayer

Using a Bayer matrix the image is converted to black and white with a cross-hatch pattern.

Dither.bayer(imageData, threshold);

Floyd-Steinberg

Altough there is nothing random about this algorithm, the results looks like a random scattering of dots, making especially photos seem very natural.

Dither.floydsteinberg(imageData);

Bill Atkinson

An improved version of the Floyd-Steinberg algorithm created by Bill Atkinson of MacPaint fame. This algorithm creates less noise in almost white backgrounds compared to Floyd-Steinberg, but has more contrast as a result.

Dither.atkinson(imageData);

License

MIT

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@jaller94/canvas-dither

Import symbol

import * as canvas_dither from "@jaller94/canvas-dither";
or

Import directly with a jsr specifier

import * as canvas_dither from "jsr:@jaller94/canvas-dither";

Add Package

pnpm i jsr:@jaller94/canvas-dither
or (using pnpm 10.8 or older)
pnpm dlx jsr add @jaller94/canvas-dither

Import symbol

import * as canvas_dither from "@jaller94/canvas-dither";

Add Package

yarn add jsr:@jaller94/canvas-dither
or (using Yarn 4.8 or older)
yarn dlx jsr add @jaller94/canvas-dither

Import symbol

import * as canvas_dither from "@jaller94/canvas-dither";

Add Package

vlt install jsr:@jaller94/canvas-dither

Import symbol

import * as canvas_dither from "@jaller94/canvas-dither";

Add Package

npx jsr add @jaller94/canvas-dither

Import symbol

import * as canvas_dither from "@jaller94/canvas-dither";

Add Package

bunx jsr add @jaller94/canvas-dither

Import symbol

import * as canvas_dither from "@jaller94/canvas-dither";