-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
177 lines (155 loc) · 5.11 KB
/
main.ts
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
import init, { gen } from "./pkg/passphoto.js";
import Panzoom from "@panzoom/panzoom";
const cropperElement = document.getElementById("cropper") as HTMLImageElement;
const zoomRange = document.getElementById("zoom") as HTMLInputElement;
const chinlineRange = document.getElementById("chinline") as HTMLInputElement;
const resultElement = document.getElementById("img") as HTMLImageElement;
const downloadLink = document.getElementById(
"download-btn",
) as HTMLAnchorElement;
const fileInput = document.getElementById("file") as HTMLInputElement;
const faceHeightContainer = document.getElementById("face-height-container");
const chinlineToggle = document.getElementById(
"chinline-toggle",
) as HTMLInputElement;
const maskToggle = document.getElementById("mask-toggle");
const imageWrapper = document.getElementById("image-wrapper");
const kidsMaskToggle = document.getElementById("kids-mask");
const panzoom = Panzoom(cropperElement, {
maxScale: 10,
});
zoomRange.addEventListener("input", (e) => {
panzoom.zoom((e.target as HTMLInputElement).valueAsNumber);
});
chinlineRange.addEventListener("input", (e) => {
faceHeightContainer.style.translate = `0 ${-(e.target as HTMLInputElement).valueAsNumber}px`;
});
chinlineToggle.addEventListener("input", (e) => {
if (e.target.checked) {
faceHeightContainer.classList.add("hidden");
chinlineRange.classList.add("hidden");
} else {
faceHeightContainer.classList.remove("hidden");
chinlineRange.classList.remove("hidden");
}
});
maskToggle.addEventListener("input", (e) => {
if (e.target.checked) {
imageWrapper.style.backgroundSize = "0 0";
} else {
imageWrapper.style.backgroundSize = "";
}
});
kidsMaskToggle.addEventListener("input", (e) => {
if (e.target.checked) {
imageWrapper.style.background = "url(./assets/face-mask-kids.png)";
faceHeightContainer.style.background = "url(./assets/face-height-kids.png)";
} else {
imageWrapper.style.background = "url(./assets/face-mask.png)";
faceHeightContainer.style.background = "url(./assets/face-height.png)";
}
});
let offsetX = 0;
let offsetY = 0;
let imageContents;
document.getElementById("generate-button")!.addEventListener("click", () => {
showLoading();
zoomRange.value = panzoom.getScale();
setTimeout(() => {
try {
const res = gen(new Int8Array(imageContents));
const resultBlob = new Blob([res], { type: "image/png" });
const url = URL.createObjectURL(resultBlob);
resultElement.src = url;
downloadLink.href = url;
downloadLink.download = "passbilder.png";
showResult();
} catch (e) {
reportError(e);
}
});
});
fileInput.addEventListener("change", function () {
var file = this.files[0];
var reader = new FileReader();
reader.onload = function (e) {
imageContents = e.target.result;
const img = new Blob([imageContents]);
cropperElement.src = URL.createObjectURL(img);
panzoom.pan(0, -300);
panzoom.zoom(1.5);
showCropper();
};
reader.readAsArrayBuffer(file);
});
const selectView = document.getElementById("select-view") as HTMLDivElement;
const cropView = document.getElementById("crop-view") as HTMLDivElement;
const resultView = document.getElementById("result-view") as HTMLDivElement;
const spinner = document.getElementById("spinner") as HTMLDivElement;
const dialog = document.querySelector("dialog") as HTMLDialogElement;
function showCropper() {
selectView.classList.add("hidden");
cropView.classList.remove("hidden");
resultView.classList.add("hidden");
}
function showResult() {
selectView.classList.add("hidden");
cropView.classList.add("hidden");
resultView.classList.remove("hidden");
spinner.classList.add("hidden");
}
function showSelect() {
selectView.classList.remove("hidden");
cropView.classList.add("hidden");
resultView.classList.add("hidden");
}
function showLoading() {
spinner.classList.remove("hidden");
}
for (const btn of document.getElementsByClassName("restart")) {
btn.addEventListener("click", () => {
fileInput.value = null;
showSelect();
});
}
document.querySelector("#close-dialog")!.addEventListener("click", () => {
dialog.close();
});
function reportError(error) {
spinner.classList.add("hidden");
const details = dialog.querySelector("p")!;
details.textContent =
error instanceof Error ? error.stack || error.message : error.toString();
dialog.showModal();
const data = {
error:
error instanceof Error ? error.stack || error.message : error.toString(),
image: {
clientWidth: cropperElement.clientWidth,
clientHeight: cropperElement.clientHeight,
naturalWidth: cropperElement.naturalWidth,
naturalHeight: cropperElement.naturalHeight,
translate: {
x: offsetX,
y: offsetY,
},
scale: cropperElement.style.scale,
},
};
fetch(
"https://storage.hannaweb.eu/api/collections/passphoto_errors/records",
{
method: "POST",
body: JSON.stringify({ data }),
headers: {
"Content-Type": "application/json",
},
},
).catch((e) => {
console.error("Error reporting error", e);
});
}
async function run() {
await init();
}
run();