- i18next 23->26 + react-i18next 15->17 - prettier 2->3, reformat all files - replace @fontsource/inter with @fontsource-variable/inter 5, update import path - domhandler 5->6 (aligns with transitive deps) - lint-staged 16->17
30 lines
982 B
TypeScript
30 lines
982 B
TypeScript
import { encode, isBlurhashValid } from 'blurhash';
|
|
|
|
export const encodeBlurHash = (
|
|
img: HTMLImageElement | HTMLVideoElement,
|
|
width?: number,
|
|
height?: number,
|
|
): string | undefined => {
|
|
const imgWidth = img instanceof HTMLVideoElement ? img.videoWidth : img.width;
|
|
const imgHeight = img instanceof HTMLVideoElement ? img.videoHeight : img.height;
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = width || imgWidth;
|
|
canvas.height = height || imgHeight;
|
|
const context = canvas.getContext('2d');
|
|
|
|
if (!context) return undefined;
|
|
context.drawImage(img, 0, 0, canvas.width, canvas.height);
|
|
const data = context.getImageData(0, 0, canvas.width, canvas.height);
|
|
return encode(data.data, data.width, data.height, 4, 4);
|
|
};
|
|
|
|
export const validBlurHash = (hash?: string): string | undefined => {
|
|
if (typeof hash === 'string') {
|
|
const validity = isBlurhashValid(hash);
|
|
|
|
return validity.result ? hash : undefined;
|
|
}
|
|
|
|
return undefined;
|
|
};
|