Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why canvas? #3

Open
WorldMaker opened this issue Dec 5, 2019 · 6 comments
Open

Why canvas? #3

WorldMaker opened this issue Dec 5, 2019 · 6 comments

Comments

@WorldMaker
Copy link

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

@WorldMaker
Copy link
Author

WorldMaker commented Dec 6, 2019

Had some time to prototype this afternoon. I couldn't find a mime-type that emulated the simple pixel format of ImageData, so it still needs a temporary canvas for the immediate work, but canvas has a handy toBlob.

Here's a gist of a simple hook-based approach: https://gist.github.com/WorldMaker/a3cbe0059acd827edee568198376b95a

(ETA: Tested it with a basic img tag and a simple CSS spin animation on it, and things seems good.)

@ngbrown
Copy link

ngbrown commented Mar 16, 2020

@WorldMaker the hook you published was useful. I didn't want to get into flipping between canvas and img. Especially when I am using srcset. To that end, I've made a BlurImg component.

The component is a very thin wrapper over img that mostly stays out of the way. It uses browser-native loading="lazy" to lazy load images. It renders a blurhash value to a blob when it about to appear on screen, but only when the image hasn't loaded yet. Once the image has finished loading, it removes the blob and background.

Here's a gist with the BlurImg component and an updated useBlurhash:
https://gist.github.com/ngbrown/d62eb518753378eb0a9bf02bb4723235

@kyle-mccarthy
Copy link

@ngbrown you should consider opening a PR, the useBlurhash hook is really useful

@ngbrown
Copy link

ngbrown commented Jun 2, 2020

@kyle-mccarthy pull request to what? Neither the useBlurhash hook nor the BlurImg component use react-blurhash, what this repository is.

@kyle-mccarthy
Copy link

kyle-mccarthy commented Jun 2, 2020 via email

@lilouartz
Copy link

Would this not be a better job for a webworker?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants