diff --git a/src/composables/useExport.ts b/src/composables/useExport.ts index 8a63eb5..5fbe1b3 100644 --- a/src/composables/useExport.ts +++ b/src/composables/useExport.ts @@ -2,11 +2,12 @@ import type { Ref } from 'vue'; import GIF from 'gif.js'; import gifWorkerUrl from 'gif.js/dist/gif.worker.js?url'; import JSZip from 'jszip'; -import type { Sprite } from '../types/sprites'; +import type { Layer, Sprite } from '../types/sprites'; import { getMaxDimensions } from './useSprites'; import { calculateNegativeSpacing } from './useNegativeSpacing'; +import { getMaxDimensionsAcrossLayers } from './useLayers'; -export const useExport = (sprites: Ref, columns: Ref, negativeSpacingEnabled: Ref, backgroundColor?: Ref, manualCellSizeEnabled?: Ref, manualCellWidth?: Ref, manualCellHeight?: Ref) => { +export const useExport = (sprites: Ref, columns: Ref, negativeSpacingEnabled: Ref, backgroundColor?: Ref, manualCellSizeEnabled?: Ref, manualCellWidth?: Ref, manualCellHeight?: Ref, layers?: Ref) => { const getCellDimensions = () => { // If manual cell size is enabled, use manual values if (manualCellSizeEnabled?.value) { @@ -17,9 +18,14 @@ export const useExport = (sprites: Ref, columns: Ref, negative }; } - // Otherwise, calculate from sprite dimensions - const { maxWidth, maxHeight } = getMaxDimensions(sprites.value); - const negativeSpacing = calculateNegativeSpacing(sprites.value, negativeSpacingEnabled.value); + // Use dimensions from ALL layers to keep canvas size stable when hiding layers + // Fall back to current sprites if layers ref is not provided + const { maxWidth, maxHeight } = layers?.value ? getMaxDimensionsAcrossLayers(layers.value, false) : getMaxDimensions(sprites.value); + + // Calculate negative spacing from all layers' sprites for consistency + const allSprites = layers?.value ? layers.value.flatMap(l => l.sprites) : sprites.value; + const negativeSpacing = calculateNegativeSpacing(allSprites, negativeSpacingEnabled.value); + return { cellWidth: maxWidth + negativeSpacing, cellHeight: maxHeight + negativeSpacing, diff --git a/src/composables/useExportLayers.ts b/src/composables/useExportLayers.ts index 90dee94..c474220 100644 --- a/src/composables/useExportLayers.ts +++ b/src/composables/useExportLayers.ts @@ -23,7 +23,8 @@ export const useExportLayers = (layersRef: Ref, columns: Ref, n // Otherwise, calculate from sprite dimensions across ALL layers (same as canvas) // This ensures export dimensions match what's shown in the canvas const { maxWidth, maxHeight } = getMaxDimensionsAcrossLayers(layersRef.value); - const allSprites = getVisibleLayers().flatMap(l => l.sprites); + // Calculate negative spacing from ALL layers (not just visible) to keep canvas size stable + const allSprites = layersRef.value.flatMap(l => l.sprites); const negativeSpacing = calculateNegativeSpacing(allSprites, negativeSpacingEnabled.value); return { cellWidth: maxWidth + negativeSpacing,