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

preact-blurhash-as

v0.10.1

Published

Preact bindings for blurhash-as

Downloads

7

Readme

preact-blurhash-as

Preact bindings for blurhash-as

NPM JavaScript Style Guide Open in CodeSandbox

Install

npm install --save blurhash-as preact-blurhash-as
yarn add blurhash-as preact-blurhash-as

Usage

Setup

Since the decoding process runs on the client, Blurhash components uses the blurhash-as/browser. As a peer dependency, you must call the blurhash.setURL to define the WASM source. This must be called before any usage of Blurhash.

import * as blurhash from 'blurhash-as/browser';

// Setup WASM URL, must be called as early as possible
blurhash.setURL(blurhashWASMURL);

Image with Blurhash Placeholder

Blurhash provides a way to lazily render images and at the same time, render the placeholders before the images.

import { Blurhash } from 'preact-blurhash-as';

// Render a lazy image w/ a Blurhash
<Blurhash
  mode="css" // 'svg', 'canvas' or 'image';
  src={imageSrc}
  alt="This is an image"
  hash={blurhash}
  width={imageWidth}
  height={imageHeight}
  punch={punchValue} // Optional

  // For "css" and "svg" modes, you can provide
  // filter and blur values
  filter={filterValue} // Defaults to 20px
  scale={scaleValue} // Defaults to 1.2

  // For "image" mode, you can provide format
  // and quality values
  format="image/png" // "image/jpeg" or "image/webp"
  quality={0.9} // Implementation-wise, defaults to 0.92

  onLoad={onLoad}
/>

Blurhash Placeholder only

In some circumstances, we want to handle the placeholder logic alone, BlurhashPlaceholder renders only the placeholder and not the source image.

import { BlurhashPlaceholder } from 'preact-blurhash-as';

// Take note that placeholders are responsive and attempts
// to fill the width and height. Aspect Ratio boxes
// work great here.
<div style={{ width: 100%, height: 100% }}>
  <BlurhashPlaceholder
    visible={showPlaceholder}
    mode="css" // 'svg', 'canvas' or 'image';
    hash={blurhash}
    width={imageWidth}
    height={height}
    punch={punchValue} // Optional

    // For "css" and "svg" modes, you can provide
    // filter and blur values
    filter={filterValue} // Defaults to 20px
    scale={scaleValue} // Defaults to 1.2

    // For "image" mode, you can provide format
    // and quality values
    format="image/png" // "image/jpeg" or "image/webp"
    quality={0.9} // Implementation-wise, defaults to 0.92

    onLoad={onLoad}
  />
</div>

Blurhash Container w/ Custom Placeholder

If you want to use the lazy-loading mechanism of Blurhash while providing your own placeholder, you can use BlurhashContainer.

import { BlurhashContainer } from 'preact-blurhash-as';

<BlurhashContainer
  src={imageSrc}
  alt="This is an image"
  width={imageWidth}
  height={imageHeight}
>
  {

    (visible, showPlaceholder) => (
      <CustomPlaceholder
        // visible controls whether or not the
        // placeholder should render
        visible={visible}
        // showPlaceholder notifies the image to
        // begin rendering after the placeholder
        // has successfully rendered.
        showPlaceholder={showPlaceholder}
      />
    )
  }
</BlurhashContainer>

Static Rendering

Use BlurhashStatic to handle the pre-rendered placeholders, in contrast with Blurhash which renders the placeholders. This is useful in combination with plugins that renders these placeholders during build-time or for SSR.

Compared to BlurhashStatic, the placeholders are not lazily-rendered.

import { BlurhashStatic } from 'preact-blurhash-as';

<BlurhashStatic
  mode="css" // 'svg' or 'image';
  src={imageSrc}
  alt="This is an image"
  placeholder={cssPlaceholder}
  width={imageWidth}
  height={imageHeight}
/>

Static Placeholder

import { BlurhashStaticPlaceholder } from 'preact-blurhash-as';

<BlurhashStaticPlaceholder
  visible
  mode="css" // "svg" or "image"
  placeholder={placeholder}
  width={width}
  height={height}
/>

Static Container

Similar to BlurhashContainer however, the placeholder is not lazily-rendered.

Styling

preact-blurhash-as provides the following class names:

  • blurhash-as__aspect-ratio-box: The outer container of the Blurhash component, this retains the aspect ratio.
  • blurhash-as__aspect-ratio-content: The inner container of the Blurhash component.
  • blurhash-as__image-container: This allows the image and the placeholder to overlap.
  • blurhash-as__image: The image.
  • blurhash-as__placeholder: The placeholder

After the image has loaded, the image and the placeholder changes opacity. This is useful if you want to provide transitions when the image is displayed.

Sponsors

Sponsors

License

MIT © lxsmnsyc