Coder Social home page Coder Social logo

dither's Introduction

about

3-bit Web Worker friendly implementation of common dithering algorithms, ordered and spatial.

setup

Load via script tag:

<!-- Just an IIFE namespaced `dither` -->
<script src="https://thewhodidthis.github.io/dither/dither.js"></script>

Source from an import map:

{
  "imports": {
    "@thewhodidthis/dither": "https://thewhodidthis.github.io/dither/main.js"
  }
}

Download from GitHub directly if using a package manager:

# Add to package.json
npm install thewhodidthis/dither

usage

Choose between ordered or spatial and initialize with a matrix array to get an ImageData processing function. For example:

import { spatial as bender } from "@thewhodidthis/dither"

const source = document.createElement("img")
const target = document.createElement("canvas").getContext("2d")

const canvas = Object.assign(target.canvas, { width: 180, height: 180 })

// The Floyd/Steinberg variety is default
const filter = bender()

source.addEventListener("load", () => {
  target.drawImage(source, 0, 0)

  const pixels = target.getImageData(0, 0, canvas.width, canvas.height)
  const result = filter(pixels)

  target.putImageData(result, 0, 0)

  document.body.appendChild(canvas)
})

source.setAttribute("crossOrigin", "anonymous")
source.setAttribute(
  "src",
  `//source.unsplash.com/random/${canvas.width}x${canvas.height}`,
)

see also

dither's People

Contributors

thewhodidthis avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.