Skip to content

Visualize Your Ideas With Code

feat(2d): warn about missing image source (#817)

Jacob6dcdb5f3b83d
diff --git a/packages/2d/src/components/Img.ts b/packages/2d/src/components/Img.ts
index 4aa1b9c0..fdb64e10 100644
--- a/packages/2d/src/components/Img.ts
+++ b/packages/2d/src/components/Img.ts
@@ -14,7 +14,8 @@ import {
SignalValue,
SimpleSignal,
} from '@motion-canvas/core/lib/signals';
-import {viaProxy} from '@motion-canvas/core/lib/utils';
+import {useLogger, viaProxy} from '@motion-canvas/core/lib/utils';
+import imageWithoutSource from './__logs__/image-without-source.md';
export interface ImgProps extends RectProps {
/**
@@ -123,6 +124,13 @@ export class Img extends Rect {
public constructor(props: ImgProps) {
super(props);
+ if (!('src' in props)) {
+ useLogger().warn({
+ message: 'No source specified for the image',
+ remarks: imageWithoutSource,
+ inspect: this.key,
+ });
+ }
}
protected override desiredSize(): SerializedVector2<DesiredLength> {
@@ -140,19 +148,25 @@ export class Img extends Rect {
@computed()
protected image(): HTMLImageElement {
- const src = viaProxy(this.src());
- const url = new URL(src, window.location.origin);
- if (url.origin === window.location.origin) {
- const hash = this.view().assetHash();
- url.searchParams.set('asset-hash', hash);
+ const rawSrc = this.src();
+ let src = '';
+ let key = '';
+ if (rawSrc) {
+ key = viaProxy(rawSrc);
+ const url = new URL(key, window.location.origin);
+ if (url.origin === window.location.origin) {
+ const hash = this.view().assetHash();
+ url.searchParams.set('asset-hash', hash);
+ }
+ src = url.toString();
}
- let image = Img.pool[src];
+ let image = Img.pool[key];
if (!image) {
image = document.createElement('img');
image.crossOrigin = 'anonymous';
- image.src = url.toString();
- Img.pool[src] = image;
+ image.src = src;
+ Img.pool[key] = image;
}
if (!image.complete) {
diff --git a/packages/2d/src/components/__logs__/image-without-source.md b/packages/2d/src/components/__logs__/image-without-source.md
new file mode 100644
index 00000000..01252766
--- /dev/null
+++ b/packages/2d/src/components/__logs__/image-without-source.md
@@ -0,0 +1,17 @@
+The image won't be visible unless you specify a source:
+
+```tsx
+import myImage from './example.png';
+// ...
+<Img src={myImage} />;
+```
+
+If you did this intentionally, and don't want to see this warning, set the `src`
+property to `null`:
+
+```tsx
+<Img src={null} />
+```
+
+[Learn more](https://motioncanvas.io/docs/media#images) about working with
+images.