-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
229 lines (212 loc) · 6.07 KB
/
index.html
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
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>App Store Images</title>
<style>
img,
canvas {
max-width: min(100%, 512px);
max-height: min(50vh, 512px);
outline: 1px solid black;
}
</style>
</head>
<body>
<div>
<label>
<div>Sizes:</div>
<textarea type="text" id="sizeInput" onchange="handleFile()"></textarea>
</label>
</div>
<hr />
<div>
<label>
Input Image:
<input type="file" id="fileInput" onchange="handleFile()" />
</label>
</div>
<hr />
<div>
<div>Download Images:</div>
<ul id="downloadList">
<li>
<a href="javascript:downloadAll()">Download All</a>
</li>
</ul>
</div>
<hr />
<div style="display: flex">
<label>
<div>Image Preview:</div>
<img id="previewImg" />
</label>
<hr style="margin: 0 1rem" />
<label>
<div>Drawing Space:</div>
<canvas id="canvas"></canvas>
</label>
</div>
<script>
sizeInput.value = `
512x512
1024x500
1240x1240 (1024x1024) icon.png
2732x2732 (1200x1200) splash.png
2732x2732 (1200x1200) icon_background.png
2732x2732 (1200x1200) icon_foreground.png
`.trim()
sizeInput.style.minHeight =
sizeInput.value.split('\n').length * 1.5 + 'em'
sizeInput.style.minWidth = '25em'
let context = canvas.getContext('2d')
function handleFile() {
let file = fileInput.files[0]
if (!file) return
let reader = new FileReader()
reader.onload = () => {
previewImg.onload = handleImage
previewImg.src = reader.result
}
reader.readAsDataURL(file)
}
handleFile()
function sampleColor() {
canvas.width = previewImg.naturalWidth
canvas.height = previewImg.naturalHeight
context.drawImage(previewImg, 0, 0, canvas.width, canvas.height)
let imageData = context.getImageData(0, 0, canvas.width, canvas.height)
let data = imageData.data
let counts = {}
for (let offset = 0; offset < data.length; offset += 4) {
let key =
data[offset + 0] + ',' + data[offset + 1] + ',' + data[offset + 2]
let n = counts[key] || 0
counts[key] = n + 1
}
let entries = Object.entries(counts)
let code =
'#' +
entries
.sort((a, b) => a[1] - b[1])
.pop()[0]
.split(',')
.map(code => toHex(+code))
.join('')
if (code === '#000000' && entries.length === 1) {
return '#ffffff'
}
return code
}
function toHex(code) {
let hex = Math.floor(code).toString(16)
if (hex.length == 1) {
return '0' + hex
}
return hex
}
async function handleImage() {
downloadList.querySelectorAll('li').forEach(li => {
if (li.querySelector('a').href.startsWith('data:image/')) {
li.remove()
}
})
let color = sampleColor()
let sizes = sizeInput.value
.split('\n')
.map(s => s.trim())
.filter(s => s)
// TODO auto crop the image before resize, to avoid duplicated paddings
for (let size of sizes) {
await resizeImage(color, 'logo', size)
}
}
function parseSize(size) {
return size.split('x').map(x => +x)
}
function fillBackground(color) {
context.fillStyle = color
context.rect(0, 0, canvas.width, canvas.height)
context.fill()
}
async function resizeImage(color, name, size) {
let parts = size.split(' ')
let [w, h] = parseSize(parts[0])
let [safeW, safeH] = parseSize(
(parts[1] || '').replace('(', '').replace(')', ''),
)
let filename = parts[2] || `${name}-${size}.png`
canvas.width = w
canvas.height = h
fillBackground(color)
let imgRatio = previewImg.naturalWidth / previewImg.naturalHeight
let canvasRatio = canvas.width / canvas.height
let drawWidth = canvas.height * imgRatio
let drawHeight = canvas.width / imgRatio
if (drawHeight > canvas.height) {
let gap = canvas.width - drawWidth
context.drawImage(
previewImg,
0,
0,
previewImg.naturalWidth,
previewImg.naturalHeight,
gap / 2,
0,
drawWidth,
canvas.height,
)
} else {
let gap = canvas.height - drawHeight
context.drawImage(
previewImg,
0,
0,
previewImg.naturalWidth,
previewImg.naturalHeight,
0,
gap / 2,
canvas.width,
drawHeight,
)
}
if (safeW && safeH) {
let dataUrl = canvas.toDataURL('image/png')
let img = document.createElement('img')
await new Promise((resolve, reject) => {
img.onload = resolve
img.src = dataUrl
})
fillBackground(color)
context.drawImage(
img,
0,
0,
canvas.width,
canvas.height,
(canvas.width - safeW) / 2,
(canvas.height - safeH) / 2,
safeW,
safeH,
)
}
let dataUrl = canvas.toDataURL('image/png')
let li = document.createElement('li')
let a = document.createElement('a')
a.href = dataUrl
a.textContent = filename
a.download = filename
li.appendChild(a)
downloadList.appendChild(li)
}
function downloadAll() {
downloadList.querySelectorAll('a').forEach(a => {
if (!a.href.startsWith('data:image/')) return
a.click()
})
}
</script>
</body>
</html>