AI-native design editor. Open-source Figma alternative.
Optimize zoom/pan smoothness with shallowReactive, useRafFn, and input coalescing
Danila Poyarkova5fe5b0cf4f2
diff --git a/src/components/properties/TypographySection.vue b/src/components/properties/TypographySection.vueindex 5971bdb45..f10e381dd 100644--- a/src/components/properties/TypographySection.vue+++ b/src/components/properties/TypographySection.vue@@ -81,7 +81,9 @@ onMounted(async () => {<icon-lucide-alert-trianglev-if="hasMissingFonts"class="size-3.5 shrink-0 text-amber-400"- :title="'Missing font' + (missingFonts.length > 1 ? 's' : '') + ': ' + missingFonts.join(', ')"+ :title="+ 'Missing font' + (missingFonts.length > 1 ? 's' : '') + ': ' + missingFonts.join(', ')+ "/></div>diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.tsindex 0bd09c866..79fc45314 100644--- a/src/composables/use-canvas-input.ts+++ b/src/composables/use-canvas-input.ts@@ -919,6 +919,29 @@ export function useCanvasInput(cursorOverride.value = null}+ let wheelAccum = {+ deltaX: 0,+ deltaY: 0,+ zoomDelta: 0,+ zoomCenterX: 0,+ zoomCenterY: 0,+ hasZoom: false,+ rafId: 0+ }++ function flushWheel() {+ wheelAccum.rafId = 0+ if (wheelAccum.hasZoom) {+ store.applyZoom(wheelAccum.zoomDelta, wheelAccum.zoomCenterX, wheelAccum.zoomCenterY)+ } else {+ store.pan(wheelAccum.deltaX, wheelAccum.deltaY)+ }+ wheelAccum.deltaX = 0+ wheelAccum.deltaY = 0+ wheelAccum.zoomDelta = 0+ wheelAccum.hasZoom = false+ }+function onWheel(e: WheelEvent) {e.preventDefault()const canvas = canvasRef.value@@ -926,11 +949,16 @@ export function useCanvasInput(if (e.ctrlKey || e.metaKey) {const rect = canvas.getBoundingClientRect()- const sx = e.clientX - rect.left- const sy = e.clientY - rect.top- store.applyZoom(e.deltaY, sx, sy)+ wheelAccum.zoomCenterX = e.clientX - rect.left+ wheelAccum.zoomCenterY = e.clientY - rect.top+ wheelAccum.zoomDelta += e.deltaY+ wheelAccum.hasZoom = true} else {- store.pan(-e.deltaX, -e.deltaY)+ wheelAccum.deltaX -= e.deltaX+ wheelAccum.deltaY -= e.deltaY+ }+ if (!wheelAccum.rafId) {+ wheelAccum.rafId = requestAnimationFrame(flushWheel)}}@@ -1161,6 +1189,22 @@ export function useCanvasInput(// Safari macOS: trackpad pinch-to-zoom uses gesture events, not wheel+ctrlKeylet gestureStartZoom = 1+ let gestureRafId = 0+ let pendingGesture: { scale: number; sx: number; sy: number } | null = null++ function flushGesture() {+ gestureRafId = 0+ if (!pendingGesture) return+ const { scale, sx, sy } = pendingGesture+ pendingGesture = null+ const newZoom = Math.max(0.02, Math.min(256, gestureStartZoom * scale))+ const zoomRatio = newZoom / store.state.zoom+ store.state.panX = sx - (sx - store.state.panX) * zoomRatio+ store.state.panY = sy - (sy - store.state.panY) * zoomRatio+ store.state.zoom = newZoom+ store.requestRepaint()+ }+useEventListener(canvasRef,'gesturestart' as keyof HTMLElementEventMap,@@ -1179,14 +1223,14 @@ export function useCanvasInput(const canvas = canvasRef.valueif (!canvas) returnconst rect = canvas.getBoundingClientRect()- const sx = (ge.clientX ?? rect.width / 2) - rect.left- const sy = (ge.clientY ?? rect.height / 2) - rect.top- const newZoom = Math.max(0.02, Math.min(256, gestureStartZoom * ge.scale))- const zoomRatio = newZoom / store.state.zoom- store.state.panX = sx - (sx - store.state.panX) * zoomRatio- store.state.panY = sy - (sy - store.state.panY) * zoomRatio- store.state.zoom = newZoom- store.requestRepaint()+ pendingGesture = {+ scale: ge.scale,+ sx: (ge.clientX ?? rect.width / 2) - rect.left,+ sy: (ge.clientY ?? rect.height / 2) - rect.top+ }+ if (!gestureRafId) {+ gestureRafId = requestAnimationFrame(flushGesture)+ }},{ passive: false })diff --git a/src/composables/use-canvas.ts b/src/composables/use-canvas.tsindex 803d65fd3..c35f0451a 100644--- a/src/composables/use-canvas.ts+++ b/src/composables/use-canvas.ts@@ -1,5 +1,5 @@-import { useResizeObserver } from '@vueuse/core'-import { onMounted, onUnmounted, watch, type Ref } from 'vue'+import { useRafFn, useResizeObserver } from '@vueuse/core'+import { onMounted, onUnmounted, type Ref } from 'vue'import { getCanvasKit } from '@/engine/canvaskit'import { SkiaRenderer } from '@/engine/renderer'@@ -11,6 +11,9 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Editolet renderer: SkiaRenderer | null = nulllet ck: CanvasKit | null = nulllet destroyed = false+ let dirty = true+ let lastRenderVersion = -1+ let lastSelectedIds: Set<string> | null = nullasync function init() {const canvas = canvasRef.value@@ -62,8 +65,6 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Editoconst params = new URLSearchParams(window.location.search)const showRulers = !params.has('no-rulers')- let rafId = 0-function renderNow() {if (!renderer) returnrenderer.dpr = window.devicePixelRatio || 1@@ -98,15 +99,18 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Edito},store.state.sceneVersion)+ lastRenderVersion = store.state.renderVersion+ lastSelectedIds = store.state.selectedIds}- function render() {- if (rafId) return- rafId = requestAnimationFrame(() => {- rafId = 0+ const { pause } = useRafFn(() => {+ const versionChanged = store.state.renderVersion !== lastRenderVersion+ const selectionChanged = store.state.selectedIds !== lastSelectedIds+ if (dirty || versionChanged || selectionChanged) {+ dirty = falserenderNow()- })- }+ }+ })onMounted(() => {init()@@ -114,7 +118,7 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: EditoonUnmounted(() => {destroyed = true- cancelAnimationFrame(rafId)+ pause()cancelAnimationFrame(resizeRaf)renderer?.destroy()})@@ -129,16 +133,6 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Edito})})- watch(- () => store.state.renderVersion,- () => render()- )-- watch(- () => store.state.selectedIds,- () => render()- )-function hitTestSectionTitle(canvasX: number, canvasY: number) {return renderer?.hitTestSectionTitle(store.graph, canvasX, canvasY) ?? null}@@ -147,5 +141,11 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Editoreturn renderer?.hitTestComponentLabel(store.graph, canvasX, canvasY) ?? null}- return { render, hitTestSectionTitle, hitTestComponentLabel }+ return {+ render: () => {+ dirty = true+ },+ hitTestSectionTitle,+ hitTestComponentLabel+ }}diff --git a/src/stores/editor.ts b/src/stores/editor.tsindex 91b8df080..e3a340d22 100644--- a/src/stores/editor.ts+++ b/src/stores/editor.ts@@ -1,4 +1,4 @@-import { reactive, shallowRef, computed, watch } from 'vue'+import { shallowReactive, shallowRef, computed, watch } from 'vue'import {IS_TAURI,@@ -142,7 +142,7 @@ export function createEditorStore() {}, 100)}- const state = reactive({+ const state = shallowReactive({activeTool: 'SELECT' as Tool,currentPageId: graph.getPages()[0].id,selectedIds: new Set<string>(),@@ -1446,7 +1446,9 @@ export function createEditorStore() {},inverse: () => {graph.deleteNode(id)- state.selectedIds.delete(id)+ const next = new Set(state.selectedIds)+ next.delete(id)+ state.selectedIds = nextrequestRender()}})