From 83e01db31e8ba0dabba70aa42801cfe62156d9c1 Mon Sep 17 00:00:00 2001
From: Milan Pramod
Date: Sat, 14 Nov 2026 21:10:22 +0530
Subject: [PATCH 1/2] feat(canvas): implement image element with drag-and-drop,
clipboard paste, and canvas rendering (#28)
fix(canvas): resolve image re-rendering and caching issues after element deletion
---
src-tauri/tauri.conf.json | 3 +-
src/App.tsx | 18 +-
src/AppImportIntegration.test.tsx | 29 +++
src/components/canvas/CanvasViewport.tsx | 182 ++++++++++--------
.../canvas/CanvasViewportDragDrop.test.tsx | 63 ++++++
src/core/clipboard/clipboard.test.ts | 59 +++++-
src/core/clipboard/clipboard.ts | 153 ++++++++++++++-
src/core/engine/CanvasRenderer.test.ts | 123 ++++++++++++
src/core/engine/CanvasRenderer.ts | 136 +++++++++++++
src/core/engine/HitTester.test.ts | 44 +++++
src/core/engine/HitTester.ts | 9 +
src/core/geometry/transformMath.test.ts | 47 ++++-
src/core/geometry/transformMath.ts | 33 +++-
src/core/persistence/autoSave.ts | 10 +-
src/core/persistence/fileIO.test.ts | 50 ++++-
src/core/persistence/fileIO.ts | 4 +-
.../importers/excalidrawImporter.test.ts | 36 ++++
.../importers/excalidrawImporter.ts | 41 ++++
src/types/canvas.test.ts | 30 +++
src/types/canvas.ts | 13 +-
20 files changed, 982 insertions(+), 101 deletions(-)
diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json
index 5a9400e..4bdabb8 100644
--- a/src-tauri/tauri.conf.json
+++ b/src-tauri/tauri.conf.json
@@ -14,7 +14,8 @@
{
"title": "scribl",
"width": 800,
- "height": 600
+ "height": 600,
+ "dragDropEnabled": false
}
],
"security": {
diff --git a/src/App.tsx b/src/App.tsx
index 3bde65e..60f709e 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -142,7 +142,7 @@ export function App() {
return;
}
e.preventDefault();
- paste();
+ paste(e.clipboardData);
};
window.addEventListener('copy', handleCopy);
@@ -155,6 +155,22 @@ export function App() {
};
}, []);
+ // Prevent default browser dragover/drop navigation behavior across window
+ useEffect(() => {
+ const handleDragOver = (e: DragEvent) => {
+ e.preventDefault();
+ };
+ const handleDrop = (e: DragEvent) => {
+ e.preventDefault();
+ };
+ window.addEventListener('dragover', handleDragOver);
+ window.addEventListener('drop', handleDrop);
+ return () => {
+ window.removeEventListener('dragover', handleDragOver);
+ window.removeEventListener('drop', handleDrop);
+ };
+ }, []);
+
const handleSave = () => {
const doc = useDocumentStore.getState();
const camera = useCameraStore.getState().camera;
diff --git a/src/AppImportIntegration.test.tsx b/src/AppImportIntegration.test.tsx
index 49b534c..828bbcb 100644
--- a/src/AppImportIntegration.test.tsx
+++ b/src/AppImportIntegration.test.tsx
@@ -85,4 +85,33 @@ describe('App Import Integration', () => {
expect(screen.getByText(/1 unsupported items \(note\) were discarded/i)).toBeTruthy();
});
});
+
+ it('imports dropped image file directly into canvas as an ImageElement', async () => {
+ const { container } = render();
+ const viewport = container.querySelector('[data-testid="canvas-viewport"]');
+ expect(viewport).toBeTruthy();
+
+ const fakeBlob = new Blob(['fakepngbytes'], { type: 'image/png' });
+ const imageFile = new File([fakeBlob], 'mockup.png', { type: 'image/png' });
+
+ fireEvent.drop(viewport!, {
+ dataTransfer: {
+ files: [imageFile],
+ types: ['Files'],
+ },
+ clientX: 400,
+ clientY: 300,
+ });
+
+ await vi.waitFor(() => {
+ const doc = useDocumentStore.getState();
+ expect(doc.elementIds.length).toBe(1);
+ const el = doc.elements[doc.elementIds[0]];
+ expect(el.type).toBe('image');
+ if (el.type === 'image') {
+ expect(el.fileName).toBe('mockup.png');
+ expect(el.mimeType).toBe('image/png');
+ }
+ });
+ });
});
diff --git a/src/components/canvas/CanvasViewport.tsx b/src/components/canvas/CanvasViewport.tsx
index 7271aab..2cf642a 100644
--- a/src/components/canvas/CanvasViewport.tsx
+++ b/src/components/canvas/CanvasViewport.tsx
@@ -1,12 +1,14 @@
import React, { useEffect, useRef, useState } from 'react';
import { screenToWorld, worldToScreen } from '../../core/camera/cameraMath';
-import { renderCommittedCanvas } from '../../core/engine/CanvasRenderer';
+import { createImageElementFromBlob } from '../../core/clipboard/clipboard';
+import { imageCache, renderCommittedCanvas } from '../../core/engine/CanvasRenderer';
import { ScratchRenderer } from '../../core/engine/ScratchRenderer';
import {
computeSelectionBounds,
HandleType,
hitTestHandles,
} from '../../core/geometry/transformMath';
+import { applyElementDelta } from '../../core/history/commands';
import { PointerRouter } from '../../core/input/PointerRouter';
import { MarqueeBox } from '../../core/input/ToolFSM';
import { importDrawingDocument } from '../../core/persistence/importers';
@@ -53,6 +55,14 @@ export const CanvasViewport: React.FC = ({ onImportNotifica
const [isRotateKeyHeld, setIsRotateKeyHeld] = useState(false);
const [hoveredHandle, setHoveredHandle] = useState(null);
const [isDraggingFile, setIsDraggingFile] = useState(false);
+ const [imageTick, setImageTick] = useState(0);
+
+ // Subscribe to image decoding loads to re-render committed canvas
+ useEffect(() => {
+ return imageCache.subscribe(() => {
+ setImageTick((t) => t + 1);
+ });
+ }, []);
const scratchRendererRef = useRef(new ScratchRenderer());
const pointerRouterRef = useRef(null);
@@ -174,7 +184,7 @@ export const CanvasViewport: React.FC = ({ onImportNotifica
editingText?.elementId,
themeMode,
);
- }, [elements, elementIds, camera, width, height, editingText?.elementId, themeMode]);
+ }, [elements, elementIds, camera, width, height, editingText?.elementId, themeMode, imageTick]);
// Mouse wheel zoom
const handleWheel = (e: React.WheelEvent) => {
@@ -239,103 +249,109 @@ export const CanvasViewport: React.FC = ({ onImportNotifica
}
};
- const handleDrop = (e: React.DragEvent) => {
+ const handleDrop = async (e: React.DragEvent) => {
e.preventDefault();
setIsDraggingFile(false);
- const file = e.dataTransfer.files?.[0];
- if (!file) return;
+ const files = Array.from(e.dataTransfer.files || []);
+ if (files.length === 0) return;
- const reader = new FileReader();
- reader.onload = (event) => {
- try {
- const text = event.target?.result as string;
- const result = importDrawingDocument(text, file.name);
+ useToolStore.getState().setTool('select');
- if (result.elements.length === 0) {
- if (result.discardedCount > 0) {
+ const rect = containerRef.current?.getBoundingClientRect();
+ const screenX = rect ? e.clientX - rect.left : e.clientX;
+ const screenY = rect ? e.clientY - rect.top : e.clientY;
+ const dropWorld = screenToWorld(screenX, screenY, camera, width, height);
+
+ let imageOffset = 0;
+
+ for (const file of files) {
+ const isImage = file.type.startsWith('image/')
+ || /\.(png|jpe?g|webp|svg|gif|avif|bmp|ico)$/i.test(file.name);
+
+ if (isImage) {
+ const offsetPos = {
+ x: dropWorld.x + imageOffset * 24,
+ y: dropWorld.y + imageOffset * 24,
+ };
+ try {
+ const success = await createImageElementFromBlob(file, offsetPos, file.name);
+ if (success) {
onImportNotification?.({
id: String(Date.now()),
- type: 'warning',
- message: `No supported elements found in ${file.name}`,
- details: `${result.discardedCount} unsupported items (${
- result.discardedTypes.join(', ')
- }) were discarded.`,
+ type: 'success',
+ message: `Imported image ${file.name}`,
});
}
- return;
+ } catch (err) {
+ onImportNotification?.({
+ id: String(Date.now()),
+ type: 'error',
+ message: `Failed to import image ${file.name}`,
+ details: err instanceof Error ? err.message : String(err),
+ });
}
+ imageOffset++;
+ continue;
+ }
- // Compute drop position in world coordinates
- const rect = containerRef.current?.getBoundingClientRect();
- const screenX = rect ? e.clientX - rect.left : e.clientX;
- const screenY = rect ? e.clientY - rect.top : e.clientY;
- const dropWorld = screenToWorld(screenX, screenY, camera, width, height);
-
- // Center elements at drop position
- const origCenterX = (result.bounds.minX + result.bounds.maxX) / 2;
- const origCenterY = (result.bounds.minY + result.bounds.maxY) / 2;
- const dx = dropWorld.x - origCenterX;
- const dy = dropWorld.y - origCenterY;
-
- const translatedElements = result.elements.map((el) => {
- if (el.type === 'stroke') {
- return {
- ...el,
- x: el.x + dx,
- y: el.y + dy,
- points: el.points.map((pt) => ({ ...pt, x: pt.x + dx, y: pt.y + dy })),
- };
- } else if (el.type === 'shape') {
- return {
- ...el,
- x: el.x + dx,
- y: el.y + dy,
- startPoint: el.startPoint
- ? { x: el.startPoint.x + dx, y: el.startPoint.y + dy }
- : undefined,
- endPoint: el.endPoint
- ? { x: el.endPoint.x + dx, y: el.endPoint.y + dy }
- : undefined,
- };
- } else if (el.type === 'text') {
- return {
- ...el,
- x: el.x + dx,
- y: el.y + dy,
- };
+ const reader = new FileReader();
+ reader.onload = (event) => {
+ try {
+ const text = event.target?.result as string;
+ const result = importDrawingDocument(text, file.name);
+
+ if (result.elements.length === 0) {
+ if (result.discardedCount > 0) {
+ onImportNotification?.({
+ id: String(Date.now()),
+ type: 'warning',
+ message: `No supported elements found in ${file.name}`,
+ details: `${result.discardedCount} unsupported items (${
+ result.discardedTypes.join(', ')
+ }) were discarded.`,
+ });
+ }
+ return;
}
- return el;
- });
- useDocumentStore.getState().insertElements(translatedElements);
+ // Center elements at drop position
+ const origCenterX = (result.bounds.minX + result.bounds.maxX) / 2;
+ const origCenterY = (result.bounds.minY + result.bounds.maxY) / 2;
+ const dx = dropWorld.x - origCenterX;
+ const dy = dropWorld.y - origCenterY;
- if (result.discardedCount > 0) {
- onImportNotification?.({
- id: String(Date.now()),
- type: 'warning',
- message: `Imported ${result.elements.length} elements from ${file.name}`,
- details: `${result.discardedCount} unsupported items (${
- result.discardedTypes.join(', ')
- }) were discarded.`,
- });
- } else {
+ const translatedElements = result.elements.map((el) => applyElementDelta(el, dx, dy));
+
+ useDocumentStore.getState().insertElements(translatedElements);
+
+ if (result.discardedCount > 0) {
+ onImportNotification?.({
+ id: String(Date.now()),
+ type: 'warning',
+ message: `Imported ${result.elements.length} elements from ${file.name}`,
+ details: `${result.discardedCount} unsupported items (${
+ result.discardedTypes.join(', ')
+ }) were discarded.`,
+ });
+ } else {
+ onImportNotification?.({
+ id: String(Date.now()),
+ type: 'success',
+ message: `Imported ${result.elements.length} elements from ${file.name}`,
+ });
+ }
+ } catch (err) {
onImportNotification?.({
id: String(Date.now()),
- type: 'success',
- message: `Imported ${result.elements.length} elements from ${file.name}`,
+ type: 'error',
+ message: `Failed to import ${file.name}`,
+ details: err instanceof Error ? err.message : String(err),
});
}
- } catch (err) {
- onImportNotification?.({
- id: String(Date.now()),
- type: 'error',
- message: `Failed to import ${file.name}`,
- details: err instanceof Error ? err.message : String(err),
- });
- }
- };
- reader.readAsText(file);
+ };
+ reader.readAsText(file);
+ }
};
const dpr = window.devicePixelRatio || 1;
@@ -495,7 +511,7 @@ export const CanvasViewport: React.FC = ({ onImportNotifica
color: 'var(--color-text-primary, #f8fafc)',
}}
>
- Drop drawing to insert
+ Drop image or drawing to insert
= ({ onImportNotifica
color: 'var(--color-text-secondary, #94a3b8)',
}}
>
- Supports .scribl, .excalidraw, and .tldr files
+ Supports images (PNG, JPG, SVG, WebP) and drawings (.scribl, .excalidraw, .tldr)
diff --git a/src/components/canvas/CanvasViewportDragDrop.test.tsx b/src/components/canvas/CanvasViewportDragDrop.test.tsx
index af2cf5f..b4a630f 100644
--- a/src/components/canvas/CanvasViewportDragDrop.test.tsx
+++ b/src/components/canvas/CanvasViewportDragDrop.test.tsx
@@ -74,4 +74,67 @@ describe('CanvasViewport Drag and Drop', () => {
}),
);
});
+
+ it('imports dropped image file and creates an ImageElement', async () => {
+ render();
+ const viewport = screen.getByTestId('canvas-viewport');
+
+ const imageFile = new File(['fake-png-binary-data'], 'test-image.png', {
+ type: 'image/png',
+ });
+
+ fireEvent.drop(viewport, {
+ clientX: 300,
+ clientY: 300,
+ dataTransfer: {
+ files: [imageFile],
+ },
+ });
+
+ await vi.waitFor(() => {
+ const doc = useDocumentStore.getState();
+ const imageEl = Object.values(doc.elements).find((el) => el.type === 'image');
+ expect(imageEl).toBeDefined();
+ expect(imageEl?.type).toBe('image');
+ expect((imageEl as any)?.fileName).toBe('test-image.png');
+ });
+ });
+
+ it('imports image, erases it, then imports another image', async () => {
+ render();
+ const viewport = screen.getByTestId('canvas-viewport');
+
+ const file1 = new File(['fake-png-1'], 'image-1.png', { type: 'image/png' });
+ fireEvent.drop(viewport, {
+ clientX: 300,
+ clientY: 300,
+ dataTransfer: { files: [file1] },
+ });
+
+ let firstImageId = '';
+ await vi.waitFor(() => {
+ const doc = useDocumentStore.getState();
+ const img = Object.values(doc.elements).find((el) => el.type === 'image');
+ expect(img).toBeDefined();
+ firstImageId = img!.id;
+ });
+
+ // Erase the first image
+ useDocumentStore.getState().deleteElements([firstImageId]);
+ expect(useDocumentStore.getState().elements[firstImageId]).toBeUndefined();
+
+ // Now drop second image
+ const file2 = new File(['fake-png-2'], 'image-2.png', { type: 'image/png' });
+ fireEvent.drop(viewport, {
+ clientX: 400,
+ clientY: 400,
+ dataTransfer: { files: [file2] },
+ });
+
+ await vi.waitFor(() => {
+ const doc = useDocumentStore.getState();
+ const img = Object.values(doc.elements).find((el) => (el as any).fileName === 'image-2.png');
+ expect(img).toBeDefined();
+ });
+ });
});
diff --git a/src/core/clipboard/clipboard.test.ts b/src/core/clipboard/clipboard.test.ts
index a4e3a1e..a2b9421 100644
--- a/src/core/clipboard/clipboard.test.ts
+++ b/src/core/clipboard/clipboard.test.ts
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useDocumentStore } from '../../stores/useDocumentStore';
-import { StrokeElement } from '../../types/canvas';
+import { ImageElement, StrokeElement } from '../../types/canvas';
import {
copyCanvasAsPng,
copySelected,
@@ -255,4 +255,61 @@ describe('Clipboard Module', () => {
expect(navigator.clipboard.write).not.toHaveBeenCalled();
});
});
+
+ describe('Image Clipboard Handling', () => {
+ it('copies selected ImageElement to system clipboard as both raster blob and JSON payload', async () => {
+ const img: ImageElement = {
+ id: 'img-1',
+ type: 'image',
+ x: 50,
+ y: 50,
+ width: 100,
+ height: 100,
+ naturalWidth: 200,
+ naturalHeight: 200,
+ aspectRatio: 1,
+ src:
+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
+ zIndex: 1,
+ };
+
+ useDocumentStore.getState().addElements([img]);
+ useDocumentStore.getState().setSelected(['img-1']);
+
+ const success = await copySelected();
+ expect(success).toBe(true);
+ expect(navigator.clipboard.write).toHaveBeenCalled();
+ const callArgs = vi.mocked(navigator.clipboard.write).mock.calls[0][0];
+ expect(callArgs[0].types).toContain('image/png');
+ expect(callArgs[0].types).toContain('text/plain');
+ });
+
+ it('pastes image file from clipboardData and creates ImageElement', async () => {
+ const fakeBlob = new Blob(['fakepngbytes'], { type: 'image/png' });
+ const fakeFile = new File([fakeBlob], 'screenshot.png', { type: 'image/png' });
+
+ const mockDataTransfer = {
+ items: [
+ {
+ type: 'image/png',
+ getAsFile: () => fakeFile,
+ },
+ ],
+ files: [fakeFile],
+ } as unknown as DataTransfer;
+
+ const success = await paste(mockDataTransfer);
+ expect(success).toBe(true);
+
+ const state = useDocumentStore.getState();
+ expect(state.selectedIds).toHaveLength(1);
+ const pastedId = state.selectedIds[0];
+ const pastedEl = state.elements[pastedId];
+ expect(pastedEl.type).toBe('image');
+ if (pastedEl.type === 'image') {
+ expect(pastedEl.mimeType).toBe('image/png');
+ expect(pastedEl.fileName).toBe('screenshot.png');
+ }
+ });
+ });
});
diff --git a/src/core/clipboard/clipboard.ts b/src/core/clipboard/clipboard.ts
index 39824a2..a265b93 100644
--- a/src/core/clipboard/clipboard.ts
+++ b/src/core/clipboard/clipboard.ts
@@ -1,7 +1,10 @@
+import { useCameraStore } from '../../stores/useCameraStore';
import { useDocumentStore } from '../../stores/useDocumentStore';
-import { CanvasElement } from '../../types/canvas';
+import { useToolStore } from '../../stores/useToolStore';
+import { CanvasElement, ImageElement } from '../../types/canvas';
+import { imageCache } from '../engine/CanvasRenderer';
import { applyElementDelta } from '../history/commands';
-import { exportToBlob } from '../persistence/fileIO';
+import { dataUrlToBlob, exportToBlob } from '../persistence/fileIO';
import { ThemeMode } from '../theme/themeManager';
export interface ScriblClipboardPayload {
@@ -50,13 +53,36 @@ export async function copySelected(): Promise {
pasteCount = 0;
try {
- if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
+ if (typeof navigator !== 'undefined') {
const payload: ScriblClipboardPayload = {
type: 'scribl/elements',
version: 1,
elements: toCopy,
};
- await navigator.clipboard.writeText(JSON.stringify(payload));
+
+ if (
+ toCopy.length === 1
+ && toCopy[0].type === 'image'
+ && navigator.clipboard?.write
+ && typeof ClipboardItem !== 'undefined'
+ ) {
+ try {
+ const imgEl = toCopy[0] as ImageElement;
+ const blob = dataUrlToBlob(imgEl.src);
+ await navigator.clipboard.write([
+ new ClipboardItem({
+ [blob.type || 'image/png']: blob,
+ 'text/plain': new Blob([JSON.stringify(payload)], { type: 'text/plain' }),
+ }),
+ ]);
+ } catch {
+ if (navigator.clipboard?.writeText) {
+ await navigator.clipboard.writeText(JSON.stringify(payload));
+ }
+ }
+ } else if (navigator.clipboard?.writeText) {
+ await navigator.clipboard.writeText(JSON.stringify(payload));
+ }
}
} catch {
// Non-blocking in environments without clipboard access
@@ -116,10 +142,125 @@ export async function cutSelected(): Promise {
return true;
}
-export async function paste(): Promise {
+export function readBlobAsDataUrl(blob: Blob): Promise {
+ return new Promise((resolve, reject) => {
+ const reader = new FileReader();
+ reader.onload = () => resolve(reader.result as string);
+ reader.onerror = () => reject(reader.error);
+ reader.readAsDataURL(blob);
+ });
+}
+
+export function getImageDimensions(src: string): Promise<{ width: number; height: number; }> {
+ return imageCache.loadImage(src).then((img) => ({
+ width: img.naturalWidth || 400,
+ height: img.naturalHeight || 300,
+ }));
+}
+
+export async function createImageElementFromBlob(
+ blob: Blob,
+ worldPos?: { x: number; y: number; },
+ fileName?: string,
+): Promise {
+ const dataUrl = await readBlobAsDataUrl(blob);
+ const dims = await getImageDimensions(dataUrl);
+
+ const cameraState = useCameraStore.getState();
+ const camera = cameraState.camera;
+ const viewportWidth = cameraState.viewportWidth || 1000;
+ const viewportHeight = cameraState.viewportHeight || 800;
+
+ // Maximum initial size: 50% of viewport in world units
+ const maxWorldW = (viewportWidth / camera.zoom) * 0.5;
+ const maxWorldH = (viewportHeight / camera.zoom) * 0.5;
+
+ let width = dims.width;
+ let height = dims.height;
+ const aspectRatio = dims.width && dims.height ? dims.width / dims.height : 1;
+
+ if (width > maxWorldW || height > maxWorldH) {
+ const scale = Math.min(maxWorldW / width, maxWorldH / height);
+ width = Math.round(width * scale);
+ height = Math.round(height * scale);
+ }
+
+ const targetX = worldPos !== undefined ? worldPos.x : camera.x;
+ const targetY = worldPos !== undefined ? worldPos.y : camera.y;
+
+ const x = Math.round(targetX - width / 2);
+ const y = Math.round(targetY - height / 2);
+
+ const doc = useDocumentStore.getState();
+ const nextZIndex = doc.elementIds.length + 1;
+ const imageId = `image_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
+
+ const newImage: ImageElement = {
+ id: imageId,
+ type: 'image',
+ x,
+ y,
+ width: Math.max(1, width),
+ height: Math.max(1, height),
+ naturalWidth: dims.width,
+ naturalHeight: dims.height,
+ aspectRatio,
+ src: dataUrl,
+ opacity: 1,
+ zIndex: nextZIndex,
+ fileName: fileName || (blob as File).name,
+ mimeType: blob.type || 'image/png',
+ };
+
+ doc.addElements([newImage]);
+ doc.setSelected([imageId]);
+ useToolStore.getState().setTool('select');
+ return true;
+}
+
+export async function paste(clipboardData?: DataTransfer | null): Promise {
+ // 1. Try reading image from passed ClipboardEvent.clipboardData
+ if (clipboardData) {
+ if (clipboardData.items) {
+ for (const item of Array.from(clipboardData.items)) {
+ if (item.type.startsWith('image/')) {
+ const file = item.getAsFile();
+ if (file) {
+ return await createImageElementFromBlob(file);
+ }
+ }
+ }
+ }
+ if (clipboardData.files) {
+ for (const file of Array.from(clipboardData.files)) {
+ if (file.type.startsWith('image/')) {
+ return await createImageElementFromBlob(file);
+ }
+ }
+ }
+ }
+
+ // 2. Try reading image from navigator.clipboard.read() if no image found in clipboardData
+ if (typeof navigator !== 'undefined' && navigator.clipboard?.read) {
+ try {
+ const items = await navigator.clipboard.read();
+ for (const item of items) {
+ const imageType = item.types.find((t) => t.startsWith('image/'));
+ if (imageType) {
+ const blob = await item.getType(imageType);
+ if (blob) {
+ return await createImageElementFromBlob(blob);
+ }
+ }
+ }
+ } catch {
+ // Permission denied or format not supported, continue to text/json
+ }
+ }
+
let sourceElements: CanvasElement[] = [];
- // Try reading from system clipboard first
+ // 3. Try reading from system clipboard text (Scribl JSON payload)
try {
if (typeof navigator !== 'undefined' && navigator.clipboard?.readText) {
const text = await navigator.clipboard.readText();
diff --git a/src/core/engine/CanvasRenderer.test.ts b/src/core/engine/CanvasRenderer.test.ts
index 39fd6f3..4fe4534 100644
--- a/src/core/engine/CanvasRenderer.test.ts
+++ b/src/core/engine/CanvasRenderer.test.ts
@@ -2,12 +2,15 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
Camera,
CanvasElement,
+ ImageElement,
ShapeElement,
StrokeElement,
TextElement,
} from '../../types/canvas';
import {
+ imageCache,
renderCommittedCanvas,
+ renderImage,
renderShape,
renderStroke,
renderText,
@@ -39,10 +42,14 @@ function createMockContext() {
setLineDash: vi.fn(),
fill: vi.fn(),
stroke: vi.fn(),
+ drawImage: vi.fn(),
+ strokeRect: vi.fn(),
+ fillRect: vi.fn(),
font: '',
fillStyle: '',
strokeStyle: '',
textBaseline: '',
+ globalAlpha: 1,
lineWidth: 1,
lineCap: 'butt',
lineJoin: 'miter',
@@ -57,6 +64,9 @@ function createMockContext() {
setLineDash: ReturnType;
fill: ReturnType;
stroke: ReturnType;
+ drawImage: ReturnType;
+ strokeRect: ReturnType;
+ fillRect: ReturnType;
};
}
@@ -465,4 +475,117 @@ describe('CanvasRenderer - renderCommittedCanvas with shapes', () => {
expect(ctx.fillStyle).toBe('#ffffff');
});
});
+
+ describe('CanvasRenderer - ImageElement', () => {
+ let ctx: ReturnType;
+
+ beforeEach(() => {
+ ctx = createMockContext();
+ imageCache.clear();
+ });
+
+ it('renders placeholder when image is decoding/loading', () => {
+ const imgEl: ImageElement = {
+ id: 'img-1',
+ type: 'image',
+ x: 50,
+ y: 50,
+ width: 200,
+ height: 150,
+ naturalWidth: 400,
+ naturalHeight: 300,
+ aspectRatio: 400 / 300,
+ src: 'data:image/png;base64,abc123',
+ zIndex: 1,
+ };
+
+ renderImage(ctx, imgEl);
+
+ expect(ctx.save).toHaveBeenCalled();
+ expect(ctx.setLineDash).toHaveBeenCalledWith([4, 4]);
+ expect(ctx.strokeRect).toHaveBeenCalledWith(50, 50, 200, 150);
+ expect(ctx.fillRect).toHaveBeenCalledWith(50, 50, 200, 150);
+ expect(ctx.restore).toHaveBeenCalled();
+ });
+
+ it('renders image via drawImage when loaded in cache', () => {
+ const mockImg = {
+ complete: true,
+ naturalWidth: 400,
+ naturalHeight: 300,
+ } as HTMLImageElement;
+
+ // Seed the cache
+ // @ts-expect-error accessing private for test
+ imageCache.cache.set('data:image/png;base64,loaded', mockImg);
+
+ const imgEl: ImageElement = {
+ id: 'img-loaded',
+ type: 'image',
+ x: 100,
+ y: 200,
+ width: 300,
+ height: 225,
+ naturalWidth: 400,
+ naturalHeight: 300,
+ aspectRatio: 400 / 300,
+ opacity: 0.8,
+ src: 'data:image/png;base64,loaded',
+ zIndex: 1,
+ };
+
+ renderImage(ctx, imgEl);
+
+ expect(ctx.save).toHaveBeenCalled();
+ expect(ctx.globalAlpha).toBe(0.8);
+ expect(ctx.drawImage).toHaveBeenCalledWith(mockImg, 100, 200, 300, 225);
+ expect(ctx.restore).toHaveBeenCalled();
+ });
+
+ it('renders ImageElement in renderCommittedCanvas with culling', () => {
+ const mockImg = {
+ complete: true,
+ naturalWidth: 200,
+ naturalHeight: 200,
+ } as HTMLImageElement;
+ // @ts-expect-error accessing private for test
+ imageCache.cache.set('data:image/png;base64,inside', mockImg);
+
+ const elements: Record = {
+ 'img-inside': {
+ id: 'img-inside',
+ type: 'image',
+ x: 100,
+ y: 100,
+ width: 200,
+ height: 200,
+ naturalWidth: 200,
+ naturalHeight: 200,
+ aspectRatio: 1,
+ src: 'data:image/png;base64,inside',
+ zIndex: 1,
+ },
+ 'img-culled': {
+ id: 'img-culled',
+ type: 'image',
+ x: 5000,
+ y: 5000,
+ width: 200,
+ height: 200,
+ naturalWidth: 200,
+ naturalHeight: 200,
+ aspectRatio: 1,
+ src: 'data:image/png;base64,culled',
+ zIndex: 2,
+ },
+ };
+
+ const camera: Camera = { x: 0, y: 0, zoom: 1 };
+ renderCommittedCanvas(ctx, elements, ['img-inside', 'img-culled'], camera, 800, 600, 1);
+
+ // Only img-inside should be drawn
+ expect(ctx.drawImage).toHaveBeenCalledTimes(1);
+ expect(ctx.drawImage).toHaveBeenCalledWith(mockImg, 100, 100, 200, 200);
+ });
+ });
});
diff --git a/src/core/engine/CanvasRenderer.ts b/src/core/engine/CanvasRenderer.ts
index ecadfe5..f3afc97 100644
--- a/src/core/engine/CanvasRenderer.ts
+++ b/src/core/engine/CanvasRenderer.ts
@@ -2,6 +2,7 @@ import { useConfigStore } from '../../stores/useConfigStore';
import {
Camera,
CanvasElement,
+ ImageElement,
ShapeElement,
StrokeElement,
TextElement,
@@ -93,6 +94,8 @@ export function renderCommittedCanvas(
continue;
}
renderText(ctx, el, mode);
+ } else if (el.type === 'image') {
+ renderImage(ctx, el);
}
}
@@ -178,3 +181,136 @@ export function renderStroke(
ctx.fill(stroke.cachedPath2D);
}
}
+
+export class ImageCache {
+ private cache = new Map();
+ private listeners = new Set<() => void>();
+
+ public getImage(src: string, onLoad?: () => void): HTMLImageElement | null {
+ const existing = this.cache.get(src);
+ if (existing) {
+ if (existing.complete && existing.naturalWidth > 0) {
+ return existing;
+ }
+ if (onLoad) {
+ existing.addEventListener('load', onLoad, { once: true });
+ }
+ return null;
+ }
+
+ if (typeof Image === 'undefined') {
+ return null;
+ }
+
+ const img = new Image();
+ img.src = src;
+ this.cache.set(src, img);
+
+ // If decoded synchronously (common with data URLs)
+ if (img.complete && img.naturalWidth > 0) {
+ return img;
+ }
+
+ const notify = () => {
+ onLoad?.();
+ for (const listener of this.listeners) {
+ listener();
+ }
+ };
+
+ img.onload = notify;
+ if (typeof img.decode === 'function') {
+ img.decode().then(notify).catch(() => {});
+ }
+
+ return null;
+ }
+
+ public loadImage(src: string): Promise {
+ return new Promise((resolve) => {
+ const existing = this.cache.get(src);
+ if (existing && existing.complete && existing.naturalWidth > 0) {
+ resolve(existing);
+ return;
+ }
+
+ if (typeof Image === 'undefined') {
+ const dummy = { naturalWidth: 400, naturalHeight: 300, complete: true } as HTMLImageElement;
+ resolve(dummy);
+ return;
+ }
+
+ const img = existing || new Image();
+ let settled = false;
+
+ const finish = () => {
+ if (!settled) {
+ settled = true;
+ this.cache.set(src, img);
+ resolve(img);
+ for (const listener of this.listeners) {
+ listener();
+ }
+ }
+ };
+
+ img.onload = finish;
+ img.onerror = finish;
+ if (!existing) {
+ img.src = src;
+ this.cache.set(src, img);
+ }
+
+ if (img.complete && img.naturalWidth > 0) {
+ finish();
+ return;
+ }
+
+ if (typeof img.decode === 'function') {
+ img.decode().then(finish).catch(finish);
+ }
+
+ const isTest = typeof navigator !== 'undefined' && /jsdom/i.test(navigator.userAgent);
+ setTimeout(finish, isTest ? 50 : 10000);
+ });
+ }
+
+ public subscribe(listener: () => void): () => void {
+ this.listeners.add(listener);
+ return () => this.listeners.delete(listener);
+ }
+
+ public clear(): void {
+ this.cache.clear();
+ this.listeners.clear();
+ }
+}
+
+export const imageCache = new ImageCache();
+
+export function renderImage(
+ ctx: CanvasRenderingContext2D,
+ el: ImageElement,
+ onLoad?: () => void,
+) {
+ const img = imageCache.getImage(el.src, onLoad);
+
+ ctx.save();
+ if (el.opacity !== undefined) {
+ ctx.globalAlpha = Math.max(0, Math.min(1, el.opacity));
+ }
+
+ if (img && img.complete && img.naturalWidth > 0) {
+ ctx.drawImage(img, el.x, el.y, el.width, el.height);
+ } else {
+ // Subtle translucent placeholder while decoding
+ ctx.strokeStyle = 'rgba(156, 163, 175, 0.4)';
+ ctx.setLineDash([4, 4]);
+ ctx.lineWidth = 1;
+ ctx.strokeRect(el.x, el.y, el.width, el.height);
+ ctx.fillStyle = 'rgba(156, 163, 175, 0.08)';
+ ctx.fillRect(el.x, el.y, el.width, el.height);
+ }
+
+ ctx.restore();
+}
diff --git a/src/core/engine/HitTester.test.ts b/src/core/engine/HitTester.test.ts
index e580511..17d8a7d 100644
--- a/src/core/engine/HitTester.test.ts
+++ b/src/core/engine/HitTester.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import {
CanvasElement,
+ ImageElement,
ShapeElement,
ShapeType,
StrokeElement,
@@ -99,6 +100,49 @@ describe('HitTester', () => {
});
});
+ describe('ImageElement hit testing', () => {
+ const imageEl: ImageElement = {
+ id: 'img1',
+ type: 'image',
+ x: 100,
+ y: 100,
+ width: 200,
+ height: 150,
+ naturalWidth: 400,
+ naturalHeight: 300,
+ aspectRatio: 400 / 300,
+ src: 'data:image/png;base64,123',
+ zIndex: 1,
+ };
+ const imageElements: Record = { img1: imageEl };
+ const imageIds = ['img1'];
+
+ it('detects point hit inside image bounding box', () => {
+ const hit = hitTestPoint(imageElements, imageIds, { x: 150, y: 150 });
+ expect(hit).toBe('img1');
+ });
+
+ it('detects point hit within threshold edge margin', () => {
+ const hit = hitTestPoint(imageElements, imageIds, { x: 95, y: 95 });
+ expect(hit).toBe('img1');
+ });
+
+ it('returns null when point is far from image box', () => {
+ const hit = hitTestPoint(imageElements, imageIds, { x: 500, y: 500 });
+ expect(hit).toBeNull();
+ });
+
+ it('selects image element in marquee intersection', () => {
+ const selected = hitTestMarquee(imageElements, imageIds, {
+ minX: 50,
+ minY: 50,
+ maxX: 150,
+ maxY: 150,
+ });
+ expect(selected).toEqual(['img1']);
+ });
+ });
+
describe('Shape Hit Testing', () => {
it('hit-tests interior of a filled rectangle', () => {
const shapeElements: Record = {
diff --git a/src/core/engine/HitTester.ts b/src/core/engine/HitTester.ts
index e932b7e..d6c5ba0 100644
--- a/src/core/engine/HitTester.ts
+++ b/src/core/engine/HitTester.ts
@@ -79,6 +79,15 @@ export function hitTestPoint(
) {
return el.id;
}
+ } else if (el.type === 'image') {
+ if (
+ point.x >= el.x - threshold
+ && point.x <= el.x + el.width + threshold
+ && point.y >= el.y - threshold
+ && point.y <= el.y + el.height + threshold
+ ) {
+ return el.id;
+ }
}
}
diff --git a/src/core/geometry/transformMath.test.ts b/src/core/geometry/transformMath.test.ts
index 251221a..9320c13 100644
--- a/src/core/geometry/transformMath.test.ts
+++ b/src/core/geometry/transformMath.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { ShapeElement, StrokeElement, TextElement } from '../../types/canvas';
+import { ImageElement, ShapeElement, StrokeElement, TextElement } from '../../types/canvas';
import {
computeSelectionBounds,
getHandlePositions,
@@ -266,4 +266,49 @@ describe('transformMath', () => {
expect(rotated.x).toBeCloseTo(25, 1);
expect(rotated.y).toBeCloseTo(-25, 1);
});
+
+ it('scales an ImageElement proportionally from anchor', () => {
+ const img: ImageElement = {
+ id: 'img-1',
+ type: 'image',
+ x: 100,
+ y: 100,
+ width: 200,
+ height: 100,
+ naturalWidth: 400,
+ naturalHeight: 200,
+ aspectRatio: 2,
+ src: 'data:image/png;base64,123',
+ zIndex: 1,
+ };
+
+ // Scale 2x from top-left anchor (100, 100)
+ const scaled = scaleElement(img, { x: 100, y: 100 }, 2, 2) as ImageElement;
+ expect(scaled.x).toBe(100);
+ expect(scaled.y).toBe(100);
+ expect(scaled.width).toBe(400);
+ expect(scaled.height).toBe(200);
+ });
+
+ it('rotates an ImageElement around center', () => {
+ const img: ImageElement = {
+ id: 'img-1',
+ type: 'image',
+ x: 100,
+ y: 100,
+ width: 200,
+ height: 100,
+ naturalWidth: 400,
+ naturalHeight: 200,
+ aspectRatio: 2,
+ src: 'data:image/png;base64,123',
+ zIndex: 1,
+ };
+
+ // Center is (200, 150). Rotate 90 degrees around center
+ const rotated = rotateElement(img, { x: 200, y: 150 }, Math.PI / 2) as ImageElement;
+ // New center should stay (200, 150)
+ expect(rotated.x + rotated.width / 2).toBeCloseTo(200, 1);
+ expect(rotated.y + rotated.height / 2).toBeCloseTo(150, 1);
+ });
});
diff --git a/src/core/geometry/transformMath.ts b/src/core/geometry/transformMath.ts
index a32f493..98a0734 100644
--- a/src/core/geometry/transformMath.ts
+++ b/src/core/geometry/transformMath.ts
@@ -1,4 +1,4 @@
-import { CanvasElement, ShapeElement, StrokePoint } from '../../types/canvas';
+import { CanvasElement, ImageElement, ShapeElement, StrokePoint } from '../../types/canvas';
import { getFontFamilyString } from '../engine/fontConstants';
import { measureTextBounds } from '../engine/textMeasurement';
@@ -296,9 +296,38 @@ export function scaleElement(
return scaleShape(element, anchor, sx, sy);
}
+ if (element.type === 'image') {
+ return scaleImage(element, anchor, sx, sy);
+ }
+
return element;
}
+export function scaleImage(
+ element: ImageElement,
+ anchor: { x: number; y: number; },
+ sx: number,
+ sy: number,
+): ImageElement {
+ const p1x = anchor.x + (element.x - anchor.x) * sx;
+ const p1y = anchor.y + (element.y - anchor.y) * sy;
+ const p2x = anchor.x + (element.x + element.width - anchor.x) * sx;
+ const p2y = anchor.y + (element.y + element.height - anchor.y) * sy;
+
+ const minX = Math.min(p1x, p2x);
+ const minY = Math.min(p1y, p2y);
+ const maxX = Math.max(p1x, p2x);
+ const maxY = Math.max(p1y, p2y);
+
+ return {
+ ...element,
+ x: Math.round(minX),
+ y: Math.round(minY),
+ width: Math.max(1, Math.round(maxX - minX)),
+ height: Math.max(1, Math.round(maxY - minY)),
+ };
+}
+
export function rotateElement(
element: CanvasElement,
center: { x: number; y: number; },
@@ -338,7 +367,7 @@ export function rotateElement(
points: newPoints,
cachedPath2D: undefined,
};
- } else if (element.type === 'text') {
+ } else if (element.type === 'text' || element.type === 'image') {
const cos = Math.cos(rad);
const sin = Math.sin(rad);
const elCenterX = element.x + element.width / 2;
diff --git a/src/core/persistence/autoSave.ts b/src/core/persistence/autoSave.ts
index 0d10a16..8c49c17 100644
--- a/src/core/persistence/autoSave.ts
+++ b/src/core/persistence/autoSave.ts
@@ -10,9 +10,13 @@ export function setupAutoSave() {
useDocumentStore.subscribe((state) => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
- const camera = useCameraStore.getState().camera;
- const json = serializeScriblDoc(state.elements, state.elementIds, camera);
- localStorage.setItem(AUTOSAVE_KEY, json);
+ try {
+ const camera = useCameraStore.getState().camera;
+ const json = serializeScriblDoc(state.elements, state.elementIds, camera);
+ localStorage.setItem(AUTOSAVE_KEY, json);
+ } catch (err) {
+ console.warn('AutoSave skipped (quota exceeded or storage unavailable):', err);
+ }
}, 400);
});
}
diff --git a/src/core/persistence/fileIO.test.ts b/src/core/persistence/fileIO.test.ts
index b9241f8..12dc49e 100644
--- a/src/core/persistence/fileIO.test.ts
+++ b/src/core/persistence/fileIO.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest';
-import { ShapeElement, StrokeElement, TextElement } from '../../types/canvas';
+import { ImageElement, ShapeElement, StrokeElement, TextElement } from '../../types/canvas';
import { exportToPng, importDrawingDocument, parseScriblJSON, serializeScriblDoc } from './fileIO';
if (typeof Path2D === 'undefined') {
@@ -221,4 +221,52 @@ describe('fileIO', () => {
expect(res.format).toBe('scribl');
expect(res.elements.length).toBe(1);
});
+
+ it('serializes and deserializes documents containing ImageElement and exports to PNG', async () => {
+ const dummyImage: ImageElement = {
+ id: 'img1',
+ type: 'image',
+ x: 10,
+ y: 20,
+ width: 100,
+ height: 80,
+ naturalWidth: 200,
+ naturalHeight: 160,
+ aspectRatio: 1.25,
+ src: 'data:image/png;base64,mockimg',
+ fileName: 'photo.png',
+ mimeType: 'image/png',
+ zIndex: 1,
+ };
+
+ const json = serializeScriblDoc({ img1: dummyImage }, ['img1'], { x: 0, y: 0, zoom: 1 });
+ const parsed = parseScriblJSON(json);
+
+ expect(parsed.elementIds).toEqual(['img1']);
+ const imgEl = parsed.elements['img1'] as ImageElement;
+ expect(imgEl.type).toBe('image');
+ expect(imgEl.src).toBe('data:image/png;base64,mockimg');
+ expect(imgEl.aspectRatio).toBe(1.25);
+ expect(imgEl.fileName).toBe('photo.png');
+
+ const mockCtx = {
+ scale: vi.fn(),
+ translate: vi.fn(),
+ save: vi.fn(),
+ restore: vi.fn(),
+ drawImage: vi.fn(),
+ strokeRect: vi.fn(),
+ fillRect: vi.fn(),
+ setLineDash: vi.fn(),
+ };
+ vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue(
+ mockCtx as unknown as CanvasRenderingContext2D,
+ );
+ vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue(
+ 'data:image/png;base64,mockpngdata',
+ );
+
+ const pngUrl = await exportToPng({ img1: dummyImage }, ['img1']);
+ expect(pngUrl).toBe('data:image/png;base64,mockpngdata');
+ });
});
diff --git a/src/core/persistence/fileIO.ts b/src/core/persistence/fileIO.ts
index 647eec6..4b09a18 100644
--- a/src/core/persistence/fileIO.ts
+++ b/src/core/persistence/fileIO.ts
@@ -1,5 +1,5 @@
import { Camera, CanvasElement } from '../../types/canvas';
-import { renderShape, renderStroke, renderText } from '../engine/CanvasRenderer';
+import { renderImage, renderShape, renderStroke, renderText } from '../engine/CanvasRenderer';
import { ThemeMode } from '../theme/themeManager';
export * from './importers';
@@ -125,6 +125,8 @@ export function exportToPng(
renderShape(ctx, el, themeMode);
} else if (el.type === 'text') {
renderText(ctx, el, themeMode);
+ } else if (el.type === 'image') {
+ renderImage(ctx, el);
}
}
diff --git a/src/core/persistence/importers/excalidrawImporter.test.ts b/src/core/persistence/importers/excalidrawImporter.test.ts
index 1df86a5..6fae16c 100644
--- a/src/core/persistence/importers/excalidrawImporter.test.ts
+++ b/src/core/persistence/importers/excalidrawImporter.test.ts
@@ -167,4 +167,40 @@ describe('importExcalidraw', () => {
expect(result.discardedCount).toBe(3);
expect(result.discardedTypes).toEqual(expect.arrayContaining(['image', 'frame', 'embeddable']));
});
+
+ it('imports image element when fileId matches files map', () => {
+ const excalidrawDoc = {
+ elements: [
+ {
+ id: 'img-el-1',
+ type: 'image',
+ fileId: 'file-123',
+ x: 100,
+ y: 100,
+ width: 300,
+ height: 200,
+ },
+ ],
+ files: {
+ 'file-123': {
+ id: 'file-123',
+ mimeType: 'image/png',
+ dataURL: 'data:image/png;base64,samplepngdata',
+ created: 1234567,
+ },
+ },
+ };
+
+ const result = importExcalidraw(excalidrawDoc);
+ expect(result.elements.length).toBe(1);
+ expect(result.discardedCount).toBe(0);
+ const imgEl = result.elements[0];
+ expect(imgEl.type).toBe('image');
+ if (imgEl.type === 'image') {
+ expect(imgEl.src).toBe('data:image/png;base64,samplepngdata');
+ expect(imgEl.width).toBe(300);
+ expect(imgEl.height).toBe(200);
+ expect(imgEl.aspectRatio).toBe(1.5);
+ }
+ });
});
diff --git a/src/core/persistence/importers/excalidrawImporter.ts b/src/core/persistence/importers/excalidrawImporter.ts
index 3d44c85..536a7b2 100644
--- a/src/core/persistence/importers/excalidrawImporter.ts
+++ b/src/core/persistence/importers/excalidrawImporter.ts
@@ -1,6 +1,7 @@
import {
BoundingBox,
CanvasElement,
+ ImageElement,
ShapeElement,
StrokeElement,
StrokePoint,
@@ -26,6 +27,15 @@ interface ExcalidrawElement {
fontSize?: number;
lineHeight?: number;
isDeleted?: boolean;
+ fileId?: string;
+}
+
+interface ExcalidrawFile {
+ id?: string;
+ mimeType?: string;
+ dataURL?: string;
+ created?: number;
+ lastRetrieved?: number;
}
interface ExcalidrawDocument {
@@ -33,6 +43,7 @@ interface ExcalidrawDocument {
version?: number;
elements?: ExcalidrawElement[];
appState?: { name?: string; };
+ files?: Record;
}
export function importExcalidraw(
@@ -232,6 +243,36 @@ export function importExcalidraw(
break;
}
+ case 'image': {
+ const fileId = el.fileId;
+ const fileData = fileId && doc.files ? doc.files[fileId] : undefined;
+ if (fileData && fileData.dataURL) {
+ const width = el.width || 200;
+ const height = el.height || 200;
+ const imageEl: ImageElement = {
+ id: elId,
+ type: 'image',
+ x: el.x,
+ y: el.y,
+ width,
+ height,
+ naturalWidth: width,
+ naturalHeight: height,
+ aspectRatio: width / height,
+ src: fileData.dataURL,
+ mimeType: fileData.mimeType || 'image/png',
+ zIndex,
+ };
+ elements.push(imageEl);
+ elementIds.push(elId);
+ updateBounds(el.x, el.y, width, height);
+ } else {
+ discardedCount++;
+ discardedTypesSet.add('image');
+ }
+ break;
+ }
+
default: {
discardedCount++;
if (el.type) {
diff --git a/src/types/canvas.test.ts b/src/types/canvas.test.ts
index a9f75cb..75c14ff 100644
--- a/src/types/canvas.test.ts
+++ b/src/types/canvas.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import {
CanvasElement,
+ ImageElement,
ShapeElement,
ShapeType,
StrokeElement,
@@ -125,4 +126,33 @@ describe('canvas types', () => {
expect(types).toEqual(['stroke:1', 'shape:circle']);
});
+
+ it('instantiates a valid ImageElement and includes it in CanvasElement union', () => {
+ const image: ImageElement = {
+ id: 'img-1',
+ type: 'image',
+ x: 100,
+ y: 100,
+ width: 400,
+ height: 300,
+ naturalWidth: 800,
+ naturalHeight: 600,
+ aspectRatio: 800 / 600,
+ opacity: 0.9,
+ src:
+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
+ fileName: 'test.png',
+ mimeType: 'image/png',
+ zIndex: 3,
+ locked: false,
+ };
+
+ const element: CanvasElement = image;
+ expect(element.type).toBe('image');
+ if (element.type === 'image') {
+ expect(element.src).toContain('data:image/png;base64');
+ expect(element.aspectRatio).toBeCloseTo(1.333, 2);
+ expect(element.opacity).toBe(0.9);
+ }
+ });
});
diff --git a/src/types/canvas.ts b/src/types/canvas.ts
index 2a10356..df0580a 100644
--- a/src/types/canvas.ts
+++ b/src/types/canvas.ts
@@ -59,7 +59,18 @@ export interface TextElement extends BaseElement {
lineHeight: number;
}
-export type CanvasElement = StrokeElement | ShapeElement | TextElement;
+export interface ImageElement extends BaseElement {
+ type: 'image';
+ src: string;
+ naturalWidth: number;
+ naturalHeight: number;
+ aspectRatio: number;
+ opacity?: number;
+ fileName?: string;
+ mimeType?: string;
+}
+
+export type CanvasElement = StrokeElement | ShapeElement | TextElement | ImageElement;
export type ToolType =
| 'select'
From fc78769041ef9c69d7f798f0ed67bba4e3d9e0b8 Mon Sep 17 00:00:00 2001
From: Milan Pramod
Date: Sun, 15 Nov 2026 00:05:25 +0530
Subject: [PATCH 2/2] perf(canvas): optimize image decode pipeline and pre-load
document images
---
src/core/clipboard/clipboard.ts | 93 ++++++++++++++++++++++++++++---
src/core/engine/CanvasRenderer.ts | 6 ++
src/core/persistence/fileIO.ts | 29 ++++++++--
src/stores/useDocumentStore.ts | 8 +++
4 files changed, 123 insertions(+), 13 deletions(-)
diff --git a/src/core/clipboard/clipboard.ts b/src/core/clipboard/clipboard.ts
index a265b93..3795d77 100644
--- a/src/core/clipboard/clipboard.ts
+++ b/src/core/clipboard/clipboard.ts
@@ -158,13 +158,92 @@ export function getImageDimensions(src: string): Promise<{ width: number; height
}));
}
+/**
+ * Optimize an image blob for canvas performance:
+ * - Uses URL.createObjectURL for near-instant native decode (bypassing slow base64 FileReader)
+ * - Constrains ultra-high-res images (e.g. 48MP smartphone photos) to max 2560px
+ * - Pre-populates imageCache so first render draws immediately with 0ms delay
+ */
+export async function optimizeImageBlob(blob: Blob): Promise<{
+ dataUrl: string;
+ width: number;
+ height: number;
+}> {
+ const isSvg = blob.type === 'image/svg+xml'
+ || (blob as File).name?.toLowerCase().endsWith('.svg');
+
+ let objectUrl = '';
+ if (typeof URL !== 'undefined' && typeof URL.createObjectURL === 'function') {
+ try {
+ objectUrl = URL.createObjectURL(blob);
+ } catch {
+ objectUrl = '';
+ }
+ }
+
+ try {
+ const srcToLoad = objectUrl || (await readBlobAsDataUrl(blob));
+ const img = await imageCache.loadImage(srcToLoad);
+
+ const naturalWidth = img.naturalWidth || 400;
+ const naturalHeight = img.naturalHeight || 300;
+
+ const MAX_DIMENSION = 2560;
+ const needsDownscale = !isSvg
+ && (naturalWidth > MAX_DIMENSION || naturalHeight > MAX_DIMENSION);
+
+ let dataUrl: string;
+
+ if (needsDownscale && typeof document !== 'undefined') {
+ const scale = Math.min(MAX_DIMENSION / naturalWidth, MAX_DIMENSION / naturalHeight);
+ const targetW = Math.round(naturalWidth * scale);
+ const targetH = Math.round(naturalHeight * scale);
+
+ const canvas = document.createElement('canvas');
+ canvas.width = targetW;
+ canvas.height = targetH;
+ const ctx = canvas.getContext('2d');
+ if (ctx) {
+ ctx.imageSmoothingEnabled = true;
+ ctx.imageSmoothingQuality = 'high';
+ ctx.drawImage(img, 0, 0, targetW, targetH);
+
+ const isJpeg = blob.type === 'image/jpeg' || (blob as File).name?.match(/\.jpe?g$/i);
+ const format = isJpeg ? 'image/jpeg' : 'image/png';
+ const quality = isJpeg ? 0.90 : undefined;
+ dataUrl = canvas.toDataURL(format, quality);
+ } else {
+ dataUrl = await readBlobAsDataUrl(blob);
+ }
+ } else {
+ dataUrl = await readBlobAsDataUrl(blob);
+ }
+
+ // Pre-populate imageCache with dataUrl pointing to the loaded HTMLImageElement
+ imageCache.set(dataUrl, img);
+
+ return {
+ dataUrl,
+ width: naturalWidth,
+ height: naturalHeight,
+ };
+ } finally {
+ if (objectUrl) {
+ try {
+ URL.revokeObjectURL(objectUrl);
+ } catch {
+ // Ignore
+ }
+ }
+ }
+}
+
export async function createImageElementFromBlob(
blob: Blob,
worldPos?: { x: number; y: number; },
fileName?: string,
): Promise {
- const dataUrl = await readBlobAsDataUrl(blob);
- const dims = await getImageDimensions(dataUrl);
+ const { dataUrl, width: naturalWidth, height: naturalHeight } = await optimizeImageBlob(blob);
const cameraState = useCameraStore.getState();
const camera = cameraState.camera;
@@ -175,9 +254,9 @@ export async function createImageElementFromBlob(
const maxWorldW = (viewportWidth / camera.zoom) * 0.5;
const maxWorldH = (viewportHeight / camera.zoom) * 0.5;
- let width = dims.width;
- let height = dims.height;
- const aspectRatio = dims.width && dims.height ? dims.width / dims.height : 1;
+ let width = naturalWidth;
+ let height = naturalHeight;
+ const aspectRatio = naturalWidth && naturalHeight ? naturalWidth / naturalHeight : 1;
if (width > maxWorldW || height > maxWorldH) {
const scale = Math.min(maxWorldW / width, maxWorldH / height);
@@ -202,8 +281,8 @@ export async function createImageElementFromBlob(
y,
width: Math.max(1, width),
height: Math.max(1, height),
- naturalWidth: dims.width,
- naturalHeight: dims.height,
+ naturalWidth,
+ naturalHeight,
aspectRatio,
src: dataUrl,
opacity: 1,
diff --git a/src/core/engine/CanvasRenderer.ts b/src/core/engine/CanvasRenderer.ts
index f3afc97..47e123f 100644
--- a/src/core/engine/CanvasRenderer.ts
+++ b/src/core/engine/CanvasRenderer.ts
@@ -186,6 +186,10 @@ export class ImageCache {
private cache = new Map();
private listeners = new Set<() => void>();
+ public set(src: string, img: HTMLImageElement): void {
+ this.cache.set(src, img);
+ }
+
public getImage(src: string, onLoad?: () => void): HTMLImageElement | null {
const existing = this.cache.get(src);
if (existing) {
@@ -203,6 +207,7 @@ export class ImageCache {
}
const img = new Image();
+ img.decoding = 'async';
img.src = src;
this.cache.set(src, img);
@@ -241,6 +246,7 @@ export class ImageCache {
}
const img = existing || new Image();
+ img.decoding = 'async';
let settled = false;
const finish = () => {
diff --git a/src/core/persistence/fileIO.ts b/src/core/persistence/fileIO.ts
index 4b09a18..a613e3f 100644
--- a/src/core/persistence/fileIO.ts
+++ b/src/core/persistence/fileIO.ts
@@ -1,5 +1,11 @@
import { Camera, CanvasElement } from '../../types/canvas';
-import { renderImage, renderShape, renderStroke, renderText } from '../engine/CanvasRenderer';
+import {
+ imageCache,
+ renderImage,
+ renderShape,
+ renderStroke,
+ renderText,
+} from '../engine/CanvasRenderer';
import { ThemeMode } from '../theme/themeManager';
export * from './importers';
@@ -73,17 +79,28 @@ export function triggerDownload(
URL.revokeObjectURL(url);
}
-export function exportToPng(
+export async function exportToPng(
elements: Record,
elementIds: string[],
themeMode?: ThemeMode,
): Promise {
- return new Promise((resolve, reject) => {
- if (elementIds.length === 0) {
- reject(new Error('Canvas is empty'));
- return;
+ if (elementIds.length === 0) {
+ throw new Error('Canvas is empty');
+ }
+
+ // Pre-load all image elements to ensure export has all bitmaps decoded
+ const imagePromises: Promise[] = [];
+ for (const id of elementIds) {
+ const el = elements[id];
+ if (el && el.type === 'image' && el.src) {
+ imagePromises.push(imageCache.loadImage(el.src).catch(() => {}));
}
+ }
+ if (imagePromises.length > 0) {
+ await Promise.all(imagePromises);
+ }
+ return new Promise((resolve, reject) => {
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
diff --git a/src/stores/useDocumentStore.ts b/src/stores/useDocumentStore.ts
index 132f881..4d6ede8 100644
--- a/src/stores/useDocumentStore.ts
+++ b/src/stores/useDocumentStore.ts
@@ -1,4 +1,5 @@
import { create } from 'zustand';
+import { imageCache } from '../core/engine/CanvasRenderer';
import { getFontFamilyId, getFontFamilyString } from '../core/engine/fontConstants';
import { measureTextBounds } from '../core/engine/textMeasurement';
import {
@@ -332,6 +333,13 @@ export const useDocumentStore = create((set, get) => ({
canUndo: false,
canRedo: false,
});
+ // Pre-warm image cache in background for all images in the loaded document
+ for (const id of data.elementIds) {
+ const el = data.elements[id];
+ if (el && el.type === 'image' && el.src) {
+ imageCache.loadImage(el.src).catch(() => {});
+ }
+ }
},
clearDocument: () => {