!
也想出现在这里? 联系我们
广告位

HTML5 网页上下翻页时钟效果代码

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

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

  1. <html>
  2. <head></head><body>
  3. <script language="JavaScript" type="text/javascript">
  4. /*****************************************
  5. *
  6. * [No need to change] Cross browser requestAnimationFrame
  7. * To know more detail, go to the following link
  8. * http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  9. *
  10. *****************************************/
  11. window.requestAnimFrame = (function(callback) {
  12. var agent = navigator.userAgent
  13. if(agent.search(/iPhone/) != -1 || agent.search(/iPod/) != -1 || agent.search(/iPad/) != -1){
  14. return function(callback) {
  15. window.setTimeout(callback, 1000 / 60);
  16. };
  17. }
  18. return window.requestAnimationFrame ||
  19. window.webkitRequestAnimationFrame ||
  20. window.mozRequestAnimationFrame ||
  21. window.oRequestAnimationFrame ||
  22. window.msRequestAnimationFrame ||
  23. function(callback) {
  24. window.setTimeout(callback, 1000 / 60);
  25. };
  26. })();
  27. /*****************************************
  28. *
  29. * [No need to change] AnimFrame Constructor
  30. * For manageing the plural animation canvases.
  31. * Usage:
  32. * 1. Instantiate this Constructor: var animeFrame = new AnimFrame();
  33. * 2. Set animation objects extended from AnimCanvas Constructor: animeFrame.push(anim01); animeFrame.push(anim02); ...
  34. * 3. Call the start method: animeFrame.start();
  35. * 4. Be able to stop animations by calling the stop method (no used in Clocklinks): animeFrame.stop();
  36. *
  37. *****************************************/
  38. var AnimFrame = (function(){
  39. function F(){}
  40. var stack = [];
  41. var isAnimating;
  42. F.prototype.push = function(instance){
  43. stack.push(instance);
  44. };
  45. F.prototype.stop = function(){
  46. isAnimating = false;
  47. };
  48. F.prototype.start = function(){
  49. isAnimating = true;
  50. init();
  51. animate();
  52. };
  53. function init(){
  54. for(var i=0, l=stack.length; i<l; i++) {
  55. stack[i].init();
  56. }
  57. }
  58. function animate(){
  59. if(isAnimating) {
  60. requestAnimFrame(function() {
  61. animate();
  62. });
  63. }
  64. for(var i=0, l=stack.length; i<l; i++) {
  65. stack[i].render();
  66. }
  67. };
  68. return F;
  69. })();
  70. /*****************************************
  71. *
  72. * [No need to change] AnimCanvas Constructor
  73. * This is used as a base of animation canvas.
  74. *
  75. *****************************************/
  76. var AnimCanvas = (function(){
  77. /* Constructor */
  78. function F(){
  79. this.id;
  80. this.canvas;
  81. this.context;
  82. this.time;
  83. this.startTime;
  84. this.fps;
  85. this.fpsStep;
  86. this.fpsTime;
  87. }
  88. /* Public Methods */
  89. // setCanvas() is required to call for identifying the canvas to use
  90. F.prototype.setCanvas = function(canvasId){
  91. this.id = canvasId;
  92. this.canvas = document.getElementById(this.id);
  93. this.context = this.canvas.getContext("2d");
  94. };
  95. // createCanvas() is required to call for
  96. F.prototype.createCanvas = function(width, height){
  97. this.canvas = document.createElement("canvas");
  98. this.context = this.canvas.getContext("2d");
  99. this.canvas.width = width;
  100. this.canvas.height = height;
  101. };
  102. // setFps() is arbitrary to change the fps
  103. // if not called, the canvas is rendered right when animation gets ready
  104. F.prototype.setFps = function(fps){
  105. this.fps = fps;
  106. this.fpsStep = 1000 / fps;
  107. this.fpsFrame;
  108. };
  109. // init() is called by the AnimFrame constructor when starting Animation
  110. F.prototype.init = function(){
  111. this.startTime = (new Date()).getTime();
  112. };
  113. // render() is called by the AnimFrame constructor each time to render
  114. F.prototype.render = function(){
  115. this.time = (new Date()).getTime() - this.startTime;
  116. if(this.fps){
  117. var millisecond = this.time % 1000;
  118. var currentFpsFrame = Math.floor(millisecond / this.fpsStep);
  119. if(this.fpsFrame != currentFpsFrame){
  120. this.fpsFrame = currentFpsFrame;
  121. this.draw();
  122. }
  123. } else {
  124. this.draw();
  125. }
  126. };
  127. return F;
  128. })();
  129. /*****************************************
  130. *
  131. * SimpleCanvas Constructor
  132. *
  133. *****************************************/
  134. var SimpleCanvas = (function(){
  135. /* Constructor */
  136. function F(){
  137. this.id;
  138. this.canvas;
  139. this.context;
  140. }
  141. /* Public Methods */
  142. // setCanvas() is required to call for identifying the canvas to use
  143. F.prototype.setCanvas = function(canvasId){
  144. this.id = canvasId;
  145. this.canvas = document.getElementById(this.id);
  146. this.context = this.canvas.getContext("2d");
  147. };
  148. // createCanvas() is required to call for gerating a new canvas object
  149. F.prototype.createCanvas = function(width, height){
  150. this.canvas = document.createElement("canvas");
  151. this.context = this.canvas.getContext("2d");
  152. this.canvas.width = width;
  153. this.canvas.height = height;
  154. };
  155. // render() is called by the AnimFrame constructor each time to render
  156. F.prototype.render = function(){
  157. this.draw();
  158. };
  159. return F;
  160. })();
  161. // Now is Html5 Clock begin, above all is render frame.
  162. function isCanvasSupported(){
  163. var elem = document.createElement("canvas");
  164. return !!(elem.getContext && elem.getContext("2d"));
  165. }
  166. var urlClock = 'Clock-Html5';
  167. var urlTimezone = 'CCT';
  168. var urlSize = '271';
  169. var urlTitle = '';
  170. var urlMessage = '';
  171. var urlTarget = '';
  172. var urlFrom = '2014,1,1,0,0,0';
  173. var urlColor = 'gray';
  174. //var adImageUrl = "";
  175. //var redirectUrl = "";
  176. var serverYear = new Date().getFullYear();
  177. var serverMonth = new Date().getMonth();
  178. var serverDay = new Date().getDay();
  179. var serverHour = new Date().getHours();
  180. var serverMinute = new Date().getMinutes();
  181. var serverSecond = new Date().getSeconds();
  182. var serverMillisecond = new Date().getMilliseconds();
  183. var rootPath = "/";
  184. //var adId = "Clock-Html5-AD";
  185. /*
  186. Global valuables
  187. - urlClock
  188. - urlTimezone
  189. - urlColor
  190. - urlSize
  191. - serverYear
  192. - serverMonth
  193. - serverDay
  194. - serverHour
  195. - serverMinute
  196. - serverSecond
  197. - serverMillisecond
  198. */
  199. var baseSize = 227;
  200. var srcPath = rootPath + "html5-008/";
  201. document.write('<canvas id="' + urlClock + '" width="' + urlSize + 'px" height="' + urlSize/2.91 + 'px"></canvas>');
  202. /*****************************************
  203. *
  204. * Clock Constructor extended by AnimCanvas
  205. *
  206. *****************************************/
  207. var Clock = (function(){
  208. /* Constructor */
  209. function F(){}
  210. /* Inheritance */
  211. F.prototype = new AnimCanvas();
  212. F.prototype.__super__ = AnimCanvas.prototype;
  213. F.prototype.init = function(){
  214. this.__super__.init.apply(this, arguments);
  215. var servertime = new Date();
  216. servertime.setYear(serverYear);
  217. servertime.setMonth(serverMonth - 1);
  218. servertime.setDate(serverDay);
  219. servertime.setHours(serverHour);
  220. servertime.setMinutes(serverMinute);
  221. servertime.setSeconds(serverSecond);
  222. unixservertime = servertime.getTime();
  223. };
  224. /* Private Valuables (don't change the values) */
  225. var canvas, context, time;
  226. var unixservertime,
  227. currenttime;
  228. var hour,
  229. minute,
  230. second,
  231. millisecond;
  232. var previousHour,
  233. previousMinute,
  234. previousSecond;
  235. var scaleValue = 1;
  236. /* Private Functions */
  237. function countTime(){
  238. currenttime = new Date();
  239. currenttime.setTime(unixservertime + time);
  240. hour = currenttime.getHours();
  241. minute = currenttime.getMinutes();
  242. second = currenttime.getSeconds();
  243. millisecond = currenttime.getMilliseconds()
  244. }
  245. /* Public Methods */
  246. F.prototype.setScale = function(val){
  247. scaleValue = val;
  248. };
  249. F.prototype.draw = function(){
  250. canvas = this.canvas;
  251. context = this.context;
  252. time = this.time;
  253. /* Clear and save initial setting */
  254. context.clearRect(0, 0, canvas.width, canvas.height);
  255. context.save();
  256. /********* Draw from here *********/
  257. countTime();
  258. context.translate(0, canvas.height/2);
  259. context.scale(scaleValue, scaleValue);
  260. // hour panel
  261. hourPanel.setNumber(hour);
  262. context.drawImage(hourPanel.canvas, 0, -hourPanel.canvas.height/2);
  263. // minute panel
  264. minutePanel.setNumber(minute);
  265. context.drawImage(minutePanel.canvas, 77, -minutePanel.canvas.height/2);
  266. // second panel
  267. secondPanel.setNumber(second);
  268. context.drawImage(secondPanel.canvas, 154, -secondPanel.canvas.height/2);
  269. /********* Revert to initial setting *********/
  270. context.restore();
  271. }
  272. return F;
  273. })();
  274. /*****************************************
  275. *
  276. * ClockPanel Constructor extended by AnimCanvas
  277. *
  278. *****************************************/
  279. var ClockPanel = (function(){
  280. /* Constructor */
  281. function F(){}
  282. /* Inheritance */
  283. F.prototype = new AnimCanvas();
  284. F.prototype.__super__ = AnimCanvas.prototype;
  285. F.prototype.init = function(){
  286. this.__super__.init.apply(this, arguments);
  287. this.currentNum = 0;
  288. this.previousNum = 0;
  289. this.count = 0;
  290. };
  291. /* Private Valuables (don't change the values) */
  292. var canvas, context, time;
  293. var flipFrame = 10;
  294. /* Public Methods */
  295. F.prototype.draw = function(){
  296. canvas = this.canvas;
  297. context = this.context;
  298. time = this.time;
  299. /* Clear and save initial setting */
  300. context.clearRect(0, 0, canvas.width, canvas.height);
  301. context.save();
  302. /********* Draw from here *********/
  303. if(this.currentNum == this.previousNum){
  304. context.drawImage(this.panelList[this.currentNum].canvas, 0, 0);
  305. } else {
  306. this.count++;
  307. var progress = Math.cos(Math.PI * this.count / flipFrame);
  308. var currentPanel = this.panelList[this.currentNum].canvas;
  309. var previousPanel = this.panelList[this.previousNum].canvas;
  310. context.save();
  311. context.translate(0, currentPanel.height/2);
  312. // Upper Current Panel
  313. context.drawImage(currentPanel, 0, 0, currentPanel.width, currentPanel.height/2, 0, -currentPanel.height/2, currentPanel.width, currentPanel.height/2);
  314. // Lower Previous Panel
  315. context.drawImage(previousPanel, 0, previousPanel.height/2, previousPanel.width, previousPanel.height/2, 0, 0, previousPanel.width, previousPanel.height/2);
  316. if(progress>0){
  317. // Upper Previous Panel
  318. context.drawImage(previousPanel, 0, 0, previousPanel.width, previousPanel.height/2, 0, -previousPanel.height/2*progress, previousPanel.width, previousPanel.height/2*progress);
  319. } else {
  320. progress = progress* -1;
  321. // Lower Current Panel
  322. context.drawImage(currentPanel, 0, currentPanel.height/2, currentPanel.width, currentPanel.height/2, 0, 0, currentPanel.width, currentPanel.height/2*progress);
  323. }
  324. context.restore();
  325. if(this.count==flipFrame){
  326. this.count=0;
  327. this.previousNum = this.currentNum;
  328. }
  329. }
  330. /********* Revert to initial setting *********/
  331. context.restore();
  332. }
  333. F.prototype.setPanelList = function(array){
  334. this.panelList = array;
  335. }
  336. F.prototype.setNumber = function(num){
  337. this.currentNum = num;
  338. }
  339. return F;
  340. })();
  341. /*****************************************
  342. *
  343. * General60 Constructor extended by AnimCanvas
  344. *
  345. *****************************************/
  346. var General60 = (function(){
  347. /* Constructor */
  348. function F(){}
  349. /* Inheritance */
  350. F.prototype = new SimpleCanvas();
  351. /* Private Valuables */
  352. var canvas, context, time;
  353. var corderRound = { x : 10, y : 8 }
  354. /* Public Methods */
  355. F.prototype.draw = function(){
  356. canvas = this.canvas;
  357. context = this.context;
  358. time = this.time;
  359. /* Clear and save initial setting */
  360. context.clearRect(0, 0, canvas.width, canvas.height);
  361. context.save();
  362. /********* Draw from here *********/
  363. // Create Clip Path
  364. context.beginPath();
  365. context.moveTo(0, corderRound.y);
  366. context.quadraticCurveTo(0, 0, corderRound.x, 0);
  367. context.lineTo(canvas.width-corderRound.x, 0);
  368. context.quadraticCurveTo(canvas.width, 0, canvas.width, corderRound.y);
  369. context.lineTo(canvas.width, canvas.height-corderRound.y);
  370. context.quadraticCurveTo(canvas.width, canvas.height, canvas.width-corderRound.x, canvas.height);
  371. context.lineTo(corderRound.x, canvas.height);
  372. context.quadraticCurveTo(0, canvas.height, 0, canvas.height-corderRound.y);
  373. context.closePath();
  374. // context.clip();
  375. // Fill Background Color
  376. context.fillStyle = this.color;
  377. context.fill();
  378. // context.fillRect(0, 0, canvas.width, canvas.height);
  379. // Draw the Number
  380. context.font = "62px Arial"
  381. context.textAlign = "center";
  382. context.textBaseline = "Alphabetic";
  383. context.fillStyle = "#ffffff";
  384. if(this.color.toLowerCase() == "white" || this.color.toLowerCase() == "#ffffff"){
  385. context.fillStyle = "#000000";
  386. }
  387. context.fillText(this.number, canvas.width/2, canvas.height-8.5);
  388. // Draw Center Line
  389. context.strokeStyle = this.color;
  390. context.lineWidth = 1;
  391. context.beginPath();
  392. context.moveTo(0, canvas.height/2);
  393. context.lineTo(canvas.width, canvas.height/2);
  394. context.stroke();
  395. /********* Revert to initial setting *********/
  396. context.restore();
  397. }
  398. F.prototype.setColor = function(color){
  399. this.color = color.toString();
  400. }
  401. F.prototype.setNumber = function(number){
  402. this.number = number.toString();
  403. if(this.number.length == 1){
  404. this.number = "0" + this.number;
  405. }
  406. }
  407. return F;
  408. })();
  409. /*****************************************
  410. *
  411. * Hour24 Constructor extended by AnimCanvas
  412. *
  413. *****************************************/
  414. var Hour24 = (function(){
  415. /* Constructor */
  416. function F(){
  417. this.pm = false;
  418. }
  419. /* Inheritance */
  420. F.prototype = new SimpleCanvas();
  421. /* Private Valuables */
  422. var canvas, context, time;
  423. var corderRound = { x : 10, y : 8 }
  424. /* Public Methods */
  425. F.prototype.draw = function(){
  426. canvas = this.canvas;
  427. context = this.context;
  428. time = this.time;
  429. /* Clear and save initial setting */
  430. context.clearRect(0, 0, canvas.width, canvas.height);
  431. context.save();
  432. /********* Draw from here *********/
  433. // Create Clip Path
  434. context.beginPath();
  435. context.moveTo(0, corderRound.y);
  436. context.quadraticCurveTo(0, 0, corderRound.x, 0);
  437. context.lineTo(canvas.width-corderRound.x, 0);
  438. context.quadraticCurveTo(canvas.width, 0, canvas.width, corderRound.y);
  439. context.lineTo(canvas.width, canvas.height-corderRound.y);
  440. context.quadraticCurveTo(canvas.width, canvas.height, canvas.width-corderRound.x, canvas.height);
  441. context.lineTo(corderRound.x, canvas.height);
  442. context.quadraticCurveTo(0, canvas.height, 0, canvas.height-corderRound.y);
  443. context.closePath();
  444. // context.clip();
  445. // Fill Background Color
  446. context.fillStyle = this.color;
  447. context.fill();
  448. // context.fillRect(0, 0, canvas.width, canvas.height);
  449. // Draw the Number
  450. context.font = "62px Arial"
  451. context.textAlign = "center";
  452. context.textBaseline = "Alphabetic";
  453. context.fillStyle = "#ffffff";
  454. if(this.color.toLowerCase() == "white" || this.color.toLowerCase() == "#ffffff"){
  455. context.fillStyle = "#000000";
  456. }
  457. context.fillText(this.number, canvas.width/2, canvas.height-8.5);
  458. // Draw the Number
  459. context.font = "8px Arial"
  460. context.textAlign = "left";
  461. if(this.pm){
  462. context.fillText("PM", 4, canvas.height-8.5);
  463. } else {
  464. context.fillText("AM", 4, canvas.height-8.5);
  465. }
  466. // Draw Center Line
  467. context.strokeStyle = this.color;
  468. context.lineWidth = 1;
  469. context.beginPath();
  470. context.moveTo(0, canvas.height/2);
  471. context.lineTo(canvas.width, canvas.height/2);
  472. context.stroke();
  473. /********* Revert to initial setting *********/
  474. context.restore();
  475. }
  476. F.prototype.setColor = function(color){
  477. this.color = color.toString();
  478. }
  479. F.prototype.setNumber = function(number){
  480. this.number = number;
  481. if(this.number >= 12){
  482. this.pm = true;
  483. this.number = this.number - 12
  484. }
  485. this.number = this.number != 0 ? this.number : 12;
  486. this.number = this.number.toString();
  487. }
  488. return F;
  489. })();
  490. /* Create Instance */
  491. var color = urlColor;
  492. switch(urlColor){
  493. case "black":
  494. color = "#000000";
  495. break;
  496. case "blue":
  497. color = "#0000ff";
  498. break;
  499. case "gray":
  500. color = "#808080";
  501. break;
  502. case "green":
  503. color = "#008000";
  504. break;
  505. case "orange":
  506. color = "#ffa500";
  507. break;
  508. case "pink":
  509. color = "#ffc0cb";
  510. break;
  511. case "red":
  512. color = "#ff0000";
  513. break;
  514. case "white":
  515. color = "#ffffff";
  516. break;
  517. }
  518. var generalNum = [];
  519. var hourNum = [];
  520. for(var i=0; i<60; i++){
  521. var num = new General60();
  522. num.createCanvas(73, 61);
  523. num.setColor(color);
  524. num.setNumber(i);
  525. num.render();
  526. generalNum.push(num);
  527. }
  528. for(var i=0; i<24; i++){
  529. var num = new Hour24();
  530. num.createCanvas(73, 61);
  531. num.setColor(color);
  532. num.setNumber(i);
  533. num.render();
  534. hourNum.push(num);
  535. }
  536. var hourPanel = new ClockPanel();
  537. hourPanel.createCanvas(73, 61);
  538. hourPanel.setFps(30);
  539. hourPanel.setPanelList(hourNum);
  540. var minutePanel = new ClockPanel();
  541. minutePanel.createCanvas(73, 61);
  542. minutePanel.setFps(30);
  543. minutePanel.setPanelList(generalNum);
  544. var secondPanel = new ClockPanel();
  545. secondPanel.createCanvas(73, 61);
  546. secondPanel.setFps(30);
  547. secondPanel.setPanelList(generalNum);
  548. var clock = new Clock();
  549. clock.setCanvas(urlClock);
  550. clock.setScale(urlSize/baseSize);
  551. /* Start Animation */
  552. var animFrame = new AnimFrame();
  553. animFrame.push(clock);
  554. animFrame.push(hourPanel);
  555. animFrame.push(minutePanel);
  556. animFrame.push(secondPanel);
  557. animFrame.start();
  558. </script>
  559. </body>
  560. </html>

给TA打赏
共{{data.count}}人
人已打赏
前端学习

jQuery 直接读取网站数据库数据并做判断的方法

2024-8-30 14:16:27

前端学习

详解使用『rem』实现自适应响应式布局

2024-8-30 14:16:36

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索