JS+Canvas實現動態時鐘效果

基於Canvas制作的動態時鐘demo,供大傢參考,具體內容如下

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>動態時鐘</title>
  <script type="text/javascript" src="js/lattice.js"></script>
  <script type="text/javascript" src="js/script.js"></script>
  <style type="text/css">
   *{
    padding: 0;
    margin: 0;
   }
   #canvasBox{
    border-width: 2px;
    border-color: black;
    border-style: solid;
    width: 80%;
    margin: 0 auto;
   }
  </style>
 </head>
 <body>
  <!-- 將canvas放到一個盒子中,用盒子控制canvas的位置 -->
  <div id="canvasBox">
   <canvas id="canvas">
    此瀏覽器不支持canvas
   </canvas>
  </div>
 </body>
</html>

lattice.js

lattice =
    [
        [
            [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,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]
        ],//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]
        ],//2
        [
            [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]
        ],//3
        [
            [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]
        ],//4
        [
            [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]
        ],//5
        [
            [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]
        ],//6
        [
            [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]
        ],//7
        [
            [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]
        ],//8
        [
            [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]
        ],//9
        [
            [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]
        ]//:
    ];

script.js

var CANVAS_WIDTH;
var CANVAS_HEIGHT = 600;
//設置點陣中圓的半徑
var RADIUS = 8;
//設置時鐘距離邊框的位置
var MARGIN_LEFT = 100;
var MARGIN_TOP = 40;
 
//獲取初始狀態的時間
var time = new Date();
var hours = time.getHours();
var minutes = time.getMinutes();
var seconds = time.getSeconds();
 
//用於存儲小球對象
var balls = [];
//小球顏色
var colors = ["red","orenge","yellow","green","blue","purple","pink"]
 
