Almost works
This commit is contained in:
parent
3b65cae631
commit
b33b9cc29f
@ -73,14 +73,16 @@ export default class SpriteUpdateEvent extends BaseEvent {
|
|||||||
private async calculateMaxWidth(sprites: string[]): Promise<number> {
|
private async calculateMaxWidth(sprites: string[]): Promise<number> {
|
||||||
if (!sprites.length) return 0
|
if (!sprites.length) return 0
|
||||||
|
|
||||||
const widths = await Promise.all(sprites.map(async (base64) => {
|
const widths = await Promise.all(
|
||||||
|
sprites.map(async (base64) => {
|
||||||
const uri = base64.split(';base64,').pop()
|
const uri = base64.split(';base64,').pop()
|
||||||
if (!uri) return 0
|
if (!uri) return 0
|
||||||
|
|
||||||
const imgBuffer = Buffer.from(uri, 'base64')
|
const imgBuffer = Buffer.from(uri, 'base64')
|
||||||
const image = await sharp(imgBuffer).metadata()
|
const image = await sharp(imgBuffer).metadata()
|
||||||
return image.width ?? 0
|
return image.width ?? 0
|
||||||
}))
|
})
|
||||||
|
)
|
||||||
|
|
||||||
return Math.max(...widths)
|
return Math.max(...widths)
|
||||||
}
|
}
|
||||||
@ -88,14 +90,16 @@ export default class SpriteUpdateEvent extends BaseEvent {
|
|||||||
private async calculateMaxHeight(sprites: string[]): Promise<number> {
|
private async calculateMaxHeight(sprites: string[]): Promise<number> {
|
||||||
if (!sprites.length) return 0
|
if (!sprites.length) return 0
|
||||||
|
|
||||||
const heights = await Promise.all(sprites.map(async (base64) => {
|
const heights = await Promise.all(
|
||||||
|
sprites.map(async (base64) => {
|
||||||
const uri = base64.split(';base64,').pop()
|
const uri = base64.split(';base64,').pop()
|
||||||
if (!uri) return 0
|
if (!uri) return 0
|
||||||
|
|
||||||
const imgBuffer = Buffer.from(uri, 'base64')
|
const imgBuffer = Buffer.from(uri, 'base64')
|
||||||
const image = await sharp(imgBuffer).metadata()
|
const image = await sharp(imgBuffer).metadata()
|
||||||
return image.height ?? 0
|
return image.height ?? 0
|
||||||
}))
|
})
|
||||||
|
)
|
||||||
|
|
||||||
return Math.max(...heights)
|
return Math.max(...heights)
|
||||||
}
|
}
|
||||||
@ -117,22 +121,24 @@ export default class SpriteUpdateEvent extends BaseEvent {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Find the largest dimensions
|
// Find the largest dimensions
|
||||||
const maxWidth = Math.max(...imageData.map(data => data.width))
|
const maxWidth = Math.max(...imageData.map((data) => data.width))
|
||||||
const maxHeight = Math.max(...imageData.map(data => data.height))
|
const maxHeight = Math.max(...imageData.map((data) => data.height))
|
||||||
|
|
||||||
// Extend all images to match the largest dimensions without resizing
|
// Extend all images to match the largest dimensions without resizing
|
||||||
const resizedBuffers = await Promise.all(
|
const resizedBuffers = await Promise.all(
|
||||||
imageData.map(async ({ buffer, width, height }) => {
|
imageData.map(async ({ buffer, width, height }) => {
|
||||||
// Calculate padding to center the sprite
|
// 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 leftPadding = Math.floor((maxWidth - width) / 2)
|
||||||
const topPadding = Math.floor((maxHeight - height) / 2)
|
const rightPadding = Math.ceil((maxWidth - width) / 2)
|
||||||
|
|
||||||
return await sharp(buffer)
|
return await sharp(buffer)
|
||||||
.extend({
|
.extend({
|
||||||
top: topPadding,
|
top: 0,
|
||||||
bottom: Math.ceil((maxHeight - height) / 2),
|
bottom: bottomPadding,
|
||||||
left: leftPadding,
|
left: leftPadding,
|
||||||
right: Math.ceil((maxWidth - width) / 2),
|
right: rightPadding,
|
||||||
background: { r: 0, g: 0, b: 0, alpha: 0 }
|
background: { r: 0, g: 0, b: 0, alpha: 0 }
|
||||||
})
|
})
|
||||||
.toBuffer()
|
.toBuffer()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user