-
Notifications
You must be signed in to change notification settings - Fork 85
/
Eraser.html
42 lines (36 loc) · 1.04 KB
/
Eraser.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>橡皮擦的简单演示</title>
</head>
<body>
<canvas id="canvas" height="500" width="500">Canvas not support</canvas>
<script>
'use strict';
let canvas = document.getElementById('canvas'),
context = canvas.getContext('2d');
context.rect(0, 0, canvas.width, canvas.height);
context.fill();
context.beginPath();
canvas.onmousemove = ev => {
context.save();
let mouse = windowToCanvas(ev);
//设定橡皮擦大小
context.rect(mouse.x - 10, mouse.y - 10, 20, 20);
context.clip();
context.clearRect(0, 0, canvas.width, canvas.height);
context.restore();
};
function windowToCanvas(e) {
let x = e.x || e.clientX,
y = e.y || e.clientY,
bbox = canvas.getBoundingClientRect();
return {
x: x - bbox.left * (canvas.width / bbox.width),
y: y - bbox.top * (canvas.height / bbox.height)
};
}
</script>
</body>
</html>