window.onload = function(){
 //獲得canvas外部盒子的寬度,使canvas與外部盒子寬度相同
 var canvasBox = document.getElementById("canvasBox");
 var boxWidth = getComputedStyle(canvasBox,null);
 CANVAS_WIDTH = parseInt(boxWidth.width);
 
 //設置canvas寬高並獲得畫筆對象
 var canvas = document.getElementById("canvas");
 canvas.width = CANVAS_WIDTH;
 canvas.height = CANVAS_HEIGHT;
 var context = canvas.getContext("2d");
 
 //定時調用,50毫秒調用一次
 setInterval(function(){
  //繪制函數
  draw(context);
 },50);
 
 function draw(ctx){
  //刷新屏幕
  ctx.clearRect(0,0,CANVAS_WIDTH,CANVAS_HEIGHT);
  
  //獲取當前時間
  var curTime = new Date();
  var curHours = curTime.getHours();
  var curMinutes = curTime.getMinutes();
  var curSeconds = curTime.getSeconds();
 
  //畫小時
  drawLattice(MARGIN_LEFT,MARGIN_TOP,parseInt(curHours/10),ctx);
  drawLattice(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(curHours%10),ctx);
  //畫冒號
  drawLattice(MARGIN_LEFT+30*(RADIUS+1),MARGIN_TOP,10,ctx);
  //畫分鐘
  drawLattice(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes/10),ctx);
  drawLattice(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes%10),ctx);
  //畫冒號
  drawLattice(MARGIN_LEFT+69*(RADIUS+1),MARGIN_TOP,10,ctx);
  //畫秒
  drawLattice(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(curSeconds/10),ctx);
  drawLattice(MARGIN_LEFT+93*(RADIUS+1),MARGIN_TOP,parseInt(curSeconds%10),ctx);
  
  //更新時間並繪制小球
  update(context,curHours,curMinutes,curSeconds);
  
  //觀察小球數量
  //console.log(balls.length);
 }
 
 //參數:
 //x:繪制數字點陣的左上角的橫坐標
 //y:繪制數字點陣的左上角的縱坐標
 //num:要繪制的數字,實際上是lattice的索引,10表示冒號
 //ctx:畫筆
 function drawLattice(x,y,num,ctx){
  for(var i=0;i<lattice[num].length;i++){
   for(var j=0;j<lattice[num][i].length;j++){
    if(lattice[num][i][j]==1){
     ctx.beginPath();
     ctx.fillStyle = "blue";
     ctx.arc(x+j*2*(RADIUS+1)+(RADIUS+1),y+i*2*(RADIUS+1)+(RADIUS+1),RADIUS,0,2*Math.PI);
     ctx.fill();
     ctx.closePath();
    }
   }
  }
 }
 
 //更新時間並繪制小球
 function update(ctx,curHours,curMinutes,curSeconds){
  if(seconds!=curSeconds){
   //更新小時
   if(parseInt(curHours/10)!=parseInt(hours/10)){
    addBalls(MARGIN_LEFT,MARGIN_TOP,parseInt(curHours/10));
   }
   if(parseInt(curHours%10)!=parseInt(hours%10)){
    addBalls(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(curHours%10));
   }
   //更新分鐘
   if(parseInt(curMinutes/10)!=parseInt(minutes/10)){
    addBalls(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes/10));
   }
   if(parseInt(curMinutes%10)!=parseInt(minutes%10)){
    addBalls(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes%10));
   }
   //更新秒
   if(parseInt(curSeconds/10)!=parseInt(seconds/10)){
    addBalls(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(curSeconds/10));
   }
   if(parseInt(curSeconds%10)!=parseInt(seconds%10)){
    addBalls(MARGIN_LEFT+93*(RADIUS+1),MARGIN_TOP,parseInt(curSeconds%10));
   }
   
   //更新所有時間
   hours = curHours;
   minutes = curMinutes;
   seconds = curSeconds;
  }
  //繪制小球
  drawBalls(ctx);
  //更新小球運動
  updateBalls();
 }
 
 //添加單個小球
 //參數:
 //x:小球的橫坐標
 //y:小球的縱坐標
 //num:變化的時間數字
 function addBalls(x,y,num){
  for(var i=0;i<lattice[num].length;i++){
   for(var j=0;j<lattice[num][i].length;j++){
    if(lattice[num][i][j]==1){
     //添加一個小球對象
     var ball = {
      //坐標
      x:x+j*2*(RADIUS+1)+(RADIUS+1),
      y:y+i*2*(RADIUS+1)+(RADIUS+1),
      //重力加速度
      g:1.5+Math.random(),
      //x、y方向的速度
      vx:Math.pow(-1,Math.ceil(Math.random()*1000))*5,
      vy:-5,
      //小球顏色
      color:colors[Math.floor(Math.random()*colors.length)]
     }
     //將小球添加進儲存隊列中
     balls.push(ball);
    }
   }
  }
 }
 
 //繪制小球
 function drawBalls(ctx){
  for(var i=0;i<balls.length;i++){
   ctx.beginPath();
   ctx.fillStyle = balls[i].color;
   ctx.arc(balls[i].x,balls[i].y,RADIUS,0,2*Math.PI,true);
   ctx.fill();
   ctx.closePath();
  }
 }
 
 //更新小球運動
 function updateBalls(){
  //更新儲存數組中的小球的動態
  for(var i=0;i<balls.length;i++){
   balls[i].x += balls[i].vx;
   balls[i].y += balls[i].vy;
   balls[i].vy += balls[i].g;
   
   //邊緣碰撞檢測
   if(balls[i].y>=CANVAS_HEIGHT-RADIUS){
    balls[i].y=CANVAS_HEIGHT-RADIUS;
    //模擬碰撞反彈和阻力
    balls[i].vy=-balls[i].vy*0.6;
   }
  }
  //當小球滾出畫佈便可以從數組中刪除,以下是刪除算法
  //儲存應當保留的小球的數量
  var numBall = 0;
  for(var i=0;i<balls.length;i++){
   if(balls[i].x+RADIUS>0&&balls[i].x-RADIUS<CANVAS_WIDTH){
    //若判斷當前小球在屏幕內,則將此小球盡量靠前,則每次循環後,數組最後面的小球就是滾出畫佈的小球
    balls[numBall] = balls[i];
    numBall++;
   }
  }
  //從數組後面刪除小球
  while(balls.length>numBall){
   balls.pop();
  }
 }
};

效果圖:

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: