-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.html
409 lines (377 loc) · 10.4 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<!DOCTYPE html>
<html lang="en">
<title>Badge Generator - OMG - Developer Student Clubs</title>
<meta
name="description"
content="Badge Generator - OMG - Developer Student Clubs"
/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" href="./favicon.png" />
<link
crossorigin="anonymous"
href="https://fonts.googleapis.com/icon?family=Google+Sans:400&display=swap"
rel="stylesheet"
/>
<style>
* {
box-sizing: border-box;
--shadow-elevation-1: 0 2px 4px -1px rgba(0, 0, 0, 0.2),
0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12);
--shadow-elevation-2: 0 3px 5px -1px rgba(0, 0, 0, 0.2),
0 6px 10px 0 rgba(0, 0, 0, 0.14), 0 1px 18px 0 rgba(0, 0, 0, 0.12);
--button-color: #2979ff;
--button-color-hover: #3580fd;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
background: #e0e0e0;
margin: 0;
font-family: "Google Sans", sans-serif;
display: flex;
flex-direction: column;
color: #4a4a4a;
min-height: 100vh;
}
header {
background-color: #fff;
height: 76px;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
box-shadow: 0 2px 4px -1px rgba(0, 0, 0, 0.2),
0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12);
z-index: 1;
}
.main-container {
display: flex;
flex: 1;
}
.input-panel {
padding: 24px;
width: 40%;
max-width: 420px;
background-color: #f5f5f5;
box-shadow: 0 1px 5px 0 rgba(0, 0, 0, 0.2),
0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.12);
}
.input {
margin-bottom: 36px;
display: flex;
flex-direction: column;
align-items: flex-start;
}
.input:last-child {
margin-bottom: 0;
}
.input label {
font-size: 13px;
margin-bottom: 8px;
font-weight: 500;
}
.button {
color: #fff;
font-size: 15px;
border-radius: 4px;
padding: 10px 12px;
box-shadow: var(--shadow-elevation-1);
background-color: var(--button-color);
cursor: pointer;
border: none;
transition: box-shadow 100ms ease, background-color 100ms ease;
}
.button:hover {
box-shadow: var(--shadow-elevation-2);
background-color: var(--button-color-hover);
}
input.category-input {
font-size: 15px;
padding: 8px;
background: #fff;
border: 1px solid #cacaca;
border-radius: 4px;
width: 100%;
max-width: 360px;
}
input.category-input:focus {
outline: none;
border-color: #8e8e8e;
}
.select-container {
display: flex;
border-radius: 4px;
border: 1px solid #cacaca;
overflow: hidden;
}
.select-container .select {
background-color: #fff;
padding: 6px 12px;
font-size: 15px;
cursor: pointer;
transition: background-color 100ms ease;
}
.select-container .select:not(:last-child) {
border-right: 1px solid #cacaca;
}
.select-container .select[selected] {
color: #fff;
background-color: var(--button-color);
}
.preview-panel {
padding: 36px 0;
flex: 1;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
canvas {
width: 500px;
max-width: 80%;
}
.download-fab {
cursor: pointer;
position: absolute;
bottom: 24px;
right: 24px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
width: 64px;
height: 64px;
background-color: #2979ff;
box-shadow: var(--shadow-elevation-1);
transition: background-color 100ms ease, box-shadow 100ms ease;
}
.download-fab:hover {
background-color: #3580fd;
box-shadow: var(--shadow-elevation-2);
}
@media screen and (max-width: 860px) {
header {
height: 56px;
font-size: 20px;
}
.main-container {
flex-direction: column;
}
.input-panel,
.preview-panel {
width: 100%;
max-width: initial;
}
.preview-panel {
padding: 24px 0 96px 0;
flex: 1;
}
.input {
margin-bottom: 24px;
}
.download-fab {
width: 56px;
height: 56px;
}
}
</style>
<header style="padding: 1rem">
<div class="title">OMG Badge Generator</div>
</header>
<div class="main-container">
<div class="input-panel">
<div class="input">
<label>Profile Picture</label>
<button class="button" onclick="uploadImage()">Upload Image</button>
<input
class="profile-input"
type="file"
accept="image/*"
onchange="upload(event)"
hidden
/>
</div>
<div class="input">
<label>Image Shape</label>
<div class="select-container">
<div
class="select"
id="original"
selected
onclick="changeShape('original')"
>
Original
</div>
<div class="select" id="square" onclick="changeShape('square')">
Square
</div>
<div class="select" id="circle" onclick="changeShape('circle')">
Circle
</div>
</div>
</div>
<div class="input">
<a href="https://dscomg.com">🎉 Developer Student Clubs OMG</a>
</div>
</div>
<div class="preview-panel">
<canvas></canvas>
<div class="download-fab" onclick="download()">
<svg
xmlns="http://www.w3.org/2000/svg"
height="24"
viewBox="0 0 24 24"
width="24"
fill="#fff"
>
<path d="M0 0h24v24H0V0z" fill="none" />
<path
d="M16.59 9H15V4c0-.55-.45-1-1-1h-4c-.55 0-1 .45-1 1v5H7.41c-.89 0-1.34 1.08-.71 1.71l4.59 4.59c.39.39 1.02.39 1.41 0l4.59-4.59c.63-.63.19-1.71-.7-1.71zM5 19c0 .55.45 1 1 1h12c.55 0 1-.45 1-1s-.45-1-1-1H6c-.55 0-1 .45-1 1z"
/>
</svg>
</div>
</div>
</div>
<script>
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
let image = "";
let shape = "original";
let append = "";
let who = "attendee";
var banner;
const uploadImage = () => {
document.querySelector("input.profile-input").click();
};
const changeShape = (type) => {
const original = document.querySelector(
".select-container .select#original"
);
const square = document.querySelector(".select-container .select#square");
const circle = document.querySelector(".select-container .select#circle");
shape = type;
switch (type) {
case "original": {
append = "";
original.setAttribute("selected", "");
square.removeAttribute("selected");
circle.removeAttribute("selected");
break;
}
case "square": {
append = "";
square.setAttribute("selected", "");
original.removeAttribute("selected");
circle.removeAttribute("selected");
break;
}
case "circle": {
append = "-circle";
circle.setAttribute("selected", "");
original.removeAttribute("selected");
square.removeAttribute("selected");
break;
}
}
draw(who);
};
const upload = (e) => {
if (e && e.target.files && e.target.files[0]) {
const reader = new FileReader();
reader.onload = (event) => {
const img = new Image();
img.onload = () => {
image = img;
draw(who);
};
img.src = event.target.result;
};
reader.readAsDataURL(e.target.files[0]);
}
};
const draw = (who) => {
banner = new Image();
banner.src = "./" + who + append + ".png";
banner.onload = () => {
if (image) {
switch (shape) {
case "original": {
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0);
break;
}
default: {
const size = Math.min(image.width, image.height);
canvas.width = 500;
canvas.height = 500;
const hRatio = canvas.width / image.width;
const vRatio = canvas.height / image.height;
const ratio = Math.max(hRatio, vRatio);
const x = (canvas.width - image.width * ratio) / 2;
const y = (canvas.height - image.height * ratio) / 2;
ctx.drawImage(
image,
0,
0,
image.width,
image.height,
x,
y,
image.width * ratio,
image.height * ratio
);
break;
}
}
} else {
ctx.canvas.width = 500;
ctx.canvas.height = 500;
ctx.fillStyle = "#0d47a1";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
const height = (banner.height / banner.width) * canvas.width;
const y = canvas.height - height;
const fontSize = canvas.width / 17.2;
const fontY = y + height * 0.7;
ctx.drawImage(
banner,
0,
0,
banner.width,
banner.height,
0,
y,
canvas.width,
height
);
ctx.fillStyle = "#757575";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = `${fontSize}px Google Sans, sans-serif`;
// ctx.fillText(category, canvas.width / 2, fontY)
if (shape === "circle") {
ctx.globalCompositeOperation = "destination-in";
ctx.beginPath();
ctx.arc(
canvas.width / 2,
canvas.height / 2,
canvas.height / 2,
0,
Math.PI * 2
);
ctx.closePath();
ctx.fill();
}
};
};
const download = () => {
const a = document.createElement("a");
const url = canvas.toDataURL("image/png;base64");
a.download = "badge.png";
a.href = url;
a.click();
};
draw(who);
</script>
</html>