-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
290 lines (243 loc) · 7.95 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0" />
<!--<meta name="apple-touch-fullscreen" content="yes" />-->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection" content="telephone=no" />
<title>基于重力加速度感应器的小球</title>
<style>
.output{
position: absolute;;
top:0;
left:0;
color:#FFF;
}
.menu{
position: absolute;
top:24px;
left:10px;
height:30px;
padding:2px 5px;
border:1px solid #3A3A3A;
border-radius:20px;
-webkit-border-radius:20px;
background: -webkit-linear-gradient(top,#4A4A4A 0%,#3F4550 100%);
opacity:0.82;
}
.menu ul{
-webkit-padding-start:0;
-webkit-margin-before:0;
height: 30px;
padding-right: 5px;
padding-left: 10px;
display: inline-block;
}
.menu ul li{
display: inline-block;
line-height: 30px;
}
.menu ul li:after{
content:' |';
color:#FFF;
}
.menu ul li:last-child:after{
content:'';
}
.menu a{
color:#FFF;
text-decoration: none;
line-height: 30px;
}
.menu .btn-ball{
display: inline-block;
height:30px;
width:30px;
box-shadow:0px 1px 1px #000;
-webkit-border-radius:15px;
background: -webkit-linear-gradient(top,#FFBC24 0%,#FF7800 100%);
opacity:1;
}
.menu .arrow{
display: inline-block;;
width: 10px;
height:10px;
text-align: center;
border-bottom: 2px solid #FFF;
border-right:2px solid #FFF;
-webkit-transform:rotate(-45deg);
margin-left: 6px;
margin-top:9px;
}
body{
margin:0;
}
</style>
</head>
<body>
<div class="menu">
<ul>
<li><a href="#">滚球</a></li>
<!--<li><a href="#">简介</a></li>
<li><a href="#">返回</a></li>-->
</ul>
<div class="btn-ball"><span class="arrow"></span></div>
</div>
<div id="loading" class="loading"></div>
<canvas id="ball">对不起,你当前的浏览器不支持Canvas标签。</canvas>
<script type="text/javascript">
var cur_x = 0,cur_y = 0;
var ball_radius = 50;
var initialized = false;
var ww,wh;
var speed_x = 0,speed_y = 0;
var accel_x,accel_y;
var friction_accel = 0.02;
var interval = 33; //ms, 30 fps
var bg_color = "#EEEEEE";
var fg_color = "#333";
var absorbing_rate = 0.5;
var opera_pix_bg=new Image();
opera_pix_bg.src="img/bg.jpg";
//小球图片源文件名
var ballImages = ['img/ball-1.png','img/ball-2.png','img/ball-3.png','img/ball-4.png','img/ball-5.png','img/ball-6.png'],
//存放小球图片对象
ballImagesObj = [],
//小球图片总数
ballImagesLen = ballImages.length,
//加载图片统计
loadingCount = 0,
//加载循环变量
loadTimeout,
//isChangeImage = false,
ballIndex = 0;
function $(id){
return document.getElementById(id);
}
//初始化图片对象
function setImages(){
for(var i=0;i<ballImagesLen;i++){
var tmp = new Image();
tmp.src = ballImages[i];
tmp.onload = function(){
loadingCount++;
}
ballImagesObj.push(tmp);
}
}
//增加loading提示加载图片
function loadImages(){
if(loadingCount == ballImagesLen){
window.clearTimeout(loadTimeout);
init();
return;
}
loadTimeout = window.setTimeout('loadImages()',100);
}
//随机生成小球索引
function randomBallIndex(){
var tempIndex = Math.round(Math.random()*(ballImagesLen - 1));
if(ballIndex == tempIndex){
randomBallIndex();
}else{
ballIndex = tempIndex;
}
}
//判断方向
function inSameDirection(a, b){
return a > 0 && b > 0 || a <= 0 && b <= 0;
}
//返回位置坐标和速度
function getCurpos(cur_pos, speed, accel, boundary){
if (speed == 0 && Math.abs(accel) <= friction_accel)
return [cur_pos, speed];
start_speed = speed;
f_accel = (speed > 0 ? -friction_accel:friction_accel);
speed += accel + f_accel;
if (!inSameDirection(f_accel, accel) &&
!inSameDirection(f_accel, start_speed) &&
!inSameDirection(start_speed, speed))
{
speed = 0;
}
cur_pos += (start_speed + speed)/2;
if (cur_pos > boundary - ball_radius)
{
cur_pos= boundary - ball_radius;
speed = -speed * absorbing_rate;
randomBallIndex();
}
else if (cur_pos < 0)
{
cur_pos = 0;
speed = -speed * absorbing_rate;
randomBallIndex();
}
return [cur_pos, speed];
}
//实时设置当前坐标值
function physics(){
var x = getCurpos(cur_x, speed_x, accel_x, ww);
cur_x = x[0];
speed_x = x[1];
var y = getCurpos(cur_y, speed_y, accel_y, wh);
cur_y = y[0];
speed_y = y[1];
}
//画图
function paint(){
var ball_canvas = document.getElementById('ball');
var ctx = ball_canvas.getContext('2d');
ctx.save();
physics();
ctx.drawImage(opera_pix_bg, 0,0, ww, wh );
ctx.drawImage(ballImagesObj[ballIndex], cur_x,cur_y, 50, 50 );
ctx.restore();
setTimeout("paint()", interval);
}
//清除canvas画布
function clearCanvas(){
var ball_canvas = document.getElementById('ball');
var ctx = ball_canvas.getContext('2d');
ctx.fillRect(0,0, ww, wh);
}
//更新canvas为全屏状态,并开始实时跟踪小球位置
function update(evt){
accel_x = Math.sin(evt.gamma / 180 * Math.PI);
accel_y = Math.sin(evt.beta / 180 * Math.PI);
if (!initialized)
{
cur_x = ww /2 + ball_radius;
cur_y = wh /2 + ball_radius;
initialized = true;
//clearCanvas();
paint();
}
}
//预加载
function preload(){
var ball_canvas = document.getElementById('ball');
if (ball_canvas.width != window.outerWidth ||
ball_canvas.height != window.outerHeight)
{
ball_canvas.width = window.outerWidth;
ball_canvas.height = window.outerHeight;
}
ww = ball_canvas.width;
wh = ball_canvas.height;
paint();
}
//初始化所有
function init(){
preload();
window.addEventListener('deviceorientation', update, true);
}
(function(){
setImages();
loadImages();
})();
</script>
</body>
</html>