All works now

This commit is contained in:
Dennis Postma 2025-04-04 02:55:56 +02:00
parent 354d96274a
commit d6262903bf
2 changed files with 25 additions and 31 deletions

View File

@ -204,9 +204,8 @@
const resizeStart = ref({ x: 0, y: 0 });
const currentFrameDisplay = computed(() => {
const totalFrames = Math.max(1, sprites.value.length);
const frame = Math.min(currentFrame.value + 1, totalFrames);
return `${frame} / ${totalFrames}`;
if (sprites.value.length === 0) return '0 / 0';
return `${currentFrame.value + 1} / ${sprites.value.length}`;
});
// Computed property to check if sprite has been moved from original position
@ -369,21 +368,22 @@
};
const handleFrameChange = () => {
// Stop any running animation
if (animation.value.isPlaying) {
stopAnimation();
}
// Ensure frame is within bounds
currentFrame.value = Math.max(0, Math.min(currentFrame.value, sprites.value.length - 1));
updateFrame();
};
const updateFrame = () => {
// Ensure frame is within bounds
currentFrame.value = Math.max(0, Math.min(currentFrame.value, sprites.value.length - 1));
animation.value.currentFrame = currentFrame.value;
animation.value.manualUpdate = true;
// Get the frame-specific offset
const frameOffset = store.getSpriteOffset(currentFrame.value);
// Render with the frame-specific offset
store.renderAnimationFrame(currentFrame.value, showAllSprites.value, frameOffset);
};
@ -573,20 +573,31 @@
const deltaX = e.clientX - canvasDragStart.value.x;
const deltaY = e.clientY - canvasDragStart.value.y;
// Update sprite offset with the delta, scaled by zoom level
// Use requestAnimationFrame for smoother dragging
requestAnimationFrame(() => {
// Get the frame-specific offset
const frameOffset = store.getSpriteOffset(currentFrame.value);
// Update both the current offset and the frame-specific offset
// Calculate new position
const newX = frameOffset.x + deltaX / previewZoom.value;
const newY = frameOffset.y + deltaY / previewZoom.value;
frameOffset.x = newX;
frameOffset.y = newY;
store.currentSpriteOffset.x = newX;
store.currentSpriteOffset.y = newY;
// Get current sprite
const currentSprite = sprites.value[currentFrame.value];
if (!currentSprite) return;
// Calculate maximum allowed offset based on sprite and cell size
const maxOffsetX = (store.cellSize.width - currentSprite.width) / 2;
const maxOffsetY = (store.cellSize.height - currentSprite.height) / 2;
// Constrain movement to stay within cell boundaries, preventing negative offsets
const constrainedX = Math.max(0, Math.min(maxOffsetX, newX));
const constrainedY = Math.max(0, Math.min(maxOffsetY, newY));
// Update both the current offset and the frame-specific offset
frameOffset.x = constrainedX;
frameOffset.y = constrainedY;
store.currentSpriteOffset.x = constrainedX;
store.currentSpriteOffset.y = constrainedY;
// Reset drag start position
canvasDragStart.value = {

View File

@ -530,23 +530,6 @@ export function useSpritesheetStore() {
const offsetX = originalOffsetX + spriteOffset.x;
const offsetY = originalOffsetY + spriteOffset.y;
// If the sprite has been moved from its original position, draw a faint outline at the original position
if (spriteOffset.x !== 0 || spriteOffset.y !== 0) {
// Save context state
animation.ctx.save();
// Draw a faint outline or indicator at the original position
animation.ctx.globalAlpha = 0.2;
animation.ctx.strokeStyle = '#00FFFF'; // Cyan color for the original position indicator
animation.ctx.lineWidth = 1;
// Draw a rectangle representing the sprite's original position
animation.ctx.strokeRect(originalOffsetX, originalOffsetY, currentSprite.width, currentSprite.height);
// Restore context state
animation.ctx.restore();
}
// Draw the current sprite at full opacity at the new position
animation.ctx.drawImage(currentSprite.img, offsetX, offsetY);
@ -680,6 +663,6 @@ export function useSpritesheetStore() {
zoomIn,
zoomOut,
resetZoom,
applyOffsetsToMainView
applyOffsetsToMainView,
};
}