CanvasBitClock

功能描述

canvas彩色碎粒掉落时钟。

依赖模块

./libs/base.js

快速使用

  1. ## demo
  2. <div style="text-align: center; margin-top: 20%;">
  3. <canvas id="canvas" style="width:200px;"></canvas>
  4. </div>
  5. <script>
  6. var canvas = document.getElementById('canvas');
  7. if (canvas.getContext) {
  8. var cxt = canvas.getContext('2d');
  9. //声明canvas的宽高
  10. var H = 100,
  11. W = 700;
  12. canvas.height = H;
  13. canvas.width = W;
  14. cxt.fillStyle = '#f00';
  15. cxt.fillRect(10, 10, 50, 50);
  16. //存储时间数据
  17. var data = [];
  18. //存储运动的小球
  19. var balls = [];
  20. //设置粒子半径
  21. var R = canvas.height / 20 - 1;
  22. (function() {
  23. var temp = /(\d)(\d):(\d)(\d):(\d)(\d)/.exec(new Date());
  24. //存储时间数字,由十位小时、个位小时、冒号、十位分钟、个位分钟、冒号、十位秒钟、个位秒钟这7个数字组成
  25. data.push(temp[1], temp[2], 10, temp[3], temp[4], 10, temp[5], temp[6]);
  26. })();
  27. /*生成点阵数字*/
  28. function renderDigit(index, num) {
  29. for (var i = 0; i < digit[num].length; i++) {
  30. for (var j = 0; j < digit[num][i].length; j++) {
  31. if (digit[num][i][j] == 1) {
  32. cxt.beginPath();
  33. cxt.arc(14 * (R + 2) * index + j * 2 * (R + 1) + (R + 1), i * 2 * (R + 1) + (R + 1), R, 0, 2 * Math.PI);
  34. cxt.closePath();
  35. cxt.fill();
  36. }
  37. }
  38. }
  39. }
  40. /*更新时钟*/
  41. function updateDigitTime() {
  42. var changeNumArray = [];
  43. var temp = /(\d)(\d):(\d)(\d):(\d)(\d)/.exec(new Date());
  44. var NewData = [];
  45. NewData.push(temp[1], temp[2], 10, temp[3], temp[4], 10, temp[5], temp[6]);
  46. for (var i = data.length - 1; i >= 0; i--) {
  47. //时间发生变化
  48. if (NewData[i] !== data[i]) {
  49. //将变化的数字值和在data数组中的索引存储在changeNumArray数组中
  50. changeNumArray.push(i + '_' + (Number(data[i]) + 1) % 10);
  51. }
  52. }
  53. //增加小球
  54. for (var i = 0; i < changeNumArray.length; i++) {
  55. addBalls.apply(this, changeNumArray[i].split('_'));
  56. }
  57. data = NewData.concat();
  58. }
  59. /*更新小球状态*/
  60. function updateBalls() {
  61. for (var i = 0; i < balls.length; i++) {
  62. balls[i].stepY += balls[i].disY;
  63. balls[i].x += balls[i].stepX;
  64. balls[i].y += balls[i].stepY;
  65. if (balls[i].x > W + R || balls[i].y > H + R) {
  66. balls.splice(i, 1);
  67. i--;
  68. }
  69. }
  70. }
  71. /*增加要运动的小球*/
  72. function addBalls(index, num) {
  73. var numArray = [1, 2, 3];
  74. var colorArray = ["#3BE", "#09C", "#A6C", "#93C", "#9C0", "#690", "#FB3", "#F80", "#F44", "#C00"];
  75. for (var i = 0; i < digit[num].length; i++) {
  76. for (var j = 0; j < digit[num][i].length; j++) {
  77. if (digit[num][i][j] == 1) {
  78. var ball = {
  79. x: 14 * (R + 2) * index + j * 2 * (R + 1) + (R + 1),
  80. y: i * 2 * (R + 1) + (R + 1),
  81. stepX: Math.floor(Math.random() * 4 - 2),
  82. stepY: -2 * numArray[Math.floor(Math.random() * numArray.length)],
  83. color: colorArray[Math.floor(Math.random() * colorArray.length)],
  84. disY: 1
  85. };
  86. balls.push(ball);
  87. }
  88. }
  89. }
  90. }
  91. /*渲染*/
  92. function render() {
  93. //重置画布宽度,达到清空画布的效果
  94. canvas.height = 100;
  95. //渲染时钟
  96. for (var i = 0; i < data.length; i++) {
  97. renderDigit(i, data[i]);
  98. }
  99. //渲染小球
  100. for (var i = 0; i < balls.length; i++) {
  101. cxt.beginPath();
  102. cxt.arc(balls[i].x, balls[i].y, R, 0, 2 * Math.PI);
  103. cxt.fillStyle = balls[i].color;
  104. cxt.closePath();
  105. cxt.fill();
  106. }
  107. }
  108. clearInterval(oTimer);
  109. var oTimer = setInterval(function() {
  110. //更新时钟
  111. updateDigitTime();
  112. //更新小球状态
  113. updateBalls();
  114. //渲染
  115. render();
  116. }, 50);
  117. }
  118. </script>

特别说明

canvas的宽度调整时钟的大小,用例详见index.html。