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

stimulus-blurhash

v0.1.2

Published

Stimulus Controller adding a blurhash background

Downloads

105

Readme

stimulus-blurhash

A Hotwire Stimulus controller which uses a blurhash to set the element's background.

Use it for

  • nice previews while images are loading,
  • for padding to aspect ratios,
  • or just to create fancy backgrounds.

Getting Started

Using npm

npm i stimulus-blurhash @hotwired/stimulus

Using yarn

yarn add stimulus-blurhash @hotwired/stimulus

Example

The blur hash LGF... in this example is from https://blurha.sh/.

<div class="some-classes" data-controller="blurhash" data-blurhash-blurhash-value="LGF5?xYk^6#M@-5c,1J5@[or[Q6." data-blurhash-x-value="4" data-blurhash-y-value="3">
  <!-- any content here -->
</div>

becomes

The screenshot actually uses the classes m-10 rounded-xl shadow-xl w-3/4 h-3/4 from tailwindcss.

Comparison

You can think of data-controller="blurhash" data-blurhash-blurhash-value="..." as an alternative to style="background-image: url('/tiny-version-of-image.png');background-size: 100% 100%, except that it saves a request.

Or as style="background-image: url('data:image/png;base64, ...');background-size: 100% 100%", except that the blurhash is only ~1% of the size of an equivalent png. You can inspect the element in your browser because the data-url is exactly what this lib generates from the blurhash.

Documentation

Usage

Register the controller in your stimulus application:

import { application } from "./application";

import { BlurhashController } from "./blurhash_controller";
application.register("blurhash", BlurhashController);

and then use it with any element:

<div data-controller="blurhash" data-blurhash-blurhash-value="LGF5?xYk^6#M@-5c,1J5@[or[Q6.">
  <!-- any content here -->
</div>

Style it any way you want, only background-size and background-color are managed by this library.

<div class="w-96 h-72 flex justify-center items-center text-xl text-white m-10 rounded-xl shadow-xl bg-red-500 bg-blend-difference" data-controller="blurhash" data-blurhash-blurhash-value="LGF5?xYk^6#M@-5c,1J5@[or[Q6.">
  Hello world
</div>

It even works on the document body:

<body data-controller="blurhash" data-blurhash-blurhash-value="LGF5?xYk^6#M@-5c,1J5@[or[Q6.">...</body>

Parameters

  • blurhash: String, required
  • backgroundSize: String, default: "100% 100%"
  • punch: Number, default: 1, increase or decrease contrast/saturation. For a similar effect, you could also set a background-color and background-blend-mode.
  • x: Number, default: 32, number of pixel components vertically
  • y: Number, default: 32, number of pixel components horizontally

Getting the Blurhash for an image

Getting the blurhash for an image is outside the scope of this library. See blurhash for more information. Rails users may want to check out https://github.com/renchap/shrine-blurhash

Contributors ✨

Made with :heart: by @bnjmnbck

Acknowledgments

Thanks to @adrienpoly for creating Stimulus Use which served as inspiration and copy-paste-source for this repo.