From 474ddd3e272714b3a5126f9500db55d5c3cb0c90 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 22 Nov 2025 02:58:54 +0100 Subject: [PATCH] Perf. fix --- src/components/SpriteCanvas.vue | 37 ++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/components/SpriteCanvas.vue b/src/components/SpriteCanvas.vue index 776cd8a..74ec52c 100644 --- a/src/components/SpriteCanvas.vue +++ b/src/components/SpriteCanvas.vue @@ -6,7 +6,7 @@
- +
@@ -130,6 +130,17 @@ const canvasRef = ref(null); + // rAF-based draw scheduler to coalesce multiple draw requests into a single frame + // Define before usage to avoid TDZ issues when passing into composables during setup + let rafId: number | null = null; + function requestDraw() { + if (rafId !== null) return; + rafId = requestAnimationFrame(() => { + rafId = null; + drawCanvas(); + }); + } + // Initialize composables const canvas2D = useCanvas2D(canvasRef); @@ -169,7 +180,7 @@ getMousePosition: (event, z) => canvas2D.getMousePosition(event, z), onUpdateSprite: (id, x, y) => emit('updateSprite', id, x, y), onUpdateSpriteCell: (id, newIndex) => emit('updateSpriteCell', id, newIndex), - onDraw: drawCanvas, + onDraw: requestDraw, }); const activeSprites = computed(() => props.layers.find(l => l.id === props.activeLayerId)?.sprites ?? []); @@ -374,7 +385,7 @@ onMounted(() => { canvas2D.initContext(); - drawCanvas(); + requestDraw(); // Attach listeners for current sprites attachImageListeners(); @@ -393,24 +404,26 @@ // Handler for force redraw event const handleForceRedraw = () => { - canvas2D.ensureIntegerPositions(props.sprites); + // Ensure integer positioning for crisp rendering on the active layer + canvas2D.ensureIntegerPositions(activeSprites.value); canvas2D.applySmoothing(); - drawCanvas(); + requestDraw(); }; + // Re-attach listeners and redraw whenever layers/sprites change watch( - () => props.sprites, + () => props.layers, () => { attachImageListeners(); - drawCanvas(); + requestDraw(); }, { deep: true } ); - watch(() => props.columns, drawCanvas); - watch(() => settingsStore.pixelPerfect, drawCanvas); - watch(() => settingsStore.darkMode, drawCanvas); - watch(() => settingsStore.negativeSpacingEnabled, drawCanvas); - watch(showAllSprites, drawCanvas); + watch(() => props.columns, requestDraw); + watch(() => settingsStore.pixelPerfect, requestDraw); + watch(() => settingsStore.darkMode, requestDraw); + watch(() => settingsStore.negativeSpacingEnabled, requestDraw); + watch(showAllSprites, requestDraw);