Put all images in a container size as big as the biggest sprite that was found

This commit is contained in:
Dennis Postma 2024-12-15 20:57:54 +01:00
parent 9467797dc9
commit 54c75896f9

View File

@ -46,10 +46,22 @@ export default class SpriteUpdateEvent {
try { try {
const parsedSpriteActions = validateSpriteActions(data.spriteActions) const parsedSpriteActions = validateSpriteActions(data.spriteActions)
// First, process sprites to get dimensions
const processedActions = await processSprites(parsedSpriteActions) const processedActions = await processSprites(parsedSpriteActions)
await updateDatabase(data.id, data.name, processedActions) // Find the maximum dimensions across all actions
await saveSpritesToDisk(data.id, processedActions) const maxFrameWidth = Math.max(...processedActions.map(action => action.frameWidth))
const maxFrameHeight = Math.max(...processedActions.map(action => action.frameHeight))
// Update all actions to use the maximum dimensions
const normalizedActions = processedActions.map(action => ({
...action,
frameWidth: maxFrameWidth,
frameHeight: maxFrameHeight
}))
await updateDatabase(data.id, data.name, normalizedActions)
await saveSpritesToDisk(data.id, normalizedActions)
callback(true) callback(true)
} catch (error) { } catch (error) {
@ -129,7 +141,6 @@ export default class SpriteUpdateEvent {
await Promise.all( await Promise.all(
processedActions.map(async ({ action, buffersWithDimensions, frameWidth, frameHeight }) => { processedActions.map(async ({ action, buffersWithDimensions, frameWidth, frameHeight }) => {
// Get and validate all frame dimensions first
const frames = await Promise.all( const frames = await Promise.all(
buffersWithDimensions.map(async ({ buffer }) => { buffersWithDimensions.map(async ({ buffer }) => {
const metadata = await sharp(buffer).metadata() const metadata = await sharp(buffer).metadata()
@ -152,10 +163,9 @@ export default class SpriteUpdateEvent {
.composite( .composite(
frames.map(({ buffer, width, height }, index) => ({ frames.map(({ buffer, width, height }, index) => ({
input: buffer, input: buffer,
// Center horizontally based on the exact middle of each frame // Center horizontally and vertically in the frame
left: (index * frameWidth) + ((frameWidth - width) >> 1), left: (index * frameWidth) + ((frameWidth - width) >> 1),
// Top position is always 0 top: 0,
top: 0
})) }))
) )
.png() .png()