2.0.5 • Published 2 years ago

blurhash v2.0.5

Weekly downloads
14,595
License
MIT
Repository
github
Last release
2 years 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-sdkmrkresz@everything-registry/sub-chunk-1248simplitime-communicationtalktsy-matrix-react-sdksvelte-imagesvelte-datocmstest-matrix-react-sdkstoreberry-ui-componentsstoreberry-ui-components-betastoreberry-ui-components-test-onestrapi-blurhashstrapi-blurhash-pluginsvdocvite-plugin-blurhashvite-plugin-blurhash-sharp-fix-fork@cutting/cloudinary-blurhash@digitaltpm/adonis-responsive-attachment@caisy/leaguejellyfin-webjs-cloudimage-responsivel-previewsharp-watchsqip-plugin-blurhashssb-blobs-blurhashstimulus-blurhashtwype-sdktyr-imagetrioslib@annatarhe/blurhash-react@afetcan/api@afetcan/server@aitech-asia/cms@alt-digital/blurhash-webpack-plugin@brendan-holmes/image-grid@citydna/experience@ckeditor/ckeditor5-ckbox@ckbox/components@adeira/sx-designvue3-blurhashupload-img-compuse-next-blurhash@fiction/ui@fiction/core@gvrs/gatsby-transformer-blurhash@hdlegend/design-system@flmngr/flmngr-server-node@ember-responsive-image/blurhash@googleforcreators/story-editor@glenlowland/jellyfin-web@lucidcms/core@itrabbit/payload-blurhash-plugin@lightdotso/app@linefusion/directus-extension-blurhash-extractor@manualengineering/react-lazyblur@m5r/gatsby-transformer-blurhash@michieldemey/strapi-blurhash@mser/matrix-react-sdk@marketplaces.inc-ui/dependency-blurhash@massimo-cassandro/unsplash-page@neutrinodev/compiler@newcoin-foundation/iosdk@newstackdev/iosdk@mux/blurhash@nitropage/internals@exakt/ui@evoltdev/strapi-blurhash@factor/ui@factor/api@marketplace-co/react-application-element-image@iyio/convo-lang-chrome@iyio/convo-lang-react@iyio/mdx-ui-builder-react@iyio/media-common@iyio/any-comp@iyio/any-comp-cli@iyio/any-comp-test@iyio/nextjs-common@iyio/pdf-viewer@iyio/react-common@iyio/svg-charts-react@iyio/syn-taxi@iyio/vfs-react@james-camilleri/sanity-web-image@knowgistics/core@kitconcept/volto-image-block@octostar/platform-react@preetjdp/gatsby-transformer-blurhash@prismx/crisp@proca/widget@protonapp/proton-runner@open-lens/strapi-plugin-blurhash@patrik_hajek/admin@pidong/mx-puppet-bridge@npio/internals@plaiceholder/blurhash@pinpt/react@splinetool/react-spline@solu77/api@showtime-xyz/universal.image
2.0.5

2 years ago

2.0.3

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.5

3 years ago

1.1.4

4 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.0.1

7 years ago