1
0
forked from noxious/server

Almost works

This commit is contained in:
Dennis Postma 2025-01-28 17:49:43 +01:00
parent 3b65cae631
commit b33b9cc29f

View File

@ -73,14 +73,16 @@ export default class SpriteUpdateEvent extends BaseEvent {
private async calculateMaxWidth(sprites: string[]): Promise<number> {
if (!sprites.length) return 0
const widths = await Promise.all(sprites.map(async (base64) => {
const uri = base64.split(';base64,').pop()
if (!uri) return 0
const widths = await Promise.all(
sprites.map(async (base64) => {
const uri = base64.split(';base64,').pop()
if (!uri) return 0
const imgBuffer = Buffer.from(uri, 'base64')
const image = await sharp(imgBuffer).metadata()
return image.width ?? 0
}))
const imgBuffer = Buffer.from(uri, 'base64')
const image = await sharp(imgBuffer).metadata()
return image.width ?? 0
})
)
return Math.max(...widths)
}
@ -88,14 +90,16 @@ export default class SpriteUpdateEvent extends BaseEvent {
private async calculateMaxHeight(sprites: string[]): Promise<number> {
if (!sprites.length) return 0
const heights = await Promise.all(sprites.map(async (base64) => {
const uri = base64.split(';base64,').pop()
if (!uri) return 0
const heights = await Promise.all(
sprites.map(async (base64) => {
const uri = base64.split(';base64,').pop()
if (!uri) return 0
const imgBuffer = Buffer.from(uri, 'base64')
const image = await sharp(imgBuffer).metadata()
return image.height ?? 0
}))
const imgBuffer = Buffer.from(uri, 'base64')
const image = await sharp(imgBuffer).metadata()
return image.height ?? 0
})
)
return Math.max(...heights)
}
@ -117,22 +121,24 @@ export default class SpriteUpdateEvent extends BaseEvent {
)
// Find the largest dimensions
const maxWidth = Math.max(...imageData.map(data => data.width))
const maxHeight = Math.max(...imageData.map(data => data.height))
const maxWidth = Math.max(...imageData.map((data) => data.width))
const maxHeight = Math.max(...imageData.map((data) => data.height))
// Extend all images to match the largest dimensions without resizing
const resizedBuffers = await Promise.all(
imageData.map(async ({ buffer, width, height }) => {
// Calculate padding to center the sprite
const topPadding = 0 // This is always 0. We don't need to calculate it
const bottomPadding = Math.ceil((maxHeight - height) / 2)
const leftPadding = Math.floor((maxWidth - width) / 2)
const topPadding = Math.floor((maxHeight - height) / 2)
const rightPadding = Math.ceil((maxWidth - width) / 2)
return await sharp(buffer)
.extend({
top: topPadding,
bottom: Math.ceil((maxHeight - height) / 2),
top: 0,
bottom: bottomPadding,
left: leftPadding,
right: Math.ceil((maxWidth - width) / 2),
right: rightPadding,
background: { r: 0, g: 0, b: 0, alpha: 0 }
})
.toBuffer()