Skip to content

Commit

Permalink
Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbende committed Jun 28, 2023
1 parent a0fd035 commit 95fc441
Showing 1 changed file with 48 additions and 39 deletions.
87 changes: 48 additions & 39 deletions src/spritesheet_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,60 @@ import sharp from "sharp";
import { globbyStream } from "globby";
import { MaxRectsPacker } from "maxrects-packer";


for (const theme of ["dark", "light"]) {
const packer = new MaxRectsPacker(Infinity, Infinity, 0, {
pot: false,
square: false,
});
const packer = new MaxRectsPacker(Infinity, Infinity, 0, {
pot: false,
square: false,
});

let images = [];
let images = [];

for await (const path of globbyStream(`${theme}/*.png`)) {
images.push(path)
}
for await (const path of globbyStream(`${theme}/*.png`)) {
images.push(path);
}

packer.addArray(await Promise.all(images.map(async f => {
packer.addArray(
await Promise.all(
images.map(async (f) => {
const p = f;
const { width, height } = await sharp(p).metadata();
const imagePath = f;
return {
width,
height,
imagePath,
};
})));

const packedBin = packer.bins[0];

const packedImage = await sharp({
create: {
width: packedBin.width,
height: packedBin.height,
channels: 4,
background: 'transparent',
},
return { width, height, imagePath };
})
)
);

const packedBin = packer.bins[0];

const packedImage = await sharp({
create: {
width: packedBin.width,
height: packedBin.height,
channels: 4,
background: "transparent",
},
})
.composite(
packedBin.rects.map((e) => ({
input: e.imagePath,
left: e.x,
top: e.y,
}))
)
.png({
compressionLevel: 9,
colors: 70, // it would fit into 60
})
.composite(packedBin.rects.map(e => ({
input: e.imagePath,
left: e.x,
top: e.y,
})))
.png({
compressionLevel: 9,
colors: 70, // it would fit into 60
})
.toBuffer();

fs.writeFileSync(`../sv_ttk/theme/spritesheet_${theme}.png`, packedImage);
fs.writeFileSync(`../sv_ttk/theme/sprites_${theme}.tcl`, `set ::spriteinfo [list \\\n ${packedBin.rects.map(e => `${path.parse(e.imagePath).name} ${e.x} ${e.y} ${e.width} ${e.height}`).join(' \\\n ')} \\\n]\n`);
.toBuffer();

fs.writeFileSync(`../sv_ttk/theme/spritesheet_${theme}.png`, packedImage);
fs.writeFileSync(
`../sv_ttk/theme/sprites_${theme}.tcl`,
`set ::spriteinfo [list \\\n ${packedBin.rects
.map(
(e) =>
`${path.parse(e.imagePath).name} ${e.x} ${e.y} ${e.width} ${e.height}`
)
.join(" \\\n ")} \\\n]\n`
);
}

0 comments on commit 95fc441

Please sign in to comment.