-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathicongen.js
More file actions
217 lines (187 loc) · 7.8 KB
/
icongen.js
File metadata and controls
217 lines (187 loc) · 7.8 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
function loadImg(src) {
return new Promise((resolve, reject) => {
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => resolve(img);
img.onerror = () => reject(new Error('Failed to load: ' + src));
img.src = src;
});
}
function canvasToBlob(canvas, type = 'image/png') {
return new Promise(resolve => canvas.toBlob(resolve, type));
}
function makeCanvas(w, h) {
const c = document.createElement('canvas');
c.width = w;
c.height = h;
return c;
}
function resizeToSquare(imgOrCanvas, w, h) {
const c = makeCanvas(w, h);
const ctx = c.getContext('2d');
ctx.clearRect(0, 0, w, h);
ctx.drawImage(imgOrCanvas, 0, 0, w, h);
return c;
}
function maskCircle(src, w, h) {
const c = makeCanvas(w, h);
const ctx = c.getContext('2d');
ctx.beginPath();
ctx.arc(w / 2, h / 2, Math.min(w, h) / 2, 0, Math.PI * 2);
ctx.clip();
ctx.drawImage(src, 0, 0, w, h);
return c;
}
function composite(base, overlay, ox, oy) {
const ctx = base.getContext('2d');
ctx.drawImage(overlay, ox, oy);
return base;
}
async function canvasToPngBytes(canvas) {
const blob = await canvasToBlob(canvas, 'image/png');
return new Uint8Array(await blob.arrayBuffer());
}
async function loadTemplateCanvas(src) {
const img = await loadImg(src);
const c = makeCanvas(img.naturalWidth, img.naturalHeight);
c.getContext('2d').drawImage(img, 0, 0);
return c;
}
async function procCube(usrCanvas, idxStr, zip) {
const hdBase = await loadTemplateCanvas('player_01-hd.png');
const reszHd = resizeToSquare(usrCanvas, 55, 56);
const rotCanv = makeCanvas(56, 55);
const rotCtx = rotCanv.getContext('2d');
rotCtx.translate(56 / 2, 55 / 2);
rotCtx.rotate(Math.PI / 2);
rotCtx.drawImage(reszHd, -55 / 2, -56 / 2);
composite(hdBase, rotCanv, 71, 5);
zip.file(`icons/player_${idxStr}-hd.png`, await canvasToPngBytes(hdBase));
const uhdBase = await loadTemplateCanvas('player_01-uhd.png');
const reszUhd = resizeToSquare(usrCanvas, 108, 108);
composite(uhdBase, reszUhd, 37, 8);
zip.file(`icons/player_${idxStr}-uhd.png`, await canvasToPngBytes(uhdBase));
await addPlist('player_01-hd.plist', `icons/player_${idxStr}-hd.plist`, 'player_01', `player_${idxStr}`, zip);
await addPlist('player_01-uhd.plist', `icons/player_${idxStr}-uhd.plist`, 'player_01', `player_${idxStr}`, zip);
}
async function procWave(usrCanvas, idxStr, zip) {
const hdBase = await loadTemplateCanvas('dart_01-hd.png');
const wHd = 24, hHd = 25;
const waveHd = resizeToSquare(usrCanvas, wHd, hHd);
const circHd = maskCircle(waveHd, wHd, hHd);
composite(hdBase, circHd, 3, 102);
zip.file(`icons/dart_${idxStr}-hd.png`, await canvasToPngBytes(hdBase));
const uhdBase = await loadTemplateCanvas('dart_01-uhd.png');
const wUhd = 48, hUhd = 53;
const waveUhd = resizeToSquare(usrCanvas, wUhd, hUhd);
const circUhd = maskCircle(waveUhd, wUhd, hUhd);
composite(uhdBase, circUhd, 4, 197);
zip.file(`icons/dart_${idxStr}-uhd.png`, await canvasToPngBytes(uhdBase));
await addPlist('dart_01-hd.plist', `icons/dart_${idxStr}-hd.plist`, 'dart_01', `dart_${idxStr}`, zip);
await addPlist('dart_01-uhd.plist', `icons/dart_${idxStr}-uhd.plist`, 'dart_01', `dart_${idxStr}`, zip);
}
async function procBall(usrCanvas, idxStr, zip) {
const hdBase = await loadTemplateCanvas('player_ball_01-hd.png');
const wHd = 65, hHd = 66;
const ballHd = resizeToSquare(usrCanvas, wHd, hHd);
const circHd = maskCircle(ballHd, wHd, hHd);
composite(hdBase, circHd, 83, 4);
zip.file(`icons/player_ball_${idxStr}-hd.png`, await canvasToPngBytes(hdBase));
const uhdBase = await loadTemplateCanvas('player_ball_01-uhd.png');
const wUhd = 130, hUhd = 130;
const ballUhd = resizeToSquare(usrCanvas, wUhd, hUhd);
const circUhd = maskCircle(ballUhd, wUhd, hUhd);
composite(uhdBase, circUhd, 162, 8);
zip.file(`icons/player_ball_${idxStr}-uhd.png`, await canvasToPngBytes(uhdBase));
await addPlist('player_ball_01-hd.plist', `icons/player_ball_${idxStr}-hd.plist`, 'player_ball_01', `player_ball_${idxStr}`, zip);
await addPlist('player_ball_01-uhd.plist', `icons/player_ball_${idxStr}-uhd.plist`, 'player_ball_01', `player_ball_${idxStr}`, zip);
}
const plistCache = {};
async function addPlist(srcPath, zipPath, fromId, toId, zip) {
try {
if (!plistCache[srcPath]) {
const resp = await fetch(srcPath);
if (!resp.ok) return;
plistCache[srcPath] = await resp.text();
}
const content = plistCache[srcPath].replaceAll(fromId, toId);
zip.file(zipPath, content);
} catch (e) {}
}
async function buildPackIcon(customFile) {
if (customFile) {
return new Promise((resolve) => {
const rdr = new FileReader();
rdr.onload = async (e) => {
try {
const img = await loadImg(e.target.result);
const c = makeCanvas(336, 336);
const ctx = c.getContext('2d');
ctx.drawImage(img, 0, 0, 336, 336);
ctx.fillStyle = 'rgba(0,0,0,0.5)';
ctx.fillRect(0, 300, 336, 36);
ctx.fillStyle = 'rgba(255,255,255,0.4)';
ctx.font = '14px sans-serif';
ctx.fillText('GDIconMaker — github pages', 60, 318);
resolve(c);
} catch {
resolve(null);
}
};
rdr.readAsDataURL(customFile);
});
}
return null;
}
async function fetchPackJson(packName, packAuthor) {
try {
const resp = await fetch('pack.json');
if (!resp.ok) throw new Error();
const data = await resp.json();
const pckId = 'gdiconmaker.' + packName.replace(/[^a-zA-Z0-9]/g, '');
data.id = pckId;
data.author = packAuthor + ' (from gdiconmaker.rf.gd)';
data.name = packName;
return JSON.stringify(data, null, 2);
} catch {
const pckId = 'gdiconmaker.' + packName.replace(/[^a-zA-Z0-9]/g, '');
return JSON.stringify({
id: pckId,
name: packName,
author: packAuthor + ' (from gdiconmaker.rf.gd)',
description: 'Custom GD icon pack',
version: '1.0.0'
}, null, 2);
}
}
async function generateIconPack({ processedImgs, packName, packAuthor, doCube, doWave, doBall, customPackIconFile }) {
if (typeof JSZip === 'undefined') {
throw new Error('JSZip not loaded — check your internet connection');
}
const zip = new JSZip();
const packJsonStr = await fetchPackJson(packName, packAuthor);
zip.file('pack.json', packJsonStr);
const customIconCanvas = await buildPackIcon(customPackIconFile);
if (customIconCanvas) {
zip.file('pack.png', await canvasToPngBytes(customIconCanvas));
} else {
try {
const resp = await fetch('pack.png');
if (resp.ok) zip.file('pack.png', await resp.arrayBuffer());
} catch {}
}
for (let i = 0; i < processedImgs.length; i++) {
const imgEntry = processedImgs[i];
const iconIdx = imgEntry.customNum || (i + 1);
const idxStr = String(iconIdx).padStart(2, '0');
const img = await loadImg(imgEntry.data);
const usrCanvas = makeCanvas(img.naturalWidth, img.naturalHeight);
usrCanvas.getContext('2d').drawImage(img, 0, 0);
if (doCube) await procCube(usrCanvas, idxStr, zip);
if (doWave) await procWave(usrCanvas, idxStr, zip);
if (doBall) await procBall(usrCanvas, idxStr, zip);
}
const blob = await zip.generateAsync({ type: 'blob', compression: 'DEFLATE', compressionOptions: { level: 6 } });
return blob;
}
window.generateIconPack = generateIconPack;