1
0
forked from noxious/server

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 {
const parsedSpriteActions = validateSpriteActions(data.spriteActions)
// First, process sprites to get dimensions
const processedActions = await processSprites(parsedSpriteActions)
await updateDatabase(data.id, data.name, processedActions)
await saveSpritesToDisk(data.id, processedActions)
// Find the maximum dimensions across all actions
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)
} catch (error) {
@ -129,7 +141,6 @@ export default class SpriteUpdateEvent {
await Promise.all(
processedActions.map(async ({ action, buffersWithDimensions, frameWidth, frameHeight }) => {
// Get and validate all frame dimensions first
const frames = await Promise.all(
buffersWithDimensions.map(async ({ buffer }) => {
const metadata = await sharp(buffer).metadata()
@ -152,10 +163,9 @@ export default class SpriteUpdateEvent {
.composite(
frames.map(({ buffer, width, height }, index) => ({
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),
// Top position is always 0
top: 0
top: 0,
}))
)
.png()