-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
77 lines (75 loc) · 2.41 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
<!DOCTYPE html>
<html lang="zn-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>画板</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<canvas id="canvas" width="100" height="100"></canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
ctx.strokeStyle = "black";
let painting = false;
let last;
canvas.width = document.documentElement.clientWidth;
canvas.height = document.documentElement.clientHeight;
function drawLine(x1, y1, x2, y2) {
ctx.beginPath();
//之前这个属性写在外面没有效果
//我猜可能是要写在beginPath之后
ctx.lineWidth = 5;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineCap = "round";
ctx.stroke();
}
// console.log(
// document.documentElement.clientWidth,
// document.documentElement.clientHeight
// );
var isTouchDevice = "ontouchstart" in document.documentElement;
console.log(isTouchDevice);
if (isTouchDevice) {
canvas.ontouchstart = (e) => {
let x = e.touches[0].clientX;
let y = e.touches[0].clientY;
last = [x, y];
console.log(last);
};
canvas.ontouchmove = (e) => {
//在手机上支持多点触控,这时候触摸可能就是一个属性,所以我们需要获取第一个的值
let x = e.touches[0].clientX;
let y = e.touches[0].clientY;
drawLine(last[0], last[1], x, y);
last = [x, y];
};
} else {
canvas.onmousedown = (e) => {
painting = true;
last = [e.clientX, e.clientY];
};
canvas.onmousemove = (e) => {
if (painting === true) {
// console.log(e.clientX);
// console.log(e.clientY);
drawLine(last[0], last[1], e.clientX, e.clientY);
last = [e.clientX, e.clientY];
// ctx.beginPath();
// ctx.arc(e.clientX, e.clientY, 10, 0, 2 * Math.PI);
// ctx.stroke();
// ctx.fill();
} else {
console.log("do none");
}
};
canvas.onmouseup = () => {
painting = false;
};
}
</script>
</body>
</html>