blob: 3c155cb61d279b7d1142be195c0e8fdf3d2eced6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import { resolveStickerId } from "@/lib/stickers";
import {
VisualNode,
type ResolvedVisualSourceNodeState,
type VisualNodeParams,
} from "./visual-node";
export interface StickerNodeParams extends VisualNodeParams {
stickerId: string;
intrinsicWidth?: number;
intrinsicHeight?: number;
}
interface CachedStickerSource {
source: HTMLImageElement;
width: number;
height: number;
}
const stickerSourceCache = new Map<string, Promise<CachedStickerSource>>();
export function loadStickerSource({
stickerId,
}: {
stickerId: string;
}): Promise<CachedStickerSource> {
const cached = stickerSourceCache.get(stickerId);
if (cached) return cached;
const promise = (async (): Promise<CachedStickerSource> => {
const url = resolveStickerId({
stickerId,
options: { width: 200, height: 200 },
});
const image = new Image();
await new Promise<void>((resolve, reject) => {
image.onload = () => resolve();
image.onerror = () =>
reject(new Error(`Failed to load sticker: ${stickerId}`));
image.src = url;
});
return { source: image, width: image.naturalWidth, height: image.naturalHeight };
})();
stickerSourceCache.set(stickerId, promise);
return promise;
}
export class StickerNode extends VisualNode<
StickerNodeParams,
ResolvedVisualSourceNodeState
> {}
|