npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-blurhash-async

v0.0.17

Published

Blurhash Web Worker implementation for React

Downloads

481

Readme

react-blurhash-async

NPM Version NPM Downloads

🔥🔥 BLAZING FAST 🔥🔥 React components for using the blurhash algorithm in your React projects.

Fork of react-blurhash library running in WebWorker (not blocking JS thread). Check additional props: loading and imageRef.

Demo

Motivation

This library was created to optimise the blurhash generation process in React projects. It is mainly intended for projects where blurHash is just placeholder for images. By using react-blurhash-async, the cost of generating an SPA page with HUNDRED such placeholders is insignificant.

The library was written on 12 June 2022 A.D, inspired by Robert Kubica's drive in the Le Mans race.

How It Works

Page generation time is crucial. When the react-blurhash-async component is with the loading=lazy flag, the generation of the CPU-expensive blurhash is done in the WebWorker (when the browser has OffScreen Canvas support), or delayed by one tick (when the browser does not have OffScreen Canvas support) so as not to delay the rendering of the new page in SPA. This allows us to generate a page with even a hundred blurhashes, without significant performance degradation.

Alt text

In addition, because blurhash is a placeholder for image, a new property imageRef has been added that can conditionally force the component to stop generating blurhash when an image is already loaded. Using this property is more efficient than writing a conditional component that does the same thing (unmounting from the DOM tree is moderately efficient when rendering several hundred components).

Suggested example of use with imageRef optimization

const ImageWithBlurhash = ({ src, hash, loading }) => {
    const ref = useRef()
    return (
      <div style={relativeStyle}>
        <BlurHash imageRef={ref} hash={hash} loading={loading} style={absoluteStyleToFitParent} />
        <img ref={ref} src={src} loading={loading} style={absoluteStyleToFitParent} />
      </div> 
    )
}

Install

yarn add blurhash react-blurhash-async

Usage

<Blurhash />

import { Blurhash } from "react-blurhash-async"

Description

Blurhash component is the recommended way to render blurhashes in your React projects. It uses BlurhashCanvas and a wrapping div to scale the decoded image to your desired size. You may control the quality of the decoded image with resolutionX and resolutionY props.

Props

| name | description | | ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | hash (string) | The encoded blurhash string. | | width (int | string) | Width (CSS) of the decoded image. | | height (int | string) | Height (CSS) of the decoded image. | | resolutionX (int) | The X-axis resolution in which the decoded image will be rendered at. Recommended min. 32px. Large sizes (>128px) will greatly decrease rendering performance. (Default: 32) | | resolutionY (int) | The Y-axis resolution in which the decoded image will be rendered at. Recommended min. 32px. Large sizes (>128px) will greatly decrease rendering performance. (Default: 32) | | punch (int) | Controls the "punch" value (~contrast) of the blurhash decoding algorithm. (Default: 1) | | loading ('eager' | 'lazy') | Controls how blurhash is rendered. "eager" -> blocks the thread, and component will be rendered together with blurhash. "lazy" -> loads blurhash after render cycle. When browser supports Canvas OffScreen, blurhash is generated inside webworker. (Default: "lazy") | | imageRef (MutableRefObject<HTMLImageElement) | Additional optimisation parameter. Most blurHashes are placeholders generated for images. Giving to component a reference to <img /> allows it to optionally not generate blurhash when image is visible (cached in browser memory).. (Default: undefined) |

Example

<Blurhash hash="LEHV6nWB2yk8pyo0adR*.7kCMdnj" width={400} height={300} resolutionX={32} resolutionY={32} punch={1} />

<BlurhashCanvas />

import { BlurhashCanvas } from "react-blurhash-async"

Description

BlurhashCanvas is the barebones implementation of a blurhash string to a canvas. You may want to use it instead of the Blurhash component e.g. if you want to control the scaling yourself.

Props

| name | description | | --------------- | --------------------------------------------------------------------------------------- | | hash (string) | The encoded blurhash string. | | width (int) | Width of the decoded image. | | height (int) | Height of the decoded image. | | punch (int) | Controls the "punch" value (~contrast) of the blurhash decoding algorithm. (Default: 1) | | loading ('eager' | 'lazy') | Controls how blurhash is rendered. "eager" -> blocks the thread, and component will be rendered together with blurhash. "lazy" -> loads blurhash after render cycle. When browser supports Canvas OffScreen, blurhash is generated inside webworker. (Default: "lazy") | | imageRef (MutableRefObject<HTMLImageElement) | Additional optimisation parameter. Most blurHashes are placeholders generated for images. Giving to component a reference to <img /> allows it to optionally not generate blurhash when image is visible (cached in browser memory).. (Default: undefined) |

Example

<BlurhashCanvas hash="LEHV6nWB2yk8pyo0adR*.7kCMdnj" width={400} height={300} punch={1} />

Browser support

  • Blurhash depends on Uint8ClampedArray, which is supported on all mainstream browsers and >=IE11.
  • Optional OffScreenCanvas : https://caniuse.com/offscreencanvas
  • Optional Web Worker: https://caniuse.com/webworkers

Credits (real authors of this library)