Skip to content

Open source CapCut alternative.

TypeScript

feat(site): snapshot painted canvases through the raster seam

David Le8e0f08c7aeaf
diff --git a/site/src/cut/lib/cameraCardComposite.test.ts b/site/src/cut/lib/cameraCardComposite.test.ts
index 39dc4b59..a541633c 100644
--- a/site/src/cut/lib/cameraCardComposite.test.ts
+++ b/site/src/cut/lib/cameraCardComposite.test.ts
@@ -45,6 +45,7 @@ beforeAll(() => {
createCanvas: (w, h) => new RecordingCanvas(w, h, `scratch${made++}`) as unknown as RasterSurface,
decodeImage: async () => null,
canvasToBlob: async () => new Blob(),
+ snapshot: async (canvas) => canvas as unknown as ImageBitmap,
});
});
diff --git a/site/src/cut/lib/compareToSource.test.ts b/site/src/cut/lib/compareToSource.test.ts
index fe0195a3..76efd5b2 100644
--- a/site/src/cut/lib/compareToSource.test.ts
+++ b/site/src/cut/lib/compareToSource.test.ts
@@ -31,6 +31,7 @@ setRasterFactory({
createCanvas: stubCanvas,
decodeImage: async () => ({ source: {} as CanvasImageSource, width: 90, height: 160 }),
canvasToBlob: async () => new Blob([new Uint8Array([1, 2, 3])], { type: "image/jpeg" }),
+ snapshot: async (canvas) => canvas as unknown as ImageBitmap,
});
// Checking a replica against its source reads two pictures per moment, so what
diff --git a/site/src/cut/lib/composite.test.ts b/site/src/cut/lib/composite.test.ts
index 83229f84..38d5189d 100644
--- a/site/src/cut/lib/composite.test.ts
+++ b/site/src/cut/lib/composite.test.ts
@@ -95,6 +95,7 @@ setRasterFactory({
createCanvas: (w, h) => new PixelCanvas(w, h) as unknown as RasterSurface,
decodeImage: async () => null,
canvasToBlob: async () => new Blob(),
+ snapshot: async (canvas) => canvas as unknown as ImageBitmap,
});
afterAll(() => clearClipLuts());
diff --git a/site/src/cut/lib/headless/skiaRaster.ts b/site/src/cut/lib/headless/skiaRaster.ts
index 1468ea41..87aa12d5 100644
--- a/site/src/cut/lib/headless/skiaRaster.ts
+++ b/site/src/cut/lib/headless/skiaRaster.ts
@@ -73,6 +73,10 @@ export async function installSkiaRaster(): Promise<boolean> {
);
return new Blob([new Uint8Array(buf)], { type });
},
+ snapshot: async (canvas) => {
+ const img = await skia.loadImage(await (canvas as unknown as InstanceType<typeof skia.Canvas>).toBuffer("png"));
+ return Object.assign(img, { close: () => {} }) as unknown as ImageBitmap;
+ },
});
return true;
} catch {
diff --git a/site/src/cut/lib/raster.test.ts b/site/src/cut/lib/raster.test.ts
new file mode 100644
index 00000000..6e481e9d
--- /dev/null
+++ b/site/src/cut/lib/raster.test.ts
@@ -0,0 +1,33 @@
+import { afterAll, describe, expect, test } from "bun:test";
+import { createRasterCanvas, setRasterFactory, snapshotRaster, type RasterFactory, type RasterSurface } from "./raster";
+
+/** Bitmaps of painted surfaces come from the installed factory, so a headless
+ * process that swaps the factory serves every snapshot. */
+
+describe("raster snapshots", () => {
+ const seen: RasterSurface[] = [];
+ const fake = { close: () => {} } as unknown as ImageBitmap;
+ const surface = {} as RasterSurface;
+ const factory: RasterFactory = {
+ createCanvas: () => surface,
+ decodeImage: async () => null,
+ canvasToBlob: async () => new Blob(),
+ snapshot: async (canvas) => {
+ seen.push(canvas);
+ return fake;
+ },
+ };
+ let replaced: RasterFactory | undefined;
+ afterAll(() => {
+ if (replaced) {
+ setRasterFactory(replaced);
+ }
+ });
+
+ test("snapshotRaster goes through the installed factory", async () => {
+ replaced = setRasterFactory(factory);
+ const canvas = createRasterCanvas(4, 4);
+ expect(await snapshotRaster(canvas)).toBe(fake);
+ expect(seen).toEqual([surface]);
+ });
+});
diff --git a/site/src/cut/lib/raster.ts b/site/src/cut/lib/raster.ts
index dae2ef79..5686c5ee 100644
--- a/site/src/cut/lib/raster.ts
+++ b/site/src/cut/lib/raster.ts
@@ -18,6 +18,8 @@ export interface RasterFactory {
/** Encode a canvas. `type` is a MIME type ("image/png", "image/jpeg");
* `quality` applies to the lossy ones. */
canvasToBlob(canvas: RasterSurface, type: string, quality?: number): Promise<Blob>;
+ /** A bitmap of what a canvas holds now, for drawing it again later. */
+ snapshot(canvas: RasterSurface): Promise<ImageBitmap>;
}
const domFactory: RasterFactory = {
@@ -54,14 +56,18 @@ const domFactory: RasterFactory = {
)
);
},
+ snapshot: (canvas) => createImageBitmap(canvas),
};
let factory: RasterFactory = domFactory;
/** Install a replacement surface, e.g. the skia factory in a headless
- * process. Affects every later createRasterCanvas/decodeRasterImage call. */
-export function setRasterFactory(f: RasterFactory): void {
+ * process. Affects every later createRasterCanvas/decodeRasterImage call.
+ * Returns the factory it replaced. */
+export function setRasterFactory(f: RasterFactory): RasterFactory {
+ const replaced = factory;
factory = f;
+ return replaced;
}
export const createRasterCanvas = (w: number, h: number): RasterSurface => factory.createCanvas(w, h);
@@ -73,6 +79,7 @@ export const rasterCanvasToBlob = (
type: string,
quality?: number
): Promise<Blob> => factory.canvasToBlob(canvas, type, quality);
+export const snapshotRaster = (canvas: RasterSurface): Promise<ImageBitmap> => factory.snapshot(canvas);
/** Decode an image that lives at a URL. Goes through `fetch` so the same call
* works on a page and in a process with no `Image` constructor; anonymous CORS
diff --git a/site/src/cut/lib/stickerCache.test.ts b/site/src/cut/lib/stickerCache.test.ts
index 6826bef5..cf91c9e8 100644
--- a/site/src/cut/lib/stickerCache.test.ts
+++ b/site/src/cut/lib/stickerCache.test.ts
@@ -17,6 +17,7 @@ setRasterFactory({
return { source: {} as CanvasImageSource, width: SIDE, height: SIDE };
},
canvasToBlob: async () => new Blob(),
+ snapshot: async (canvas) => canvas as unknown as ImageBitmap,
});
globalThis.fetch = (async () => new Response(new Blob())) as typeof fetch;