HTML5 上下翻页时钟

0
4790

因为博客首页有个Flash的时钟,对于大多数浏览器Flash是没有问题的,而Firefox等就稍微有些麻烦了,而且IOS系统是Flash的死对头。很早之前就想着上个HTML5的时钟来替代这个Flash,今天从一个网站弄了点源码,修改了下,总算完工了。

效果暂且可见:Html5 Clock

不过问题又来了,这个是我整合一个JS,调用不同大小的话又得拆开。拆开的话Http请求次数有增加,还有最严重的一个问题,之前的Flash才4KB,这个整合以后的文件16KB,纠结啊。看看能不能到时候再精简下。

20141004-01

PS:185行的urlSize为显示的宽度,高度自动适应。

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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<html>
<head></head><body>
<script language="JavaScript" type="text/javascript">
/*****************************************
 *
 *	[No need to change] Cross browser requestAnimationFrame
 *  To know more detail, go to the following link
 *  http://paulirish.com/2011/requestanimationframe-for-smart-animating/
 *
*****************************************/
window.requestAnimFrame = (function(callback) {
	var agent = navigator.userAgent
	if(agent.search(/iPhone/) != -1 || agent.search(/iPod/) != -1 || agent.search(/iPad/) != -1){
		return function(callback) {
			window.setTimeout(callback, 1000 / 60);
		};
	}
	return window.requestAnimationFrame ||
		window.webkitRequestAnimationFrame ||
		window.mozRequestAnimationFrame ||
		window.oRequestAnimationFrame ||
		window.msRequestAnimationFrame ||
		function(callback) {
			window.setTimeout(callback, 1000 / 60);
		};
})();
 
/*****************************************
 *
 *  [No need to change] AnimFrame Constructor
 *  For manageing the plural animation canvases.
 *  Usage:
 *   1. Instantiate this Constructor: var animeFrame = new AnimFrame();
 *   2. Set animation objects extended from AnimCanvas Constructor: animeFrame.push(anim01); animeFrame.push(anim02); ...
 *   3. Call the start method: animeFrame.start();
 *   4. Be able to stop animations by calling the stop method (no used in Clocklinks): animeFrame.stop();
 *
*****************************************/
var AnimFrame = (function(){
	function F(){}
	var stack = [];
	var isAnimating;
 
	F.prototype.push = function(instance){
		stack.push(instance);
	};
	F.prototype.stop = function(){
		isAnimating = false;
	};
	F.prototype.start = function(){
		isAnimating = true;
		init();
		animate();
	};
 
	function init(){
		for(var i=0, l=stack.length; i<l; i++) {
			stack[i].init();
		}
	}
 
	function animate(){
		if(isAnimating) {
			requestAnimFrame(function() {
				animate();
			});
		}
		for(var i=0, l=stack.length; i<l; i++) {
			stack[i].render();
		}
	};
	return F;
})();
 
/*****************************************
 *
 *  [No need to change] AnimCanvas Constructor
 *  This is used as a base of animation canvas.
 *
*****************************************/
var AnimCanvas = (function(){
	/* Constructor */
	function F(){
		this.id;
	    this.canvas;
	    this.context;
		this.time;
	    this.startTime;
		this.fps;
		this.fpsStep;
		this.fpsTime;
	}
 
	/* Public Methods */	
	// setCanvas() is required to call for identifying the canvas to use
	F.prototype.setCanvas = function(canvasId){
		this.id = canvasId;
		this.canvas = document.getElementById(this.id);
		this.context = this.canvas.getContext("2d");
	};
 
	// createCanvas() is required to call for 
	F.prototype.createCanvas = function(width, height){
		this.canvas = document.createElement("canvas");
		this.context = this.canvas.getContext("2d");
		this.canvas.width = width;
		this.canvas.height = height;
	};
 
	// setFps() is arbitrary to change the fps
	// if not called, the canvas is rendered right when animation gets ready
	F.prototype.setFps = function(fps){
		this.fps = fps;
		this.fpsStep = 1000 / fps;
		this.fpsFrame;
	};
 
	// init() is called by the AnimFrame constructor when starting Animation
	F.prototype.init = function(){
		this.startTime = (new Date()).getTime();
	};
 
	// render() is called by the AnimFrame constructor each time to render
	F.prototype.render = function(){
		this.time = (new Date()).getTime() - this.startTime;
 
		if(this.fps){
			var millisecond = this.time % 1000;
			var currentFpsFrame = Math.floor(millisecond / this.fpsStep);
			if(this.fpsFrame != currentFpsFrame){
				this.fpsFrame = currentFpsFrame;
				this.draw();
			}
		} else {
			this.draw();
		}
	};
	return F;
})();
 
