-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
233 lines (215 loc) · 5.07 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>drag move</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no" />
<style type="text/css">
*{margin:0;padding:0;}
body{
position: relative;
width: 100%;
height: 100%;
}
canvas{
padding: 0;
background: #abcdef;
display: block;
}
#pop{
width: 100%;
height:100%;
background: rgba(0,0,0,.6);
position: fixed;
top:0;
left:0;
}
.pop_box{
width: 80%;
height: 30%;
position: absolute;
top:0;
left:0;
bottom:0;
right:0;
margin:auto;
background: #fff;
transform:rotate(90deg);
}
img{
display: none;
}
</style>
</head>
<body>
<!-- <div id="pop">
<div id="pop_box" class="pop_box">
</div>
</div> -->
<canvas id="canvas"></canvas>
<img id="bg" src="bg.jpg">
</body>
<script type="text/javascript">
var vw = window.innerWidth;
var vh = window.innerHeight;
var DURATION = 10,Y = 0;
var TOUCHEND = {};
var list = [
{
img:document.getElementById("bg"),
x:0,
y:0,
width:4.5*vw,
height:vw,
reSpeed:15,
name:"bg",
click:false
},{
x:2*vw/3,
y:2*vh/3,
width:65,
height:65,
reSpeed:-5,
color:"pink",
name:"big",
click:true,
type:"circle"
},{
x:vw/4,
y:7*vh/8,
width:40,
height:40,
reSpeed:40,
color:"red",
name:"small",
click:true,
type:"circle"
},{
x:vw/2,
y:7*vh/3,
width:50,
height:50,
reSpeed:50,
color:"green",
name:"center",
click:true,
type:"circle"
},{
x:vw/2,
y:3*vh,
width:45,
height:45,
reSpeed:35,
color:"blue",
name:"last",
click:false,
type:"circle"
},{
x:vw/2,
y:8*vw,
width:10,
height:10,
reSpeed:50,
color:"aqua",
name:"little",
click:true,
type:"circle"
}
];
var canvas = document.getElementById("canvas"),ctx = canvas.getContext("2d");
canvas.width = vw;
canvas.height = vh;
var canvasHalfY = canvas.height/2;
var mouseY = 0;
var mouseYOnMouseDown = 0;
var targetYDistance = 0;
var targetYDistanceOnMouseDown = 0;
canvas.addEventListener('touchstart', start, false);
function start(event) {//touchstart
event.preventDefault();
TOUCHEND = {x:event.touches[0].clientX,y:event.touches[0].clientY};//获取end点坐标
canvas.addEventListener( 'touchmove', move, false );
canvas.addEventListener( 'touchend', end, false );
mouseYOnMouseDown = event.touches[0].clientY - canvasHalfY;//鼠标按下y坐标 - canvasHalfX
targetYDistanceOnMouseDown = targetYDistance;//保存滑动距离
};
function move(event) {//touchmove
mouseY = event.touches[0].clientY - canvasHalfY; //鼠标移动时 y坐标 - canvasHalfX
targetYDistance = targetYDistanceOnMouseDown + ( mouseY - mouseYOnMouseDown );//滑动距离
};
function end() {//touchend
//console.log(Math.abs(targetYDistance-Y));
if(Math.abs(targetYDistance-Y)>5){
return false;
}
// 取得画布上被单击的点
var clickX = TOUCHEND.x;
var clickY = TOUCHEND.y;
// 查找被单击的圆圈
for(var i=list.length-1; i>=0; i--) {
if(list[i].click){//可点击
// 判断这个点是否在图片范围
var img = list[i];
var minX = clickX >= img.x - img.width;
var maxX = clickX <= img.x + img.width;
var minY = clickY >= img.y - img.height + Y;
var maxY = clickY <= img.y + img.height + Y;
//console.log(minX,maxX,minY,maxY)
if (minX && maxX && minY && maxY) {
clickFns[list[i].name]&&clickFns[list[i].name]();
return false;
}
};
}
}
//可点击钩子函数
var clickFns = {
big:function(){
alert("big_");
},
center:function(){
alert("center_");
},
small:function(){
alert("small_");
},
little:function(){
alert("little_");
}
}
function render() {
//console.log(targetYDistance)
if(targetYDistance<-4.5*vw+vh){//往后滑动
targetYDistance = -4.5*vw+vh;
}
if(targetYDistance>0){//往前滑动
targetYDistance = 0;
}
Y += (targetYDistance-Y) / DURATION; //获得滑动距离
ctx.clearRect(0,0,vw,vh);
for(var i=0;i<list.length;i++){
if(list[i].type == "circle"){//测试用 圆
list[i].y += (targetYDistance-Y) * list[i].reSpeed / 300;
ctx.beginPath();
ctx.fillStyle = list[i].color;
ctx.arc(list[i].x,list[i].y+Y,list[i].width,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}else{//图片
ctx.save();//保存状态
ctx.translate(vw,0);
ctx.rotate(90*Math.PI/180);
ctx.drawImage(list[i].img,list[i].x+Y,list[i].y,list[i].width,list[i].height);
ctx.restore();//恢复状态
}
}
};
(function() {//启动动画
requestAnimationFrame( arguments.callee );
render();
}());
/*pop_box.onclick = function(){
pop.style.display = 'none';
}*/
</script>
</html>