Skip to content

The VS Code of video editing, built for agents.

TypeScript

feat: add SVG thumbnail support and improve getSvgDimensions function

konstantin-paulus5ac1c05ecae2
diff --git a/packages/assets/src/derive/thumbnail.ts b/packages/assets/src/derive/thumbnail.ts
index 4caca771..9a7a3ad0 100644
--- a/packages/assets/src/derive/thumbnail.ts
+++ b/packages/assets/src/derive/thumbnail.ts
@@ -12,6 +12,7 @@ import { ALL_FORMATS, BlobSource, CanvasSink, Input } from 'mediabunny';
import { MASK_MIME_TYPE, MaskFile } from '../mask';
import { traceMask } from '../mask-outline';
+import { getSvgDimensions } from '../probe';
/** The width the asset bar shows; what the cache stores without a variant. */
export const DEFAULT_THUMBNAIL_WIDTH = 300;
@@ -27,6 +28,7 @@ const MASK_BACKGROUND = '#1a1a1a';
/** A WebP thumbnail of `file`, `width` wide, or null when it is no image, video with a picture, or mask. */
export async function deriveThumbnail(file: Blob, mimeType: string, width = DEFAULT_THUMBNAIL_WIDTH): Promise<Blob | null> {
+ if (mimeType === 'image/svg+xml') return svgThumbnail(file, width);
if (mimeType.startsWith('image/')) return imageThumbnail(file, width);
if (mimeType.startsWith('video/')) return videoThumbnail(file, width);
if (mimeType === MASK_MIME_TYPE) return maskThumbnail(file, width);
@@ -46,6 +48,29 @@ async function imageThumbnail(file: Blob, width: number): Promise<Blob | null> {
}
}
+/**
+ * createImageBitmap takes no SVG blob, so an SVG goes through an `<img>`
+ * and is drawn at the thumbnail's size: rasterized there, not scaled down.
+ * Its size comes from the file, as an SVG with only a viewBox reports none.
+ */
+async function svgThumbnail(file: Blob, width: number): Promise<Blob | null> {
+ const size = await getSvgDimensions(file);
+ const height = Math.max(1, Math.round((width * size.height) / size.width));
+ const url = URL.createObjectURL(file);
+ try {
+ const img = new Image();
+ img.src = url;
+ await img.decode();
+ const canvas = new OffscreenCanvas(width, height);
+ const ctx = canvas.getContext('2d');
+ if (!ctx) throw new Error('Could not create a 2d context');
+ ctx.drawImage(img, 0, 0, width, height);
+ return await encode(canvas);
+ } finally {
+ URL.revokeObjectURL(url);
+ }
+}
+
async function videoThumbnail(file: Blob, width: number): Promise<Blob | null> {
const input = new Input({ formats: ALL_FORMATS, source: new BlobSource(file) });
try {
diff --git a/packages/assets/src/probe.ts b/packages/assets/src/probe.ts
index df9bde46..e43cde6d 100644
--- a/packages/assets/src/probe.ts
+++ b/packages/assets/src/probe.ts
@@ -217,7 +217,8 @@ async function getRasterDimensions(file: Blob): Promise<{ width: number; height:
return { width, height };
}
-async function getSvgDimensions(file: Blob): Promise<{ width: number; height: number }> {
+/** An SVG's size from its `width`/`height`, else its `viewBox`, else the spec's 300×150. */
+export async function getSvgDimensions(file: Blob): Promise<{ width: number; height: number }> {
const text = await file.text();
const root = new DOMParser().parseFromString(text, 'image/svg+xml').documentElement;