canvas-dither
Version:
Black and White dithering for the canvas element
1 lines • 2.06 kB
JavaScript
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).CanvasDither=t()}}(function(){return function(){return function t(e,a,n){function r(d,f){if(!a[d]){if(!e[d]){var i="function"==typeof require&&require;if(!f&&i)return i(d,!0);if(o)return o(d,!0);var l=new Error("Cannot find module '"+d+"'");throw l.code="MODULE_NOT_FOUND",l}var u=a[d]={exports:{}};e[d][0].call(u.exports,function(t){return r(e[d][1][t]||t)},u,u.exports,t,e,a,n)}return a[d].exports}for(var o="function"==typeof require&&require,d=0;d<n.length;d++)r(n[d]);return r}}()({1:[function(t,e,a){e.exports=new class{grayscale(t){for(let e=0;e<t.data.length;e+=4){const a=.299*t.data[e]+.587*t.data[e+1]+.114*t.data[e+2];t.data.fill(a,e,e+3)}return t}threshold(t,e){for(let a=0;a<t.data.length;a+=4){const n=.299*t.data[a]+.587*t.data[a+1]+.114*t.data[a+2]<e?0:255;t.data.fill(n,a,a+3)}return t}bayer(t,e){const a=[[15,135,45,165],[195,75,225,105],[60,180,30,150],[240,120,210,90]];for(let n=0;n<t.data.length;n+=4){const r=.299*t.data[n]+.587*t.data[n+1]+.114*t.data[n+2],o=n/4%t.width,d=Math.floor(n/4/t.width),f=Math.floor((r+a[o%4][d%4])/2)<e?0:255;t.data.fill(f,n,n+3)}return t}floydsteinberg(t){const e=t.width,a=new Uint8ClampedArray(t.width*t.height);for(let e=0,n=0;n<t.data.length;e++,n+=4)a[e]=.299*t.data[n]+.587*t.data[n+1]+.114*t.data[n+2];for(let n=0,r=0;r<t.data.length;n++,r+=4){const o=a[n]<129?0:255,d=Math.floor((a[n]-o)/16);t.data.fill(o,r,r+3),a[n+1]+=7*d,a[n+e-1]+=3*d,a[n+e]+=5*d,a[n+e+1]+=1*d}return t}atkinson(t){const e=t.width,a=new Uint8ClampedArray(t.width*t.height);for(let e=0,n=0;n<t.data.length;e++,n+=4)a[e]=.299*t.data[n]+.587*t.data[n+1]+.114*t.data[n+2];for(let n=0,r=0;r<t.data.length;n++,r+=4){const o=a[n]<129?0:255,d=Math.floor((a[n]-o)/8);t.data.fill(o,r,r+3),a[n+1]+=d,a[n+2]+=d,a[n+e-1]+=d,a[n+e]+=d,a[n+e+1]+=d,a[n+2*e]+=d}return t}}},{}]},{},[1])(1)});