-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsg.html
59 lines (59 loc) · 1.51 KB
/
sg.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
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
* { margin: 0; padding: 0; }
</style>
</head>
<body>
<canvas style="width:100%;height:500px;" height="500"/>
<script type="text/javascript">
function painting(beginX,beginY,stopX,stopY,ctx) {
ctx.beginPath();
ctx.globalAlpha = 1;
ctx.lineWidth = 3;
ctx.strokeStyle = 'steelblue';
ctx.moveTo(beginX,beginY);
ctx.lineTo(stopX,stopY);
ctx.closePath();
ctx.stroke();
}
let canvas = document.getElementsByTagName('canvas')[0];
let width = window.innerWidth;
canvas.width = width;
let ctx = canvas.getContext('2d');
canvas.onmousedown = function(e) {
let beginX = e.clientX,
beginY = e.clientY
// painting(beginX,beginY,beginX+1,beginY+1,ctx);
// beginX++;
// beginY++;
canvas.onmousemove = function(e) {
let stopX = e.clientX,
stopY = e.clientY
painting(beginX,beginY,stopX,stopY,ctx);
beginX = stopX;
beginY = stopY;
}
document.onmouseup = function(e) {
canvas.onmousemove = null;
document.onmouseup = null;
}
}
canvas.ontouchstart = function(e) {
e.preventDefault();
let beginX = e.touches[0].clientX,
beginY = e.touches[0].clientY
canvas.ontouchmove = function(e) {
e.preventDefault();
let stopX = e.touches[0].clientX,
stopY = e.touches[0].clientY
painting(beginX,beginY,stopX,stopY,ctx);
beginX = stopX;
beginY = stopY;
}
}
</script>
</body>
</html>