forked from ColdDay/touchRobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
129 lines (120 loc) · 3.65 KB
/
test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0, viewport-fit=cover">
<title>Document</title>
<script src="dist/touch-robot.min.js"></script>
<style>
html,body{
width: 100%;
height: 100%;
margin: 0;
}
button{
border: 2px solid #000;
font-size: 20px;
}
#canvas{
width: 100%;
height: 100%;
position: relative;
}
#canvas canvas{
display: block;
}
.pop{
position: fixed;
top: 0;
left: 0;
width: 100%;
}
</style>
</head>
<body>
<div id="canvas">
<div class="pop">
快按看彩蛋<br>
<button onclick="touchLeft()">左滑</button>
<button onclick="touchRight()">右滑</button>
<button onclick="touchTop()">上滑</button>
<button onclick="touchBottom()">下滑</button>
<button onclick="reDraw()">回放</button>
<button id="clearCanvas">清除画布</button>
</div>
</div>
</body>
<script type="text/javascript">
window.onload = function() {
new lineCanvas({
el: document.getElementById("canvas"),//绘制canvas的父级div
clearEl: document.getElementById("clearCanvas"),
});
};
var xyData = []
function lineCanvas(obj) {
this.linewidth = 1;
this.color = "#000000";
this.background = "#ffffff";
for (var i in obj) {
this[i] = obj[i];
};
this.canvas = document.createElement("canvas");
this.el.appendChild(this.canvas);
this.cxt = this.canvas.getContext("2d");
this.canvas.width = this.el.clientWidth;
this.canvas.height = this.el.clientHeight;
this.cxt.fillStyle = this.background;
this.cxt.fillRect(0, 0, this.canvas.width, this.canvas.width);
this.cxt.strokeStyle = this.color;
this.cxt.lineWidth = this.linewidth;
this.cxt.lineCap = "round";
//开始绘制
this.canvas.addEventListener("touchstart", function(e) {
xyData = []
this.cxt.beginPath();
this.cxt.moveTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
}.bind(this), false);
//绘制中
this.canvas.addEventListener("touchmove", function(e) {
xyData.push(e.changedTouches[0].pageX + ' ' + e.changedTouches[0].pageY)
this.cxt.lineTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
this.cxt.stroke();
}.bind(this), false);
//结束绘制
this.canvas.addEventListener("touchend", function() {
console.log(xyData)
this.cxt.closePath();
}.bind(this), false);
this.clearEl.addEventListener("click", function() {
this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
}.bind(this), false);
};
function clear() {
this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
function touchLeft(){
var robot = new touchRobot(document.querySelector('canvas'));
robot.touchLeft()
}
function touchRight(){
var robot = new touchRobot(document.querySelector('canvas'));
robot.touchRight()
}
function touchTop(){
var robot = new touchRobot(document.querySelector('canvas'));
robot.touchTop()
}
function touchBottom(){
var robot = new touchRobot(document.querySelector('canvas'));
robot.touchBottom()
}
function reDraw(){
document.getElementById('clearCanvas').click();
var robot = new touchRobot(document.querySelector('canvas'));
robot.touch(xyData)
}
</script>
</body>
</html>