-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
75 lines (61 loc) · 2.2 KB
/
script.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
/**
* Copyright (C) Eliot Partridge, 2019. Licensed under MIT.
*/
// ID card dimensions = 92mm * 60 mm
// Apsect ratio = 1.533 : 1
const CANVAS_WIDTH = 1533;
const CANVAS_HEIGHT = 1000;
const BORDER_RADIUS = 50;
window.addEventListener('DOMContentLoaded', () => {
const canvas = document.querySelector('#id-card');
if (!(canvas instanceof HTMLCanvasElement)) return;
canvas.width = CANVAS_WIDTH;
canvas.height = CANVAS_HEIGHT;
const ctx = canvas.getContext('2d');
if (!(ctx instanceof CanvasRenderingContext2D)) return;
const bg = new Image();
let bgPattern = null;
bg.onload = () => {
bgPattern = ctx.createPattern(bg, 'no-repeat');
drawBox();
}
bg.src = './MLP-ID-Card.png';
const drawBox = () => {
ctx.beginPath();
// top left
ctx.arc(BORDER_RADIUS, BORDER_RADIUS, BORDER_RADIUS, Math.PI, 3 * Math.PI / 2);
// top right
ctx.arc(canvas.width - BORDER_RADIUS, BORDER_RADIUS, BORDER_RADIUS, 3 * Math.PI / 2, 0);
// bottom right
ctx.arc(canvas.width - BORDER_RADIUS, canvas.height - BORDER_RADIUS, BORDER_RADIUS, 0, Math.PI / 2);
// bottom left
ctx.arc(BORDER_RADIUS, canvas.height - BORDER_RADIUS, BORDER_RADIUS, Math.PI / 2, Math.PI);
// finish path
ctx.lineTo(0, BORDER_RADIUS);
ctx.fillStyle = bgPattern || '#222';
ctx.fill();
}
drawBox();
const redrawCard = () => {
const name = document.querySelector('#char-name').value.toUpperCase();
const eyeColor = document.querySelector('#eye-color').value.toUpperCase();
const cityEl = document.querySelector('#city');
const city = cityEl.options[cityEl.selectedIndex].text.toUpperCase();
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBox();
ctx.fillStyle = '#060E21';
ctx.font = '77pt \'Patrick Hand\',sans-serif';
ctx.textBaseline = 'alphabetic';
ctx.fillText(name, 536, 374, 500);
};
document.querySelectorAll(['input, select']).forEach(input => {
input.addEventListener('input', redrawCard);
});
document.querySelector('#save').addEventListener('click', () => {
const link = document.createElement('a');
const imgData = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream');
link.setAttribute('download', 'id-card.png');
link.setAttribute('href', imgData);
link.click();
});
});