|
| 1 | +# `createPlaceholderFromHash` |
| 2 | + |
| 3 | +Generates a PNG data URI placeholder from a BlurHash or ThumbHash string. This function is used internally by [`lazyLoad`](/api/lazy-load) but can also be called directly for custom implementations. |
| 4 | + |
| 5 | +## Type Declarations |
| 6 | + |
| 7 | +```ts |
| 8 | +function createPlaceholderFromHash(options?: { |
| 9 | + /** If present, hash and ratio are extracted from element's data attributes */ |
| 10 | + image?: HTMLImageElement |
| 11 | + /** Hash string to decode */ |
| 12 | + hash?: string |
| 13 | + /** Type of hash algorithm */ |
| 14 | + hashType?: 'blurhash' | 'thumbhash' |
| 15 | + /** Size of longer edge for BlurHash decoding (ignored for ThumbHash) */ |
| 16 | + size?: number |
| 17 | + /** Aspect ratio (width/height) for BlurHash */ |
| 18 | + ratio?: number |
| 19 | +}): string | undefined |
| 20 | +``` |
| 21 | + |
| 22 | +## Parameters |
| 23 | + |
| 24 | +The function accepts an options object with the following properties: |
| 25 | + |
| 26 | +- `image`: If provided, the function extracts the hash from `data-blurhash` or `data-thumbhash` attributes and calculates the aspect ratio from the element's dimensions |
| 27 | +- `hash`: The hash string to decode. If both `image` and `hash` are provided, `hash` takes precedence |
| 28 | +- `hashType`: Specifies whether the hash is a `blurhash` or `thumbhash`. Default is `blurhash`. If `image` is provided, the type is determined from the presence of `data-blurhash` or `data-thumbhash` attributes |
| 29 | +- `size`: The size of the longer edge for BlurHash decoding. Default is `32`. This parameter is ignored for ThumbHash |
| 30 | +- `ratio`: The aspect ratio (width divided by height) for BlurHash. If `image` is provided, this is calculated automatically from the element's dimensions |
| 31 | + |
| 32 | +## Return Value |
| 33 | + |
| 34 | +Returns a PNG data URI string if the hash is successfully decoded, or `undefined` if: |
| 35 | +- No hash is provided |
| 36 | +- The hash decoding fails |
| 37 | +- The required decoding library is not available |
| 38 | + |
| 39 | +## Examples |
| 40 | + |
| 41 | +Using with a hash string: |
| 42 | + |
| 43 | +```ts |
| 44 | +import { createPlaceholderFromHash } from 'unlazy' |
| 45 | +
|
| 46 | +const placeholder = createPlaceholderFromHash({ |
| 47 | + hash: 'LKO2:N%2Tw=w]~RBVZRi};RPxuwH', |
| 48 | + hashType: 'blurhash', |
| 49 | + size: 32, |
| 50 | + ratio: 16 / 9 |
| 51 | +}) |
| 52 | +``` |
| 53 | + |
| 54 | +Using with an image element: |
| 55 | + |
| 56 | +```ts |
| 57 | +import { createPlaceholderFromHash } from 'unlazy' |
| 58 | +
|
| 59 | +const img = document.querySelector('img') |
| 60 | +const placeholder = createPlaceholderFromHash({ |
| 61 | + image: img |
| 62 | +}) |
| 63 | +
|
| 64 | +if (placeholder) { |
| 65 | + img.src = placeholder |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +::: tip |
| 70 | +For server-side rendering, use the specialized functions instead: |
| 71 | +- [`createPngDataUri`](/api/blurhash-create-png-data-uri) from `unlazy/blurhash` |
| 72 | +- [`createPngDataUri`](/api/thumbhash-create-png-data-uri) from `unlazy/thumbhash` |
| 73 | +::: |
0 commit comments