-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use web workers for placeholder hash calculation
- Loading branch information
1 parent
f714a41
commit 1ef1ef1
Showing
5 changed files
with
64 additions
and
18 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
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { thumbHashToRGBA } from 'thumbhash' | ||
import { base64ToBytes } from './utils' | ||
import { rgbaToDataUri } from './utils/dataUri' | ||
|
||
export function createPngDataUri(hash: string) { | ||
const { w, h, rgba } = thumbHashToRGBA(base64ToBytes(hash)) | ||
return rgbaToDataUri(w, h, rgba) | ||
const worker = new Worker('./workers/thumbhash-worker.ts') | ||
worker.postMessage({ hash }) | ||
|
||
return new Promise<string>((resolve) => { | ||
worker.onmessage = (event) => { | ||
resolve(event.data) | ||
} | ||
}) | ||
} |
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,19 @@ | ||
import { decodeBlurHash } from 'fast-blurhash' | ||
import { getScaledDimensions } from '../utils' | ||
import { rgbaToDataUri } from '../utils/dataUri' | ||
|
||
interface MessageEventData { | ||
hash: string | ||
ratio: number | ||
size: number | ||
} | ||
|
||
onmessage = (event: MessageEvent<MessageEventData>) => { | ||
const { hash, ratio, size } = event.data | ||
|
||
const { width, height } = getScaledDimensions(ratio, size) | ||
const rgba = decodeBlurHash(hash, width, height) | ||
const dataURL = rgbaToDataUri(width, height, rgba) | ||
|
||
postMessage(dataURL) | ||
} |
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,16 @@ | ||
import { thumbHashToRGBA } from 'thumbhash' | ||
import { base64ToBytes } from '../utils' | ||
import { rgbaToDataUri } from '../utils/dataUri' | ||
|
||
interface MessageEventData { | ||
hash: string | ||
} | ||
|
||
onmessage = (event: MessageEvent<MessageEventData>) => { | ||
const { hash } = event.data | ||
|
||
const { w, h, rgba } = thumbHashToRGBA(base64ToBytes(hash)) | ||
const dataURL = rgbaToDataUri(w, h, rgba) | ||
|
||
postMessage(dataURL) | ||
} |