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
56
57
58
59
60
61
62
63
64
65
66
67
68
|
import type { CanvasRenderer } from "../canvas-renderer";
import { resolveStickerId } from "@/lib/stickers";
import { VisualNode, type VisualNodeParams } from "./visual-node";
export interface StickerNodeParams extends VisualNodeParams {
stickerId: string;
}
interface CachedStickerSource {
source: HTMLImageElement;
width: number;
height: number;
}
const stickerSourceCache = new Map<string, Promise<CachedStickerSource>>();
function loadStickerSource(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: 200, height: 200 };
})();
stickerSourceCache.set(stickerId, promise);
return promise;
}
export class StickerNode extends VisualNode<StickerNodeParams> {
private cachedSource: Promise<CachedStickerSource>;
constructor(params: StickerNodeParams) {
super(params);
this.cachedSource = loadStickerSource(params.stickerId);
}
async render({ renderer, time }: { renderer: CanvasRenderer; time: number }) {
await super.render({ renderer, time });
if (!this.isInRange({ time })) {
return;
}
const { source, width, height } = await this.cachedSource;
this.renderVisual({
renderer,
source,
sourceWidth: width,
sourceHeight: height,
timelineTime: time,
});
}
}
|