/*****************************************
 *
 *	SimpleCanvas Constructor
 *
*****************************************/
var SimpleCanvas = (function(){
	/* Constructor */
	function F(){
		this.id;
	    this.canvas;
	    this.context;
	}
 
	/* Public Methods */	
	// setCanvas() is required to call for identifying the canvas to use
	F.prototype.setCanvas = function(canvasId){
		this.id = canvasId;
		this.canvas = document.getElementById(this.id);
		this.context = this.canvas.getContext("2d");
	};
 
	// createCanvas() is required to call for gerating a new canvas object
	F.prototype.createCanvas = function(width, height){
		this.canvas = document.createElement("canvas");
		this.context = this.canvas.getContext("2d");
		this.canvas.width = width;
		this.canvas.height = height;
	};
 
	// render() is called by the AnimFrame constructor each time to render
	F.prototype.render = function(){
		this.draw();
	};
	return F;
})();
 
// Now is Html5 Clock begin, above all is render frame.
 
function isCanvasSupported(){
	var elem = document.createElement("canvas");
	return !!(elem.getContext && elem.getContext("2d"));
}
var urlClock = 'Clock-Html5';
var urlTimezone = 'CCT';
var urlSize = '271';
var urlTitle = '';
var urlMessage = '';
var urlTarget = '';
var urlFrom = '2014,1,1,0,0,0';
var urlColor = 'gray';
//var adImageUrl = "";
//var redirectUrl = "";
var serverYear = new Date().getFullYear();
var serverMonth = new Date().getMonth();
var serverDay = new Date().getDay();
var serverHour = new Date().getHours();
var serverMinute = new Date().getMinutes();
var serverSecond = new Date().getSeconds();
var serverMillisecond = new Date().getMilliseconds();
var rootPath = "/";
//var adId = "Clock-Html5-AD";
 
/*
 Global valuables
 - urlClock
 - urlTimezone
 - urlColor
 - urlSize
 - serverYear
 - serverMonth
 - serverDay
 - serverHour
 - serverMinute
 - serverSecond
 - serverMillisecond
 */
 
var baseSize = 227;
var srcPath = rootPath + "html5-008/";
 
document.write('<canvas id="' + urlClock + '" width="' + urlSize + 'px" height="' + urlSize/2.91 + 'px"></canvas>');
 
/*****************************************
 *
 *	Clock Constructor extended by AnimCanvas
 *
*****************************************/
var Clock = (function(){
	/* Constructor */
	function F(){}
 
	/* Inheritance */
	F.prototype = new AnimCanvas();
	F.prototype.__super__ = AnimCanvas.prototype;
	F.prototype.init = function(){
		this.__super__.init.apply(this, arguments);
 
		var servertime = new Date();
		servertime.setYear(serverYear);
		servertime.setMonth(serverMonth - 1);
		servertime.setDate(serverDay);
		servertime.setHours(serverHour);
		servertime.setMinutes(serverMinute);
		servertime.setSeconds(serverSecond);
		unixservertime = servertime.getTime();
	};
 
	/* Private Valuables (don't change the values) */
	var canvas, context, time;
	var unixservertime,
		currenttime;
	var hour,
		minute,
		second,
		millisecond;
	var previousHour,
		previousMinute,
		previousSecond;
	var scaleValue = 1;
 
	/* Private Functions */
	function countTime(){
		currenttime = new Date();
		currenttime.setTime(unixservertime + time);
		hour   = currenttime.getHours();
		minute = currenttime.getMinutes();
		second = currenttime.getSeconds();
		millisecond = currenttime.getMilliseconds()
	}
 
 
	/* Public Methods */
	F.prototype.setScale = function(val){
		scaleValue = val;
	};
	F.prototype.draw = function(){
		canvas = this.canvas;
		context = this.context;
		time = this.time;
 
		/* Clear and save initial setting  */
		context.clearRect(0, 0, canvas.width, canvas.height);
		context.save();
 
		/********* Draw from here *********/
		countTime();
		context.translate(0, canvas.height/2);
		context.scale(scaleValue, scaleValue);
 
		// hour panel
		hourPanel.setNumber(hour);
		context.drawImage(hourPanel.canvas, 0, -hourPanel.canvas.height/2);
 
		// minute panel
		minutePanel.setNumber(minute);
		context.drawImage(minutePanel.canvas, 77, -minutePanel.canvas.height/2);
 
		// second panel
		secondPanel.setNumber(second);
		context.drawImage(secondPanel.canvas, 154, -secondPanel.canvas.height/2);
 
		/********* Revert to initial setting *********/
		context.restore();
	}
	return F;
})();
 
 
 
