-
+
@@ -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);