用canvas实现简易画板工具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<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>涂鸦</title>
<style>
#canvas1{
box-shadow: 0 5px 40px black;
}
</style>
</head>
<body>
<canvas id="canvas1" width="2000px" height="1000px"></canvas>
</body>
<script>
var canvas = document.getElementById('canvas1');
var context = canvas.getContext('2d')
canvas.onmousedown = function (e) {
var ev = e||window.event;
var x = ev.clientX - canvas.offsetLeft;
var y = ev.clientY - canvas.offsetTop;
context.beginPath();
context.moveTo(x,y);
canvas.onmousemove = function (e) {
var ev = e||window.event;
var x = ev.clientX - canvas.offsetLeft;
var y = ev.clientY - canvas.offsetTop;
console.log(x)
context.lineWidth = 5;
context.strokeStyle = "red";
context.lineTo(x,y);
context.stroke()
}
canvas.onmouseup = function () {
canvas.onmousemove = null;
}
}
</script>
</html>