/*****************************************
 *
 *	ClockPanel Constructor extended by AnimCanvas
 *
*****************************************/
var ClockPanel = (function(){
	/* Constructor */
	function F(){}
 
	/* Inheritance */
	F.prototype = new AnimCanvas();
	F.prototype.__super__ = AnimCanvas.prototype;
	F.prototype.init = function(){
		this.__super__.init.apply(this, arguments);
		this.currentNum = 0;
		this.previousNum = 0;
		this.count = 0;
	};
 
	/* Private Valuables (don't change the values) */
	var canvas, context, time;
	var flipFrame = 10;
 
	/* Public Methods */
	F.prototype.draw = function(){
		canvas = this.canvas;
		context = this.context;
		time = this.time;
 
		/* Clear and save initial setting  */
		context.clearRect(0, 0, canvas.width, canvas.height);
		context.save();
 
		/********* Draw from here *********/
		if(this.currentNum == this.previousNum){
			context.drawImage(this.panelList[this.currentNum].canvas, 0, 0);			
		} else {
			this.count++;
 
			var progress = Math.cos(Math.PI * this.count / flipFrame);
			var currentPanel = this.panelList[this.currentNum].canvas;
			var previousPanel = this.panelList[this.previousNum].canvas;
 
			context.save();
			context.translate(0, currentPanel.height/2);
 
			// Upper Current Panel
			context.drawImage(currentPanel, 0, 0, currentPanel.width, currentPanel.height/2, 0, -currentPanel.height/2, currentPanel.width, currentPanel.height/2);
			// Lower Previous Panel
			context.drawImage(previousPanel, 0, previousPanel.height/2, previousPanel.width, previousPanel.height/2, 0, 0, previousPanel.width, previousPanel.height/2);
 
			if(progress>0){
				// Upper Previous Panel
				context.drawImage(previousPanel, 0, 0, previousPanel.width, previousPanel.height/2, 0, -previousPanel.height/2*progress, previousPanel.width, previousPanel.height/2*progress);
			} else {
				progress = progress* -1;
				// Lower Current Panel
				context.drawImage(currentPanel, 0, currentPanel.height/2, currentPanel.width, currentPanel.height/2, 0, 0, currentPanel.width, currentPanel.height/2*progress);
			}
 
			context.restore();
 
			if(this.count==flipFrame){
				this.count=0;
				this.previousNum = this.currentNum;
			}
 
		}
 
 
		/********* Revert to initial setting *********/
		context.restore();
	}
	F.prototype.setPanelList = function(array){
		this.panelList = array;
	}
 
	F.prototype.setNumber = function(num){
		this.currentNum = num;
	}
	return F;
})();
 
 
/*****************************************
 *
 *	General60 Constructor extended by AnimCanvas
 *
*****************************************/
var General60 = (function(){
	/* Constructor */
	function F(){}
 
	/* Inheritance */
	F.prototype = new SimpleCanvas();
 
	/* Private Valuables */
	var canvas, context, time;
	var corderRound = { x : 10, y : 8 }
 
	/* Public Methods */
	F.prototype.draw = function(){
		canvas = this.canvas;
		context = this.context;
		time = this.time;
 
		/* Clear and save initial setting  */
		context.clearRect(0, 0, canvas.width, canvas.height);
		context.save();
 
		/********* Draw from here *********/
		// Create Clip Path
		context.beginPath();
		context.moveTo(0, corderRound.y);
		context.quadraticCurveTo(0, 0, corderRound.x, 0);
		context.lineTo(canvas.width-corderRound.x, 0);
		context.quadraticCurveTo(canvas.width, 0, canvas.width, corderRound.y);
		context.lineTo(canvas.width, canvas.height-corderRound.y);
		context.quadraticCurveTo(canvas.width, canvas.height, canvas.width-corderRound.x, canvas.height);
		context.lineTo(corderRound.x, canvas.height);
		context.quadraticCurveTo(0, canvas.height, 0, canvas.height-corderRound.y);
		context.closePath();
		// context.clip();
 
		// Fill Background Color
		context.fillStyle = this.color;
		context.fill();
		// context.fillRect(0, 0, canvas.width, canvas.height);
 
		// Draw the Number
		context.font = "62px Arial"
		context.textAlign = "center";
		context.textBaseline = "Alphabetic";
		context.fillStyle = "#ffffff";
		if(this.color.toLowerCase() == "white" || this.color.toLowerCase() == "#ffffff"){
			context.fillStyle = "#000000";
		}
		context.fillText(this.number, canvas.width/2, canvas.height-8.5);
 
		// Draw Center Line
		context.strokeStyle = this.color;
		context.lineWidth = 1;
		context.beginPath();
		context.moveTo(0, canvas.height/2);
		context.lineTo(canvas.width, canvas.height/2);
		context.stroke();
 
		/********* Revert to initial setting *********/
		context.restore();
	}
	F.prototype.setColor = function(color){
		this.color = color.toString();
	}
	F.prototype.setNumber = function(number){
		this.number = number.toString();
		if(this.number.length == 1){
			this.number = "0" + this.number;
		}
	}
	return F;
})();
 
