Git Product home page Git Product logo

react-blurhash's Introduction

react-blurhash

NPM Version NPM Downloads

React components for using the blurhash algorithm in your React projects

Demo

Install

npm install --save blurhash react-blurhash

Usage

<Blurhash />

import { Blurhash } from "react-blurhash";

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)

Example

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

<BlurhashCanvas />

import { BlurhashCanvas } from "react-blurhash";

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)

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.

react-blurhash's People

Contributors

chentsulin avatar muescha avatar nygardk avatar thisen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-blurhash's Issues

Why canvas?

Why not just wrap the decoded UInt8ClampedArray straight into a Blob of the appropriate bitmap mime-type and URL.createObjectUrl(blob) (making sure to appropriately URL.revokeObjectUrl on hash change or unmount) to create an image URL for a standard img tag (or anywhere an image source can be used such as CSS background-url)? It allows you more style options in CSS over the hash, and it makes it easier to swap in a real URL later when things have "loaded" without changing so many DOM elements in the process. Canvas seems like overkill for rendering the BlurHash in most scenarios to me, but maybe I'm missing something?

Blobs should be supported everywhere that UInt8ClampedArray is, so it doesn't seem like a compatibility issue to me.

https://caniuse.com/#feat=bloburls

https://caniuse.com/#feat=blobbuilder

Unable to Compile with Next

I'm trying to use this library in a NextJs application that is also using Typescript. I'm using a very basic test that based on demos. Locally, I receive the following error:

error - ./node_modules/react-blurhash/es/BlurhashCanvas.js:37:0
Module not found: Can't resolve 'blurhash'
Did you mean './blurhash'?
Requests that should resolve in the current directory need to start with './'.

I was able to re-create with a very simple Codesandbox as well:

https://codesandbox.io/s/summer-hill-xsuch?file=/pages/index.js

Alternative for React-Native

Have you found an alternative approach for this, by instead of using a canvas to use something that works on React Native?

I haven't found a single Image library or documentation on how to create a raw pixel array (UInt8ClampedArray) into an Image (or anything that's somewhat helpful, like a base64 representation)

TypeError: Cannot read property 'PureComponent' of undefined When imported by jest test

disclaimer: I am not a frontend dev or js dev so I might be missing something basic...

I used Blurhash successfully in one of my component, but trying to import that component or Blurhash component in my .tsx test file gives me an error.

I run

yarn jest --watch

my test code is

import * as React from "react";
import { Blurhash } from "react-blurhash";
...

Test output is like

โ— Test suite failed to run

    TypeError: Cannot read property 'PureComponent' of undefined

      1 | import * as React from "react";
    > 2 | import { Blurhash } from "react-blurhash";
        | ^
      3 | import { BlurhashCanvas } from "react-blurhash";
      4 | import { shallow } from 'enzyme';
      5 | import { cleanup, fireEvent, render } from "@testing-library/react";

      at Object.<anonymous> (node_modules/react-blurhash/src/BlurhashCanvas.tsx:11:51)
      at Object.<anonymous> (node_modules/react-blurhash/src/Blurhash.tsx:3:1)
      at Object.<anonymous> (node_modules/react-blurhash/src/index.ts:1:1)
      at Object.<anonymous> (src/__tests__/components/mytest.test.tsx:2:1)

Looking at your repository's package.json, react is only dev dependency but not included as a real dependency. Could it be an issue? Again... I don't know much about JS development so I might be doing something wrong...


my dependencies in package.json look like

  "dependencies": {
    "@testing-library/jest-dom": "^5.1.1",
    "@testing-library/react": "^9.4.0",
    "@types/react": "^16.9.19",
    "@types/react-dom": "^16.9.5",
    "blurhash": "^1.1.3",
    "react": "^16.12.0",
    "react-blurhash": "^0.1.2",
    "react-content-loader": "^5.0.1",
    "react-dom": "^16.12.0"
  },
  "devDependencies": {
    "@types/enzyme": "^3.10.5",
    "@types/jest": "^25.1.2",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.2",
    "jest": "^25.1.0",
    "parcel-bundler": "^1.12.4",
    "prettier": "^1.19.1",
    "react-testing-library": "^8.0.1",
    "ts-jest": "^25.2.0",
    "tslint": "^6.0.0",
    "tslint-config-prettier": "^1.18.0",
    "tslint-react": "^4.2.0",
    "typescript": "^3.7.5"
  }

How can i use blurhash in responsive image

Hey guys thank you for your amazing repository.
currently i am trying to use blurhash to my project. but problem is how can i implement this to responsive size image?
i would really appreciate if anyone answer to this question.

Failed to parse source map from '../node_modules/react-blurhash/src/Blurhash.tsx' file

Hi, I used react-blurhash and when I start my project I am getting this error messages:

WARNING in ./node_modules/react-blurhash/es/Blurhash.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from '/home/BeerCoder/MyProject/store/node_modules/react-blurhash/src/Blurhash.tsx' file:
Error: ENOENT: no such file or directory, open '/home/BeerCoder/MyProject/store/node_modules/react-blurhash/src/Blurhash.tsx'
 @ ./node_modules/react-blurhash/es/index.js 1:0-49 1:0-49
 @ ./src/index.tsx 9:0-39 29:0-4
WARNING in ./node_modules/react-blurhash/es/BlurhashCanvas.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from '/home/BeerCoder/MyProject/store/node_modules/react-blurhash/src/BlurhashCanvas.tsx' file: Error: ENOENT: no such file or directory, open '/home/BeerCoder/MyProject/store/node_modules/react-blurhash/src/BlurhashCanvas.tsx'
 @ ./node_modules/react-blurhash/es/index.js 2:0-61 2:0-61
 @ ./src/index.tsx 9:0-39 29:0-4
WARNING in ./node_modules/react-blurhash/es/index.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from '/home/BeerCoder/MyProject/store/node_modules/react-blurhash/src/index.ts' file: Error: ENOENT: no such file or directory, open '/home/BeerCoder/MyProject/store/node_modules/react-blurhash/src/index.ts'
 @ ./src/index.tsx 9:0-39 29:0-4

I am using:

"react": "^18.2.0",
"react-blurhash": "^0.1.3",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"typescript": "^4.7.4",

This seems to be some error in the package? @nygardk @muescha

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.