Update static/script.js
Browse files- static/script.js +42 -14
static/script.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
|
2 |
const canvas = document.getElementById("canvas");
|
3 |
const ctx = canvas.getContext("2d");
|
|
|
4 |
ctx.fillStyle = "black";
|
5 |
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
6 |
ctx.strokeStyle = "white";
|
@@ -9,17 +9,46 @@ ctx.lineCap = "round";
|
|
9 |
|
10 |
let drawing = false;
|
11 |
|
12 |
-
|
13 |
-
canvas.
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
if (drawing) {
|
16 |
-
const
|
17 |
-
|
18 |
-
ctx.stroke();
|
19 |
-
ctx.beginPath();
|
20 |
-
ctx.moveTo(e.clientX - rect.left, e.clientY - rect.top);
|
21 |
}
|
22 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
function clearCanvas() {
|
25 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
@@ -37,8 +66,7 @@ function submit() {
|
|
37 |
})
|
38 |
.then(res => res.json())
|
39 |
.then(data => {
|
40 |
-
|
41 |
-
|
42 |
-
});
|
43 |
-
|
44 |
}
|
|
|
|
|
1 |
const canvas = document.getElementById("canvas");
|
2 |
const ctx = canvas.getContext("2d");
|
3 |
+
|
4 |
ctx.fillStyle = "black";
|
5 |
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
6 |
ctx.strokeStyle = "white";
|
|
|
9 |
|
10 |
let drawing = false;
|
11 |
|
12 |
+
// 滑鼠事件
|
13 |
+
canvas.addEventListener("mousedown", (e) => {
|
14 |
+
drawing = true;
|
15 |
+
draw(e.clientX, e.clientY);
|
16 |
+
});
|
17 |
+
canvas.addEventListener("mouseup", () => {
|
18 |
+
drawing = false;
|
19 |
+
ctx.beginPath();
|
20 |
+
});
|
21 |
+
canvas.addEventListener("mousemove", (e) => {
|
22 |
+
if (drawing) draw(e.clientX, e.clientY);
|
23 |
+
});
|
24 |
+
|
25 |
+
// 手機觸控事件
|
26 |
+
canvas.addEventListener("touchstart", (e) => {
|
27 |
+
e.preventDefault();
|
28 |
+
drawing = true;
|
29 |
+
const touch = e.touches[0];
|
30 |
+
draw(touch.clientX, touch.clientY);
|
31 |
+
});
|
32 |
+
canvas.addEventListener("touchmove", (e) => {
|
33 |
+
e.preventDefault();
|
34 |
if (drawing) {
|
35 |
+
const touch = e.touches[0];
|
36 |
+
draw(touch.clientX, touch.clientY);
|
|
|
|
|
|
|
37 |
}
|
38 |
+
});
|
39 |
+
canvas.addEventListener("touchend", () => {
|
40 |
+
drawing = false;
|
41 |
+
ctx.beginPath();
|
42 |
+
});
|
43 |
+
|
44 |
+
// 共用繪圖函式
|
45 |
+
function draw(x, y) {
|
46 |
+
const rect = canvas.getBoundingClientRect();
|
47 |
+
ctx.lineTo(x - rect.left, y - rect.top);
|
48 |
+
ctx.stroke();
|
49 |
+
ctx.beginPath();
|
50 |
+
ctx.moveTo(x - rect.left, y - rect.top);
|
51 |
+
}
|
52 |
|
53 |
function clearCanvas() {
|
54 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
66 |
})
|
67 |
.then(res => res.json())
|
68 |
.then(data => {
|
69 |
+
const text = `辨識結果:${data.label}\n信心值:${(data.confidence * 100).toFixed(2)}%`;
|
70 |
+
document.getElementById("result").innerText = text;
|
71 |
+
});
|
|
|
72 |
}
|