/*****************************************
 *
 *	Hour24 Constructor extended by AnimCanvas
 *
*****************************************/
var Hour24 = (function(){
	/* Constructor */
	function F(){
		this.pm = false;
	}
 
	/* Inheritance */
	F.prototype = new SimpleCanvas();
 
	/* Private Valuables */
	var canvas, context, time;
	var corderRound = { x : 10, y : 8 }
 
	/* Public Methods */
	F.prototype.draw = function(){
		canvas = this.canvas;
		context = this.context;
		time = this.time;
 
		/* Clear and save initial setting  */
		context.clearRect(0, 0, canvas.width, canvas.height);
		context.save();
 
		/********* Draw from here *********/
		// Create Clip Path
		context.beginPath();
		context.moveTo(0, corderRound.y);
		context.quadraticCurveTo(0, 0, corderRound.x, 0);
		context.lineTo(canvas.width-corderRound.x, 0);
		context.quadraticCurveTo(canvas.width, 0, canvas.width, corderRound.y);
		context.lineTo(canvas.width, canvas.height-corderRound.y);
		context.quadraticCurveTo(canvas.width, canvas.height, canvas.width-corderRound.x, canvas.height);
		context.lineTo(corderRound.x, canvas.height);
		context.quadraticCurveTo(0, canvas.height, 0, canvas.height-corderRound.y);
		context.closePath();
		// context.clip();
 
		// Fill Background Color
		context.fillStyle = this.color;
		context.fill();
		// context.fillRect(0, 0, canvas.width, canvas.height);
 
		// Draw the Number
		context.font = "62px Arial"
		context.textAlign = "center";
		context.textBaseline = "Alphabetic";
		context.fillStyle = "#ffffff";
		if(this.color.toLowerCase() == "white" || this.color.toLowerCase() == "#ffffff"){
			context.fillStyle = "#000000";
		}
		context.fillText(this.number, canvas.width/2, canvas.height-8.5);
 
		// Draw the Number
		context.font = "8px Arial"
		context.textAlign = "left";
		if(this.pm){
			context.fillText("PM", 4, canvas.height-8.5);
		} else {
			context.fillText("AM", 4, canvas.height-8.5);
		}
 
		// Draw Center Line
		context.strokeStyle = this.color;
		context.lineWidth = 1;
		context.beginPath();
		context.moveTo(0, canvas.height/2);
		context.lineTo(canvas.width, canvas.height/2);
		context.stroke();	
 
		/********* Revert to initial setting *********/
		context.restore();
	}
	F.prototype.setColor = function(color){
		this.color = color.toString();
	}
	F.prototype.setNumber = function(number){
		this.number = number;
		if(this.number >= 12){
			this.pm = true;
			this.number = this.number - 12
		}
		this.number = this.number != 0 ? this.number : 12;
		this.number = this.number.toString();
	}
	return F;
})();
 
 
/* Create Instance */
var color = urlColor;
switch(urlColor){
	case "black":
		color = "#000000";
		break;
	case "blue":
		color = "#0000ff";
		break;
	case "gray":
		color = "#808080";
		break;
	case "green":
		color = "#008000";
		break;
	case "orange":
		color = "#ffa500";
		break;
	case "pink":
		color = "#ffc0cb";
		break;
	case "red":
		color = "#ff0000";
		break;
	case "white":
		color = "#ffffff";
		break;
}
 
var generalNum = [];
var hourNum = [];
 
for(var i=0; i<60; i++){
	var num = new General60();
	num.createCanvas(73, 61);
	num.setColor(color);
	num.setNumber(i);
	num.render();
	generalNum.push(num);
}
for(var i=0; i<24; i++){
	var num = new Hour24();
	num.createCanvas(73, 61);
	num.setColor(color);
	num.setNumber(i);
	num.render();
	hourNum.push(num);
}
 
var hourPanel = new ClockPanel();
hourPanel.createCanvas(73, 61);
hourPanel.setFps(30);
hourPanel.setPanelList(hourNum);
 
var minutePanel = new ClockPanel();
minutePanel.createCanvas(73, 61);
minutePanel.setFps(30);
minutePanel.setPanelList(generalNum);
 
var secondPanel = new ClockPanel();
secondPanel.createCanvas(73, 61);
secondPanel.setFps(30);
secondPanel.setPanelList(generalNum);
 
var clock = new Clock();
clock.setCanvas(urlClock);
clock.setScale(urlSize/baseSize);
 
/* Start Animation */
var animFrame = new AnimFrame();
animFrame.push(clock);
animFrame.push(hourPanel);
animFrame.push(minutePanel);
animFrame.push(secondPanel);
animFrame.start();
</script>
 
</body>
</html>

发表回复

74 ÷ 74 =