Skip to content

Visualize Your Ideas With Code

feat(2d): support HMR for images (#641)

Jacobcf17520aa8dd
diff --git a/packages/2d/src/components/Img.ts b/packages/2d/src/components/Img.ts
index 5f98b31d..061098f6 100644
--- a/packages/2d/src/components/Img.ts
+++ b/packages/2d/src/components/Img.ts
@@ -68,6 +68,18 @@ export interface ImgProps extends RectProps {
export class Img extends Rect {
private static pool: Record<string, HTMLImageElement> = {};
+ static {
+ if (import.meta.hot) {
+ import.meta.hot.on('motion-canvas:assets', ({urls}) => {
+ for (const url of urls) {
+ if (this.pool[url]) {
+ delete this.pool[url];
+ }
+ }
+ });
+ }
+ }
+
/**
* The source of this image.
*
@@ -130,11 +142,12 @@ export class Img extends Rect {
@computed()
protected image(): HTMLImageElement {
const src = viaProxy(this.src());
+ const hash = this.view().assetHash();
let image = Img.pool[src];
if (!image) {
image = document.createElement('img');
image.crossOrigin = 'anonymous';
- image.src = src;
+ image.src = `${src}?${hash}`;
Img.pool[src] = image;
}
diff --git a/packages/2d/src/components/View2D.ts b/packages/2d/src/components/View2D.ts
index 2e41b701..6523c38d 100644
--- a/packages/2d/src/components/View2D.ts
+++ b/packages/2d/src/components/View2D.ts
@@ -4,6 +4,10 @@ import {PlaybackState} from '@motion-canvas/core';
import {SimpleSignal} from '@motion-canvas/core/lib/signals';
import {lazy} from '@motion-canvas/core/lib/decorators';
+export interface View2DProps extends RectProps {
+ assetHash: string;
+}
+
export class View2D extends Rect {
@lazy(() => {
const frameID = 'motion-canvas-2d-frame';
@@ -27,7 +31,10 @@ export class View2D extends Rect {
@signal()
public declare readonly playbackState: SimpleSignal<PlaybackState, this>;
- public constructor(props: RectProps) {
+ @signal()
+ public declare readonly assetHash: SimpleSignal<string, this>;
+
+ public constructor(props: View2DProps) {
super({
composite: true,
fontFamily: 'Roboto',
diff --git a/packages/2d/src/scenes/Scene2D.ts b/packages/2d/src/scenes/Scene2D.ts
index 178fea71..e1c961bc 100644
--- a/packages/2d/src/scenes/Scene2D.ts
+++ b/packages/2d/src/scenes/Scene2D.ts
@@ -15,12 +15,19 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
private view: View2D | null = null;
private registeredNodes: Record<string, Node> = {};
private nodeCounters: Record<string, number> = {};
+ private assetHash = Date.now().toString();
public constructor(
description: FullSceneDescription<ThreadGeneratorFactory<View2D>>,
) {
super(description);
this.recreateView();
+ if (import.meta.hot) {
+ import.meta.hot.on('motion-canvas:assets', () => {
+ this.assetHash = Date.now().toString();
+ this.getView().assetHash(this.assetHash);
+ });
+ }
}
public getView(): View2D {
@@ -131,6 +138,7 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
this.view = new View2D({
position: size.scale(this.resolutionScale / 2),
scale: this.resolutionScale,
+ assetHash: this.assetHash,
size,
});
});