Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

blurhash-esm

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

blurhash-esm

Fork of the Wolt BlurHash algorithm. Repackaged as ES Module.

  • 1.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

blurhash-esm

NPM Version NPM Downloads

Fork of the excellent Wolt BlurHash that exports ES Module. Because maintainers of the original haven't merged the PR since summer 2020.

Install

npm install --save blurhash-esm

API

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

Decodes a blurhash string to pixels

Example
import { decode } from 'blurhash-esm';

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-esm';

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-esm';

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

const invalidRes = isBlurhashValid('???');
// { result: false, errorReason: 'The blurhash string must be at least 6 characters' }

Keywords

FAQs

Package last updated on 08 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc