-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
146 lines (130 loc) · 4.77 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
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script src="digit.js"></script>
</head>
<body>
<div style="position: relative; margin: 0 auto;width: 1300px;height: 768px">
<canvas width="1300" height="768" id="layer1"
style=" position: absolute; z-index: 0;border: 2px black dashed;"></canvas>
<canvas width="1300" height="768" id="layer2"
style="position: absolute; left: 0; top: 0; z-index: 1;border: 2px black dashed;"></canvas>
</div>
<script>
var canvas = document.getElementById('layer1');
var canvas2 = document.getElementById('layer2');
var context = canvas.getContext('2d');
var context2 = canvas2.getContext('2d');
var colors = ["#33B5E5","#0099CC","#AA66CC","#9933CC","#99CC00","#669900","#FFBB33","#FF8800","#FF4444","#CC0000"];
var hour = 24;
var min = 20;
var sec = 10;
var balls = [];
/*
* 根据x,y,r绘制圆形
*/
function drawCircle(ctx,x,y,r,color){
if(!color){
color = "rgb(0,102,153)";
}
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x,y,r,0,2*Math.PI);
ctx.closePath();
ctx.fill();
}
/*
* 根据数字矩阵绘制数字
* 构建了一个私有变量_idx,来检测是否跟上次的数字相同,如果不同就重绘并且加入新的小球
*/
function drawNum(){
var _idx ;
return function(ctx,idx,left,top){
if(idx != _idx){
var num = digit[idx];
context.clearRect(left-8,top-8,num[0].length*20-4, num.length*20-4);
for(var i=0; i<num[0].length;i++){
for(var j=0;j<num.length;j++){
if(num[j][i] == 1){
drawCircle(ctx,left+i*20,top+j*20,8);
balls.push({
left:left+i*20,
top:top+j*20,
vx:Math.pow( -1 , Math.ceil( Math.random()*1000 ) ) * 4,
vy:-5,
g:1.5+Math.random(),
color:colors[ Math.floor( Math.random()*colors.length ) ]
});
}
}
}
_idx = idx;
}
};
}
/*
* 绘制时钟
*/
function showClock(){
var left = 100;
var right= 100;
var hour1 = drawNum();
var hour2 = drawNum();
var maohao1 = drawNum();
var min1 = drawNum();
var min2 = drawNum();
var maohao2 = drawNum();
var sec1 = drawNum();
var sec2 = drawNum();
return function(){
hour1(context,Math.floor(hour/10),left,right);
hour2(context,Math.floor(hour%10),left+160,right);
maohao1(context,10,left+320,right);
min1(context,Math.floor(min/10),left+420,right);
min2(context,Math.floor(min%10),left+580,right);
maohao2(context,10,left+740,right);
sec1(context,Math.floor(sec/10),left+840,right);
sec2(context,Math.floor(sec%10),left+1000,right);
};
}
function throwBall(){
for(var i=0;i<balls.length;i++){
balls[i].left += balls[i].vx;
balls[i].top += balls[i].vy;
balls[i].vy += balls[i].g;
if(balls[i].top>760){
balls[i].top = 760;
balls[i].vy = -balls[i].vy * 0.5;
/* if(Math.abs(balls[i].vy)<8){
balls[i].vy = 0;
}*/
}
drawCircle(context2,balls[i].left,balls[i].top,8,balls[i].color);
}
}
/*
* 更新时间
*/
var s =showClock();
function update(){
var date = new Date();
hour = date.getHours();
min = date.getMinutes();
sec = date.getSeconds();
context2.clearRect(0,0,1300, 768);
s();
throwBall();
while( balls.length > 500 ){
balls.shift();
}
}
(function(){
setInterval(update,50);
context.font = "50px Microsoft YaHei,SimSun,Arial";
context.fillText("炫丽时钟效果,改编自慕课网",100,400);
context.fillText("—by xiaoqqchen",150,450);
})();
</script>
</body>
</html>