-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #726 from simonihmig/wc-blurhash
Support BlurHash in wc/lit package
- Loading branch information
Showing
10 changed files
with
125 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@responsive-image/wc': minor | ||
--- | ||
|
||
Add BlurHash support to web component |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@responsive-image/ember': patch | ||
'@responsive-image/core': patch | ||
--- | ||
|
||
Extract BlurHash utils onto core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { decode } from 'blurhash'; | ||
|
||
const BLURHASH_ATTRIBUTE = 'data-ri-bh'; | ||
const WIDTH_ATTRIBUTE = 'data-ri-bh-w'; | ||
const HEIGHT_ATTRIBUTE = 'data-ri-bh-h'; | ||
|
||
const BLURHASH_SCALE_FACTOR = 4; | ||
|
||
export function bh2url( | ||
hash: string, | ||
width: number, | ||
height: number, | ||
): string | undefined { | ||
const blurWidth = width * BLURHASH_SCALE_FACTOR; | ||
const blurHeight = height * BLURHASH_SCALE_FACTOR; | ||
const pixels = decode(hash, blurWidth, blurHeight); | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = blurWidth; | ||
canvas.height = blurHeight; | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) { | ||
return undefined; | ||
} | ||
|
||
const imageData = ctx.createImageData(blurWidth, blurHeight); | ||
imageData.data.set(pixels); | ||
ctx.putImageData(imageData, 0, 0); | ||
return canvas.toDataURL('image/png'); | ||
} | ||
|
||
export function applySSR(): void { | ||
const images = document.querySelectorAll<HTMLImageElement>( | ||
`img[${BLURHASH_ATTRIBUTE}]`, | ||
); | ||
images.forEach((image) => { | ||
const hash = image.getAttribute(BLURHASH_ATTRIBUTE); | ||
const width = image.getAttribute(WIDTH_ATTRIBUTE); | ||
const height = image.getAttribute(HEIGHT_ATTRIBUTE); | ||
|
||
if (hash && width && height) { | ||
const url = bh2url(hash, parseInt(width, 10), parseInt(height, 10)); | ||
if (url) { | ||
image.style.backgroundImage = `url("${url}")`; | ||
image.style.backgroundSize = 'cover'; | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1 @@ | ||
import { decode } from 'blurhash'; | ||
|
||
const BLURHASH_ATTRIBUTE = 'data-ri-bh'; | ||
const WIDTH_ATTRIBUTE = 'data-ri-bh-w'; | ||
const HEIGHT_ATTRIBUTE = 'data-ri-bh-h'; | ||
|
||
const BLURHASH_SCALE_FACTOR = 4; | ||
|
||
export function bh2url( | ||
hash: string, | ||
width: number, | ||
height: number, | ||
): string | undefined { | ||
const blurWidth = width * BLURHASH_SCALE_FACTOR; | ||
const blurHeight = height * BLURHASH_SCALE_FACTOR; | ||
const pixels = decode(hash, blurWidth, blurHeight); | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = blurWidth; | ||
canvas.height = blurHeight; | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) { | ||
return undefined; | ||
} | ||
|
||
const imageData = ctx.createImageData(blurWidth, blurHeight); | ||
imageData.data.set(pixels); | ||
ctx.putImageData(imageData, 0, 0); | ||
return canvas.toDataURL('image/png'); | ||
} | ||
|
||
export function applySSR(): void { | ||
const images = document.querySelectorAll<HTMLImageElement>( | ||
`img[${BLURHASH_ATTRIBUTE}]`, | ||
); | ||
images.forEach((image) => { | ||
const hash = image.getAttribute(BLURHASH_ATTRIBUTE); | ||
const width = image.getAttribute(WIDTH_ATTRIBUTE); | ||
const height = image.getAttribute(HEIGHT_ATTRIBUTE); | ||
|
||
if (hash && width && height) { | ||
const url = bh2url(hash, parseInt(width, 10), parseInt(height, 10)); | ||
if (url) { | ||
image.style.backgroundImage = `url("${url}")`; | ||
image.style.backgroundSize = 'cover'; | ||
} | ||
} | ||
}); | ||
} | ||
export * from '@responsive-image/core/blurhash'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.