-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path点击区域.html
48 lines (43 loc) · 1.04 KB
/
点击区域.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>点击区域</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
body {
width: 100vw;
height: 100vh;
}
#canvas {
border: 1px solid black;
margin: 200px;
}
</style>
<body>
<canvas id="canvas" width="600" height="300"></canvas>
</body>
<script>
/** @type {HTMLCanvasElement} */
let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
(() => {
ctx.beginPath();
ctx.arc(70, 80, 10, 0, 2 * Math.PI, false);
ctx.fill();
//实验性属性,需要在浏览器进行相应设置,兼容性太差。先不管。
ctx.addHitRegion({ id: "circle" });
})()
canvas.addEventListener("mousemove", event => {
console.log(event);
if (event.region) {
alert("hit region: " + event.region);
}
})
</script>
</html>