查看: 863|回复: 0

[DIV/CSS] canvas基础绘制-绚丽倒计时

发表于 2017-9-24 08:00:05
尚学堂AD

效果图:

html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>ball</title>
  6. <script src="digit_1.js"></script>
  7. <script src="countdown.js"></script>
  8. </head>
  9. <body style="height: 100%;">
  10. <canvas id="canvas" style="height: 100%"></canvas>
  11. </body>
  12. </html>
复制代码

digit_1.js在之前的 canvas基础绘制-倒计时 中有贴

countdown.js:

  1. var WINDOW_WIDTH = 1024;
  2. var WINDOW_HEIGHT = 768;
  3. var RADIUS = 8;
  4. var MARGIN_TOP = 60;
  5. var MARGIN_LEFT = 30;
  6. var endTime = new Date();//const声明变量,不可修改,必须声明时赋值;
  7. endTime.setTime( endTime.getTime() + 3600*1000);//当前时间向后一小时;
  8. var curShowTimeSeconds = 0;
  9. var balls =[];
  10. const colors = ["#33B5E5","#0099CC","#AA66CC","#9933CC","#99CC00","#669900","#FFBB33","#FF8800","#FF4444","#CC0000"];
  11. window.onload = function () {
  12. //屏幕自适应
  13. WINDOW_WIDTH = document.body.clientWidth;
  14. WINDOW_HEIGHT = document.body.clientHeight;
  15. RADIUS = Math.round(WINDOW_WIDTH*4/5/108)-1;
  16. MARGIN_TOP = Math.round(WINDOW_HEIGHT/5);
  17. MARGIN_LEFT = Math.round(WINDOW_WIDTH/10);
  18. var canvas = document.getElementById("canvas");
  19. var context = canvas.getContext("2d");
  20. canvas.width = WINDOW_WIDTH;
  21. canvas.height = WINDOW_HEIGHT;
  22. curShowTimeSeconds = getCurrentShowTimeSeconds();
  23. setInterval(
  24. function () {
  25. update();
  26. render(context);
  27. },50)
  28. };
  29. function getCurrentShowTimeSeconds() {
  30. var curTime = new Date();//获取目前时间;
  31. var ret = endTime.getTime()-curTime.getTime();
  32. ret = Math.round(ret/1000);//获取秒数差值;
  33. return ret>=0?ret:0;
  34. }
复制代码
  1. function update() {
  2. var nextShowTimeSeconds = getCurrentShowTimeSeconds();
  3. var nextHours = parseInt(nextShowTimeSeconds/3600);
  4. var nextMinutes = parseInt((nextShowTimeSeconds-nextHours*3600)/60);
  5. var nextSeconds = nextShowTimeSeconds%60;
  6. var curHours = parseInt(curShowTimeSeconds/3600);
  7. var curMinutes = parseInt((curShowTimeSeconds-curHours*3600)/60);
  8. var curSeconds = curShowTimeSeconds%60;
  9. if(nextSeconds!=curSeconds){
  10. if(parseInt(curHours/10)!=parseInt(nextHours/10)){
  11. addBalls(MARGIN_LEFT+0,MARGIN_TOP,parseInt(curHours/10));
  12. }
  13. if(parseInt(curHours%10)!=parseInt(nextHours%10)){
  14. addBalls(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(curHours%10));
  15. }
  16. if(parseInt(curMinutes/10)!=parseInt(nextMinutes/10)){
  17. addBalls(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes/10));
  18. }
  19. if(parseInt(curMinutes%10)!=parseInt(nextMinutes%10)){
  20. addBalls(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes%10));
  21. }
  22. if(parseInt(curSeconds/10)!=parseInt(nextSeconds/10)){
  23. addBalls(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(curSeconds/10));
  24. }
  25. if(parseInt(curSeconds%10)!=parseInt(nextSeconds%10)){
  26. addBalls(MARGIN_LEFT+93*(RADIUS+1),MARGIN_TOP,parseInt(curSeconds%10));
  27. }
  28. curShowTimeSeconds = nextShowTimeSeconds;
  29. }
  30. updateBalls();
  31. }
  32. function updateBalls() {
  33. //循环遍历每一个彩色动画小球
  34. for(var i=0;i<balls.length;i++){
  35. balls[i].x+=balls[i].vx;
  36. balls[i].y+=balls[i].vy;
  37. balls[i].vy+=balls[i].g;
  38. //落到画布最底部时反弹起来
  39. if(balls[i].y>=WINDOW_HEIGHT){
  40. balls[i].y = WINDOW_HEIGHT-RADIUS;
  41. balls[i].vy = -balls[i].vy*0.75;
  42. }
  43. }
  44. // 如果小球出了画布,就清除小球,性能优化
  45. var cnt = 0;
  46. for(var i=0;i<balls.length;i++){
  47. if(balls[i].x-RADIUS>0&&balls[i].x+RADIUS<WINDOW_WIDTH){
  48. balls[cnt++] = balls[i];
  49. }
  50. }
  51. while (balls.length>Math.min(300,cnt)){
  52. balls.pop();
  53. }
  54. }
