<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>炫彩时钟</title> <style> body{ background:#000;} canvas{ background:#fff; } </style> <script src="countdown.js"></script> <script src="digit.js"></script> </head> <body> <canvas id="canvas1"></canvas> </body> </html>
digit = [ [ [0,0,1,1,1,0,0], [0,1,1,0,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [0,1,1,0,1,1,0], [0,0,1,1,1,0,0] ], [ [0,0,0,1,1,0,0], [0,1,1,1,1,0,0], [0,0,0,1,1,0,0], [0,0,0,1,1,0,0], [0,0,0,1,1,0,0], [0,0,0,1,1,0,0], [0,0,0,1,1,0,0], [0,0,0,1,1,0,0], [0,0,0,1,1,0,0], [1,1,1,1,1,1,1] ], [ [0,1,1,1,1,1,0], [1,1,0,0,0,1,1], [0,0,0,0,0,1,1], [0,0,0,0,1,1,0], [0,0,0,1,1,0,0], [0,0,1,1,0,0,0], [0,1,1,0,0,0,0], [1,1,0,0,0,0,0], [1,1,0,0,0,1,1], [1,1,1,1,1,1,1] ], [ [1,1,1,1,1,1,1], [0,0,0,0,0,1,1], [0,0,0,0,1,1,0], [0,0,0,1,1,0,0], [0,0,1,1,1,0,0], [0,0,0,0,1,1,0], [0,0,0,0,0,1,1], [0,0,0,0,0,1,1], [1,1,0,0,0,1,1], [0,1,1,1,1,1,0] ], [ [0,0,0,0,1,1,0], [0,0,0,1,1,1,0], [0,0,1,1,1,1,0], [0,1,1,0,1,1,0], [1,1,0,0,1,1,0], [1,1,1,1,1,1,1], [0,0,0,0,1,1,0], [0,0,0,0,1,1,0], [0,0,0,0,1,1,0], [0,0,0,1,1,1,1] ], [ [1,1,1,1,1,1,1], [1,1,0,0,0,0,0], [1,1,0,0,0,0,0], [1,1,1,1,1,1,0], [0,0,0,0,0,1,1], [0,0,0,0,0,1,1], [0,0,0,0,0,1,1], [0,0,0,0,0,1,1], [1,1,0,0,0,1,1], [0,1,1,1,1,1,0] ], [ [0,0,0,0,1,1,0], [0,0,1,1,0,0,0], [0,1,1,0,0,0,0], [1,1,0,0,0,0,0], [1,1,0,1,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [0,1,1,1,1,1,0] ], [ [1,1,1,1,1,1,1], [1,1,0,0,0,1,1], [0,0,0,0,1,1,0], [0,0,0,0,1,1,0], [0,0,0,1,1,0,0], [0,0,0,1,1,0,0], [0,0,1,1,0,0,0], [0,0,1,1,0,0,0], [0,0,1,1,0,0,0], [0,0,1,1,0,0,0] ], [ [0,1,1,1,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [0,1,1,1,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [0,1,1,1,1,1,0] ], [ [0,1,1,1,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [0,1,1,1,0,1,1], [0,0,0,0,0,1,1], [0,0,0,0,0,1,1], [0,0,0,0,1,1,0], [0,0,0,1,1,0,0], [0,1,1,0,0,0,0] ], [ [0,0,0,0], [0,0,0,0], [0,1,1,0], [0,1,1,0], [0,0,0,0], [0,0,0,0], [0,1,1,0], [0,1,1,0], [0,0,0,0], [0,0,0,0] ]//: ];
/** * Created by dell on 2016/1/29. */ window.onload = function(){ var CVS_WIDTH = document.body.clientWidth * 4/5; var CVS_HEIGHT = document.body.clientHeight * 4/5; var MARGIN_LEFT = document.body.clientWidth * 1/10; var MARGIN_TOP = document.body.clientHeight * 1/10; var RADIUS = (CVS_WIDTH*4/5)/108 - 1; var PADDINGLEFT = (CVS_WIDTH - 108*(RADIUS+1))/2; var color = '#00688B';//数字颜色 var curShowTime = getCurShowTime();//当前剩余时间 var colors = ['#FF3030','#7CCD7C','#0000EE','#EE9A49','#EEEE00','#FF00FF','#96CDCD','#D15FEE','#EEAEEE','#EEDC82'];//彩球颜色集合 var balls = []; var oCvs = document.getElementById('canvas1'); var oCxt = oCvs.getContext('2d'); //设置画布大小 oCvs.width = CVS_WIDTH; oCvs.height = CVS_HEIGHT; oCvs.style.marginLeft = MARGIN_LEFT + 'px'; oCvs.style.marginTop = MARGIN_TOP + 'px'; setInterval(function () { render(oCxt); update(); },50); function update(){ var nextShowTime = getCurShowTime(); var nextHour = parseInt(nextShowTime/3600); var nextMin = parseInt((nextShowTime - nextHour*3600)/60); var nextSec = parseInt(nextShowTime%60); var curHour = parseInt(curShowTime/3600); var curMin = parseInt((curShowTime - curHour*3600)/60); var curSec = parseInt(curShowTime%60); if(nextSec != curSec){ curShowTime = nextShowTime; if(parseInt(nextHour/10) != parseInt(curHour/10)){ addBalls(PADDINGLEFT,MARGIN_TOP,parseInt(nextSec/10)); } if(parseInt(nextHour%10) != parseInt(curHour%10)){ addBalls(PADDINGLEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(nextSec%10)); } if(parseInt(nextMin/10) != parseInt(curMin/10)){ addBalls(PADDINGLEFT+40*(RADIUS+1),MARGIN_TOP,parseInt(nextSec/10)); } if(parseInt(nextMin%10) != parseInt(curMin%10)){ addBalls(PADDINGLEFT+55*(RADIUS+1),MARGIN_TOP,parseInt(nextSec%10)); } if(parseInt(nextSec/10) != parseInt(curSec/10)){ addBalls(PADDINGLEFT+79*(RADIUS+1),MARGIN_TOP,parseInt(nextSec/10)); } if(parseInt(nextSec%10) != parseInt(curSec%10)){ addBalls(PADDINGLEFT+94*(RADIUS+1),MARGIN_TOP,parseInt(nextSec%10)); } //console.info(balls) } updateBalls(); //console.info(balls.length) } function updateBalls(){ for(var i=0;i<balls.length;i++) { balls[i].x += balls[i].xv; balls[i].y += balls[i].yv; balls[i].yv += balls[i].g; if(balls[i].y >= CVS_HEIGHT-RADIUS){ balls[i].y = CVS_HEIGHT - RADIUS; balls[i].yv = -balls[i].yv*0.75; } } var num = 0; for(var j=0;j<balls.length;j++){ if(balls[j].x+RADIUS>0 && balls[j].x-RADIUS<CVS_WIDTH){ balls[num++] = balls[j]; } } balls = balls.slice(0,Math.min(500,num)); } function addBalls(x,y,num){ for(var i=0;i<digit[num].length;i++){ for(var j=0;j<digit[num][i].length;j++){ if(digit[num][i][j] == 1){ var ball = { x:x+j*2*(RADIUS+1)+(RADIUS+1), y:y+i*2*(RADIUS+1)+(RADIUS+1), color:colors[Math.floor(Math.random()*colors.length)], g:1.5+Math.random(), xv:Math.pow(-1,Math.ceil(Math.random()*1000))*4, yv:-10 }; balls.push(ball); } } } } function getCurShowTime(){ var date = new Date(); var ret = date.getHours()*3600 + date.getMinutes()*60 + date.getSeconds(); return ret; } function render(cxt){ var hour = parseInt(curShowTime/3600); var min = parseInt((curShowTime - hour*3600)/60); var sec = parseInt(curShowTime%60); cxt.clearRect(0,0,CVS_WIDTH,CVS_HEIGHT); renderDigit(cxt,PADDINGLEFT,MARGIN_TOP,parseInt(hour/10)); renderDigit(cxt,PADDINGLEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(hour%10)); renderDigit(cxt,PADDINGLEFT+31*(RADIUS+1),MARGIN_TOP,10); renderDigit(cxt,PADDINGLEFT+40*(RADIUS+1),MARGIN_TOP,parseInt(min/10)); renderDigit(cxt,PADDINGLEFT+55*(RADIUS+1),MARGIN_TOP,parseInt(min%10)); renderDigit(cxt,PADDINGLEFT+70*(RADIUS+1),MARGIN_TOP,10); renderDigit(cxt,PADDINGLEFT+79*(RADIUS+1),MARGIN_TOP,parseInt(sec/10)); renderDigit(cxt,PADDINGLEFT+94*(RADIUS+1),MARGIN_TOP,parseInt(sec%10)); for(var i=0;i<balls.length;i++){ cxt.fillStyle = balls[i].color; cxt.beginPath(); cxt.arc(balls[i].x,balls[i].y,RADIUS,0,2*Math.PI); cxt.closePath(); cxt.fill(); } } function renderDigit(cxt,x,y,num){ cxt.fillStyle = color; for(var i=0;i<digit[num].length;i++){ for(var j=0;j<digit[num][i].length;j++){ if(digit[num][i][j] == 1){ cxt.beginPath(); cxt.arc(x+j*2*(RADIUS+1)+(RADIUS+1),y+i*2*(RADIUS+1)+(RADIUS+1),RADIUS,0,2*Math.PI); cxt.closePath(); cxt.fill(); } } } } }
实例出处:慕课网http://www.imooc.com/,喜欢的朋友可以到慕课网搜索学习哦~