-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (65 loc) · 2.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const { program } = require("commander");
const { createCanvas, loadImage } = require("canvas");
const fs = require("fs");
const main = async (inputs, output, width, height, paddingX, paddingY) => {
const canvas = createCanvas(width, height);
const ctx = canvas.getContext("2d");
ctx.fillStyle = "white";
ctx.fillRect(0, 0, width,height);
const images = await Promise.all(
inputs.map((filename) => loadImage(filename))
);
const row = Math.ceil(Math.sqrt(inputs.length));
const col = Math.ceil(inputs.length / row);
const xBlockOffset = width / row;
const yBlockOffset = height / col;
ctx.setLineDash([1, 20]);
Array.from({ length: col }, (_, index) => index).forEach(index=>{
ctx.beginPath();
ctx.moveTo(0,index*yBlockOffset);
ctx.lineTo(width,index*yBlockOffset);
ctx.stroke();
});
Array.from({ length: row }, (_, index) => index).forEach(index=>{
ctx.beginPath();
ctx.moveTo(index*xBlockOffset,0);
ctx.lineTo(index*xBlockOffset, height);
ctx.stroke();
});
const blockWidth = xBlockOffset - paddingX;
const blockHeight = yBlockOffset - paddingY;
images.forEach((image, index) => {
const imageRow = Math.trunc(index / col);
const imageCol = index % col;
const { width, height } = image;
const applySale = Math.min(blockWidth / width, blockHeight / height);
const renderWidth = width * applySale;
const renderHeight = height * applySale;
const renderOriginX =
xBlockOffset * imageRow + xBlockOffset / 2 - renderWidth / 2;
const renderOriginY =
yBlockOffset * imageCol + yBlockOffset / 2 - renderHeight / 2;
ctx.drawImage(
image,
renderOriginX,
renderOriginY,
renderWidth,
renderHeight
);
});
fs.writeFileSync(output, canvas.toBuffer());
};
program
.option("-i, --inputs [strings...]", "input filenames")
.option("-o, --output <string>", "output filename")
.option("-r, --resolution [number...]", "output resolution [width,height]")
.option("-p, --padding [number...]", "padding [x,y]");
program.parse(process.argv);
const options = program.opts();
if (options.inputs && options.output && options.resolution) {
const [width, height] = options.resolution.map((value) => parseInt(value));
const [paddingX, paddingY] = options.padding.map((value) => parseInt(value));
main(options.inputs, options.output, width, height, paddingX, paddingY);
} else {
console.log(`arguments not found, current args: ${JSON.stringify(options)}`);
}