-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic_tailor.js
174 lines (156 loc) · 4.2 KB
/
pic_tailor.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const ctx = canvas.getContext("2d");
let gCount = 0;
let gTotal = 0;
let imgs = [];
let objUrls = [];
let mode = "modeRoll";
let firstLoad = true;
function modeChange(ev) {
let el = ev.target;
if (el.checked) mode = el.id;
paramChange();
}
modeRoll.addEventListener('change', modeChange);
modePile.addEventListener('change', modeChange);
modeTTB.addEventListener('change', modeChange);
modeLTR.addEventListener('change', modeChange);
function addImage(src) {
const image = new Image();
image.onload = drawImageAll;
image.src = src;
imgs.push(image);
}
function drawImagePile(offsetX, offsetY) {
let imgsCnt = imgs.length;
canvas.width = imgs[0].naturalWidth + (imgsCnt - 1) * offsetX;
canvas.height = imgs[0].naturalHeight + (imgsCnt - 1) * offsetY;
for (let i = 0; i < imgs.length; i++) {
ctx.drawImage(imgs[i], offsetX * i, offsetY * i);
}
}
function drawImageRoll(offsetX, offsetY) {
let imgsCnt = imgs.length;
canvas.width = imgs[0].naturalWidth + (imgsCnt - 1) * offsetX;
canvas.height = imgs[0].naturalHeight + (imgsCnt - 1) * offsetY;
let coords = [[0,0]];
for (let i = 1; i < imgs.length; i++) {
let i0w = imgs[i-1].naturalWidth, i0h = imgs[i-1].naturalHeight,
i1w = imgs[i].naturalWidth, i1h = imgs[i].naturalHeight;
coords.push(
[i0w + offsetX - i1w + coords[i-1][0],
i0h + offsetY - i1h + coords[i-1][1]]
);
}
// console.log(coords);
for (let i = imgs.length - 1; i >= 0; i--) {
let dx = coords[i][0], dy = coords[i][1];
ctx.drawImage(imgs[i], dx, dy);
}
}
const getMax = (a,b) => Math.max(a,b);
const getSum = (acc,val) => acc+val;
const maxImgWidth = (imgs) => imgs.map(x=>x.naturalWidth).reduce(getMax);
const maxImgHeight = (imgs) => imgs.map(x=>x.naturalHeight).reduce(getMax);
const totalImgWidth = (imgs) => imgs.map(x=>x.naturalWidth).reduce(getSum);
const totalImgHeight = (imgs) => imgs.map(x=>x.naturalHeight).reduce(getSum);
function drawImageTTB() {
canvas.width = maxImgWidth(imgs);
canvas.height = totalImgHeight(imgs);
let dy = 0;
for (let i = 0; i < imgs.length; i++) {
ctx.drawImage(imgs[i], 0, dy);
dy += imgs[i].naturalHeight;
}
}
function drawImageLTR(image) {
canvas.width = totalImgWidth(imgs);
canvas.height = maxImgHeight(imgs);
let dx = 0;
for (let i = 0; i < imgs.length; i++) {
ctx.drawImage(imgs[i], dx, 0);
dx += imgs[i].naturalWidth;
}
}
function drawImageAll() {
gCount++;
if (gCount == 1 && firstLoad) {
let gap = Math.floor(this.naturalHeight / 8);
paramY.value = gap;
}
if (gCount == gTotal) {
let offsetX = parseInt(paramX.value);
let offsetY = parseInt(paramY.value);
switch(mode) {
case 'modeRoll': drawImageRoll(offsetX, offsetY); break;
case 'modePile': drawImagePile(offsetX, offsetY); break;
case 'modeTTB': drawImageTTB(); break;
case 'modeLTR': drawImageLTR(); break;
}
}
}
btnDownload.addEventListener('click', function(e) {
const a = document.createElement('a');
a.href = canvas.toDataURL(`image/${paramMime.value}`, 1);
a.download = `all.${paramMime.value}`;
a.click();
a.remove();
});
inputImg.addEventListener('change', function(e) {
if (this.files.length == 0) {
reset();
return;
}
if (modeAppend.checked) {
adjust();
firstLoad = (gTotal == 0);
gTotal += this.files.length;
} else {
reset();
firstLoad = true;
gTotal = this.files.length;
}
const fileList = this.files;
for (let file of fileList) {
const fileURL = URL.createObjectURL(file);
objUrls.push(fileURL);
}
//console.log(objUrls);
for (let url of objUrls) {
addImage(url);
}
});
paramX.addEventListener('blur', paramChange);
paramY.addEventListener('blur', paramChange);
function paramChange() {
adjust();
firstLoad = false;
if (objUrls.length > 0) {
for (let url of objUrls) {
addImage(url);
}
}
}
function adjust() {
clearCanvas();
imgs = [];
gCount = 0;
}
function reset() {
clearObjUrls();
clearCanvas();
imgs = [];
gCount = 0;
gTotal = 0;
}
function clearObjUrls() {
if (objUrls.length > 0) {
for (let url of objUrls) {
window.URL.revokeObjectURL(url);
}
objUrls = [];
}
}
function clearCanvas() {
canvas.width = 0;
canvas.height = 0;
}