1
+ var c = document . getElementById ( 'canvas' ) ;
2
+ c . width = document . body . clientWidth ;
3
+ c . height = document . body . clientHeight ;
4
+
5
+ // 全局常量
6
+ var marginLeft = Math . round ( c . width / 20 ) ;
7
+ var marginTop = Math . round ( c . height / 5 ) ;
8
+ var radius = Math . round ( c . width * 4.5 / 5 / 108 ) - 1 ;
9
+
10
+ // 全局变量
11
+ curHours = new Date ( ) . getHours ( ) ;
12
+ curMinutes = new Date ( ) . getMinutes ( ) ;
13
+ curSeconds = new Date ( ) . getSeconds ( ) ;
14
+
15
+
16
+
17
+ // 兼容性检测
18
+ if ( c . getContext ) {
19
+ var ctx = c . getContext ( '2d' ) ;
20
+ // drawing code here
21
+ } else {
22
+ alert ( "Your browser doesn't support it Canvas" )
23
+ }
24
+
25
+
26
+ setInterval ( function ( ) {
27
+ draw . clock ( ) ;
28
+ contrast ( ) ;
29
+
30
+ } , 50 ) ;
31
+
32
+ function contrast ( ) {
33
+ nextHours = new Date ( ) . getHours ( ) ;
34
+ nextMinutes = new Date ( ) . getMinutes ( ) ;
35
+ nextSeconds = new Date ( ) . getSeconds ( ) ;
36
+
37
+ // 当前时和下次时的个位十位数比较
38
+ if ( parseInt ( curHours / 10 ) != parseInt ( nextHours / 10 ) ) {
39
+ console . log ( `当前时${ curSeconds } 下次时${ nextSeconds } ` ) ;
40
+ ball . add ( marginLeft + 0 , marginTop , parseInt ( nextMinutes / 10 ) ) ;
41
+
42
+ }
43
+ if ( parseInt ( curHours % 10 ) != parseInt ( nextHours % 10 ) ) {
44
+ console . log ( `当前时${ curSeconds } 下次时${ nextSeconds } ` ) ;
45
+ ball . add ( marginLeft + 15 * ( radius + 1 ) , marginTop , parseInt ( nextMinutes / 10 ) ) ;
46
+
47
+ }
48
+
49
+ // 当前分和下次时的个位十位数比较
50
+ if ( parseInt ( curMinutes / 10 ) != parseInt ( nextMinutes / 10 ) ) {
51
+ console . log ( `当前分${ curSeconds } 下次分${ nextSeconds } ` ) ;
52
+ ball . add ( marginLeft + 38 * ( radius + 1 ) , marginTop , parseInt ( nextMinutes / 10 ) )
53
+
54
+ }
55
+ if ( parseInt ( curMinutes % 10 ) != parseInt ( nextMinutes % 10 ) ) {
56
+ console . log ( `当前分${ curSeconds } 下次分${ nextSeconds } ` ) ;
57
+ ball . add ( marginLeft + 53 * ( radius + 1 ) , marginTop , parseInt ( nextMinutes % 10 ) )
58
+ }
59
+
60
+ // 当前秒和下次时的个位十位数比较
61
+ if ( parseInt ( curSeconds / 10 ) != parseInt ( nextSeconds / 10 ) ) {
62
+ console . log ( `当前秒${ curSeconds } 下次秒${ nextSeconds } ` ) ;
63
+ ball . add ( marginLeft + 76 * ( radius + 1 ) , marginTop , parseInt ( nextSeconds / 10 ) )
64
+ }
65
+ if ( parseInt ( curSeconds % 10 ) != parseInt ( nextSeconds % 10 ) ) {
66
+ console . log ( `当前秒${ curSeconds } 下次秒${ nextSeconds } ` ) ;
67
+ ball . add ( marginLeft + 91 * ( radius + 1 ) , marginTop , parseInt ( nextSeconds % 10 ) ) ;
68
+
69
+ }
70
+
71
+
72
+ // 当前的等于下次的
73
+ curHours = nextHours ;
74
+ curMinutes = nextMinutes ;
75
+ curSeconds = nextSeconds ;
76
+ ball . up ( ) ;
77
+ console . log ( `当前页面有${ ball . message . length } 个小球` ) ;
78
+
79
+
80
+ }
81
+ // 小球
82
+ var ball = {
83
+ colors : [
84
+ "#666699" ,
85
+ "#0099CC" ,
86
+ "#CC3399" ,
87
+ "#FFFF00" ,
88
+ "#009933" ,
89
+ "#CC6600" ,
90
+ "#FF6666" ,
91
+ "#CCCCCC" ,
92
+ "#009999" ,
93
+ "#FF0033"
94
+ ] ,
95
+ message : [ ] ,
96
+ // 添加小球
97
+ add : function ( x , y , num ) {
98
+ for ( var i = 0 ; i < digit [ num ] . length ; i ++ ) {
99
+ for ( var j = 0 ; j < digit [ num ] [ i ] . length ; j ++ ) {
100
+ if ( digit [ num ] [ i ] [ j ] == 1 ) {
101
+ var aBall = {
102
+ x : x + j * 2 * ( radius + 1 ) + ( radius + 1 ) ,
103
+ y : y + i * 2 * ( radius + 1 ) + ( radius + 1 ) ,
104
+ g : 1.5 + Math . random ( ) ,
105
+ angle : "90deg" ,
106
+ vx : Math . pow ( - 1 , Math . ceil ( Math . random ( ) * 1000 ) ) * 4 ,
107
+ vy : - 5 ,
108
+ color : ball . colors [ Math . floor ( Math . random ( ) * ball . colors . length ) ]
109
+ } ;
110
+
111
+ ball . message . push ( aBall ) ;
112
+ }
113
+ }
114
+ }
115
+ } ,
116
+
117
+ up : function ( ) {
118
+ for ( var i = 0 ; i < ball . message . length ; i ++ ) {
119
+ ball . message [ i ] . x += ball . message [ i ] . vx ;
120
+ ball . message [ i ] . y += ball . message [ i ] . vy ;
121
+ ball . message [ i ] . vy += ball . message [ i ] . g ;
122
+ if ( ball . message [ i ] . y >= c . height - radius ) {
123
+ ball . message [ i ] . y = c . height - radius ;
124
+ ball . message [ i ] . vy = - ball . message [ i ] . vy * 0.62 ;
125
+ }
126
+ }
127
+
128
+ var cnt = 0 ;
129
+ for ( var i = 0 ; i < ball . message . length ; i ++ ) {
130
+ if (
131
+ ball . message [ i ] . x + radius > 0 &&
132
+ ball . message [ i ] . x - radius < c . width
133
+ ) {
134
+ ball . message [ cnt ++ ] = ball . message [ i ] ;
135
+ }
136
+ }
137
+
138
+ while ( ball . message . length > cnt ) {
139
+ ball . message . pop ( ) ;
140
+ }
141
+ }
142
+ } ;
143
+
144
+
145
+ // 绘制时钟
146
+ var draw = {
147
+
148
+ clock : function ( ) {
149
+ ctx . clearRect ( 0 , 0 , c . width , c . height ) ;
150
+ var time = new Date ( ) ;
151
+ var hours = time . getHours ( ) ;
152
+ var minutes = time . getMinutes ( ) ;
153
+ var seconds = time . getSeconds ( ) ;
154
+ console . log ( hours , minutes , seconds ) ;
155
+
156
+ // 时
157
+ draw . renderDigit ( marginLeft , marginTop , parseInt ( hours / 10 ) , ctx ) ;
158
+ draw . renderDigit ( marginLeft + 15 * ( radius + 1 ) , marginTop , parseInt ( hours % 10 ) , ctx ) ;
159
+ draw . renderDigit ( marginLeft + 30 * ( radius + 1 ) , marginTop , 10 , ctx ) ;
160
+
161
+ // 分
162
+ draw . renderDigit ( marginLeft + 38 * ( radius + 1 ) , marginTop , parseInt ( minutes / 10 ) , ctx ) ;
163
+ draw . renderDigit ( marginLeft + 53 * ( radius + 1 ) , marginTop , parseInt ( minutes % 10 ) , ctx ) ;
164
+ draw . renderDigit ( marginLeft + 68 * ( radius + 1 ) , marginTop , 10 , ctx ) ;
165
+
166
+ // 秒
167
+ draw . renderDigit ( marginLeft + 76 * ( radius + 1 ) , marginTop , parseInt ( seconds / 10 ) , ctx ) ;
168
+ draw . renderDigit ( marginLeft + 91 * ( radius + 1 ) , marginTop , parseInt ( seconds % 10 ) , ctx ) ;
169
+
170
+ // 绘制彩色小球
171
+ for ( var i = 0 ; i < ball . message . length ; i ++ ) {
172
+ ctx . fillStyle = ball . message [ i ] . color ;
173
+ ctx . beginPath ( ) ;
174
+ ctx . arc ( ball . message [ i ] . x , ball . message [ i ] . y , radius , 0 , 2 * Math . PI ) ;
175
+ // ctx.rect(ball.message[i].x, ball.message[i].y, radius * 2, radius * 2);
176
+ ctx . closePath ( ) ;
177
+ ctx . fill ( ) ;
178
+ }
179
+
180
+ } ,
181
+
182
+ // 绘制函数
183
+ renderDigit : function ( x , y , num ) {
184
+
185
+ ctx . fillStyle = "#0099CC" ;
186
+
187
+ for ( var i = 0 ; i < digit [ num ] . length ; i ++ ) {
188
+ for ( var j = 0 ; j < digit [ num ] [ i ] . length ; j ++ ) {
189
+ // 计算圆心的位置
190
+ // ctxX:x+j*2*(R+1)+(R+1)
191
+ // ctxY:y+i*2*(R+1)+(R+1)
192
+ if ( digit [ num ] [ i ] [ j ] == 1 ) {
193
+ ctx . beginPath ( ) ;
194
+ ctx . arc (
195
+ x + j * 2 * ( radius + 1 ) + ( radius + 1 ) ,
196
+ y + i * 2 * ( radius + 1 ) + ( radius + 1 ) ,
197
+ radius ,
198
+ 0 ,
199
+ 2 * Math . PI
200
+ ) ;
201
+ // ctx.rect(x + j * 2 * (radius + 1) + (radius + 1), y + i * 2 * (radius + 1) + (radius + 1), radius * 2, radius * 2);
202
+ ctx . closePath ( ) ;
203
+ ctx . fill ( ) ;
204
+ }
205
+ }
206
+ }
207
+ }
208
+ } ;
0 commit comments