2.0.5 • Published 1 year ago

blurhash v2.0.5

Weekly downloads
14,595
License
MIT
Repository
github
Last release
1 year ago

blurhash

NPM Version NPM Downloads

JavaScript encoder and decoder for the Wolt BlurHash algorithm

Install

npm install --save blurhash

See react-blurhash to use blurhash with React.

API

decode(blurhash: string, width: number, height: number, punch?: number) => Uint8ClampedArray

Decodes a blurhash string to pixels

Example

import { decode } from "blurhash";

const pixels = decode("LEHV6nWB2yk8pyo0adR*.7kCMdnj", 32, 32);

const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const imageData = ctx.createImageData(width, height);
imageData.data.set(pixels);
ctx.putImageData(imageData, 0, 0);
document.body.append(canvas);

encode(pixels: Uint8ClampedArray, width: number, height: number, componentX: number, componentY: number) => string

Encodes pixels to a blurhash string

import { encode } from "blurhash";

const loadImage = async src =>
  new Promise((resolve, reject) => {
    const img = new Image();
    img.onload = () => resolve(img);
    img.onerror = (...args) => reject(args);
    img.src = src;
  });

const getImageData = image => {
  const canvas = document.createElement("canvas");
  canvas.width = image.width;
  canvas.height = image.height;
  const context = canvas.getContext("2d");
  context.drawImage(image, 0, 0);
  return context.getImageData(0, 0, image.width, image.height);
};

const encodeImageToBlurhash = async imageUrl => {
  const image = await loadImage(imageUrl);
  const imageData = getImageData(image);
  return encode(imageData.data, imageData.width, imageData.height, 4, 4);
};

isBlurhashValid(blurhash: string) => { result: boolean; errorReason?: string }

import { isBlurhashValid } from "blurhash";

const validRes = isBlurhashValid("LEHV6nWB2yk8pyo0adR*.7kCMdnj");
// { result: true }

const invalidRes = isBlurhashValid("???");
// { result: false, errorReason: "The blurhash string must be at least 6 characters" }
jam3-react-sdkmrkreszintraactive-sdk-ui@everything-registry/sub-chunk-1248simplitime-communicationpwa-sharchat@robertlong/matrix-react-sdksimple-image-cloud-frontsharp-watch@preetjdp/gatsby-transformer-blurhash@prismx/crispstimulus-blurhash@plaiceholder/blurhashstoreberry-ui-componentsstoreberry-ui-components-betastoreberry-ui-components-test-onereact-cloudimage-blurhashreact-uvcprobe-plaiceholdersqip-plugin-blurhashssb-blobs-blurhash@sentrei/ui@showtime-xyz/universal.image@tillen/fresco@twk-ui-lib/react@pidong/mx-puppet-bridge@pinpt/react@patrik_hajek/admin@protonapp/proton-runnersvdocsvelte-imagetalktsy-matrix-react-sdkstrapi-blurhashstrapi-blurhash-plugin@tmpc/ui@tmpc/ui-nextjssvelte-datocmsember-responsive-imageember-blurhashmatrix-react-sdkjs-cloudimage-responsiveblitz-libsnext-blurnext-blur-imagenext-blurhashnext-blurhash-blurdataurlnext-blurhash-previews@exakt/ui@fiction/core@fiction/uingx-blurhash-rendermagic-img@ember-responsive-image/blurhashmx-puppet-bridgegl-react-blurhashnext-placeholder@factor/api@factor/uinuxt-blurhashpayload-blurhash-plugin@flmngr/flmngr-server-nodenitropageop-headless-libangular-blurhashupload-img-compuse-next-blurhashanother-vue3-blurhashadonis-responsive-attachment@volverjs/ui-vuetwype-sdk@adeira/sx-design@afetcan/api@afetcan/server@alt-digital/blurhash-webpack-plugin@annatarhe/blurhash-reactbasic-react-ui-kitvue3-blurhashbh-clityr-imagevite-plugin-blurhashvite-plugin-blurhash-sharp-fix-fork@aitech-asia/cms@uplo/analyzer-image-blurhash@zapal/payload-blurhash-plugin@unpic/astro@unpic/placeholder@varld/sparkles@gvrs/gatsby-transformer-blurhash@hdlegend/design-system@glenlowland/jellyfin-web@googleforcreators/story-editor@dative-gpi/foundation-shared-components@brendan-holmes/image-griddirectus-extension-blurhashdirectus-extension-image-blurhashdatocms-plugin-unsplash-asset-source@caisy/league@ckeditor/ckeditor5-ckbox@citydna/experience@ckbox/components
2.0.5

1 year ago

2.0.3

2 years ago

2.0.4

1 year ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.5

2 years ago

1.1.4

3 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.0.1

6 years ago