-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic_paper.js
48 lines (40 loc) · 1.41 KB
/
pic_paper.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
const canvas = document.getElementById('canvasOrigin');
const ctx = canvas.getContext("2d");
presetSizeChange();
paramASize.addEventListener('change', presetSizeChange);
paramPPI.addEventListener('change', presetSizeChange);
paramW.addEventListener('blur', function(e) { paramWmm.value = px2mm(this.value); });
paramH.addEventListener('blur', function(e) { paramHmm.value = px2mm(this.value); });
paramWmm.addEventListener('blur', function(e) { paramW.value = mm2px(this.value); });
paramHmm.addEventListener('blur', function(e) { paramH.value = mm2px(this.value); });
function presetSizeChange() {
let hMM = paramASize.value;
let wMM = Math.round(paramASize.value / Math.sqrt(2));
paramHmm.value = hMM;
paramWmm.value = wMM;
paramH.value = mm2px(hMM);
paramW.value = mm2px(wMM);
}
function mm2px(mm) {
let ppi = paramPPI.value;
let inch = mm/25.4;
return Math.ceil(inch*ppi);
}
function px2mm(px) {
let ppi = paramPPI.value;
return Math.floor(px/ppi*25.4);
}
function getPaperImage() {
canvas.width = paramW.value;
canvas.height = paramH.value;
ctx.fillStyle = paramColor.value;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
btnOk.addEventListener("click", getPaperImage);
btnDownload.addEventListener('click', function(e) {
const a = document.createElement('a');
a.href = canvas.toDataURL(`image/${paramMime.value}`);
a.download = `paper.${paramMime.value}`;
a.click();
a.remove();
});