复制代码
  1. function addBalls(x,y,num) {
  2. for (var i = 0; i < digit[num].length; i++) {//数组行
  3. for (var j = 0; j < digit[num][i].length; j++) {//数组列
  4. if (digit[num][i][j] == 1) {
  5. var aBall = {
  6. x: x + j * 2 * (RADIUS + 1) + (RADIUS + 1),
  7. y: y + i * 2 * (RADIUS + 1) + (RADIUS + 1),
  8. g: 1.5 + Math.random(),
  9. vx: Math.pow(-1, Math.ceil(Math.random() * 1000)) * 4,//pow(x,y),x 的 y 次幂;ceil()可对一个数进行上舍入;
  10. vy: -5,
  11. color: colors[Math.floor(Math.random() * colors.length)]//floor()对一个数进行下舍入
  12. };
  13. balls.push(aBall);
  14. }
  15. }
  16. }
  17. }
  18. function render(cxt) {
  19. //每一帧都要对动画进行刷新,不然就会新的旧的叠在一起;
  20. cxt.clearRect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);//对一个矩形空间里的动画进行刷新;
  21. //倒计时的时间绘制
  22. var hours = parseInt(curShowTimeSeconds/3600);
  23. var minutes = parseInt((curShowTimeSeconds-hours*3600)/60);
  24. var seconds = curShowTimeSeconds%60;
  25. renderDigit(MARGIN_LEFT,MARGIN_TOP,parseInt(hours/10),cxt);
  26. renderDigit(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(hours%10),cxt);
  27. renderDigit(MARGIN_LEFT+30*(RADIUS+1),MARGIN_TOP,10,cxt);
  28. renderDigit(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(minutes/10),cxt);
  29. renderDigit(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(minutes%10),cxt);
  30. renderDigit(MARGIN_LEFT+69*(RADIUS+1),MARGIN_TOP,10,cxt);
  31. renderDigit(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(seconds/10),cxt);
  32. renderDigit(MARGIN_LEFT+93*(RADIUS+1),MARGIN_TOP,parseInt(seconds%10),cxt);
  33. //彩色动画小球的绘制
  34. for(var i=0;i<balls.length;i++){
  35. cxt.fillStyle = balls[i].color;
  36. cxt.beginPath();
  37. cxt.arc(balls[i].x,balls[i].y,RADIUS,0,2*Math.PI,true);
  38. cxt.closePath();
  39. cxt.fill();
  40. }
  41. }
  42. function renderDigit(x,y,num,cxt) {
  43. cxt.fillStyle = "rgb(0,102,153)";
  44. for (var i = 0; i < digit[num].length; i++) {//数组行
  45. for (var j = 0; j < digit[num][i].length; j++) {//数组列
  46. if (digit[num][i][j] == 1) {
  47. cxt.beginPath();
  48. cxt.arc(x + j * 2 * (RADIUS + 1) + (RADIUS + 1), y + i * 2 * (RADIUS + 1) + (RADIUS + 1), RADIUS, 0, 2 * Math.PI);
  49. cxt.closePath();
  50. cxt.fill();
  51. }
  52. }
  53. }
  54. }
复制代码



回复

使用道具 举报