1
0
forked from noxious/server
This commit is contained in:
Dennis Postma 2024-12-15 20:25:30 +01:00
parent a8934f8e40
commit 9467797dc9

View File

@ -129,41 +129,32 @@ export default class SpriteUpdateEvent {
await Promise.all(
processedActions.map(async ({ action, buffersWithDimensions, frameWidth, frameHeight }) => {
// First prepare each frame with exact dimensions
const centeredFrames = await Promise.all(
// Get and validate all frame dimensions first
const frames = await Promise.all(
buffersWithDimensions.map(async ({ buffer }) => {
// Get the current dimensions
const metadata = await sharp(buffer).metadata()
const width = metadata.width!
// Calculate padding to center the sprite
const leftPadding = ((frameWidth - width) >> 1)
return await sharp(buffer)
.extend({
top: 0,
bottom: 0,
left: leftPadding,
right: frameWidth - width - leftPadding,
background: { r: 0, g: 0, b: 0, alpha: 0 }
})
.toBuffer()
return {
buffer,
width: metadata.width!,
height: metadata.height!
}
})
)
// Combine the frames
const combinedImage = await sharp({
create: {
width: frameWidth * centeredFrames.length,
width: frameWidth * frames.length,
height: frameHeight,
channels: 4,
background: { r: 0, g: 0, b: 0, alpha: 0 }
}
})
.composite(
centeredFrames.map((buffer, index) => ({
frames.map(({ buffer, width, height }, index) => ({
input: buffer,
left: index * frameWidth,
// Center horizontally based on the exact middle of each frame
left: (index * frameWidth) + ((frameWidth - width) >> 1),
// Top position is always 0
top: 0
}))
)