Canvas绘制动态时钟效果,Canvas实现时钟效果

作者:江苏福彩快三计算机网络
<!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Title</title>
<style>
*{margin:0; padding:0;}
body{ background:#000; text-align:center;}
canvas{ background:#fff;}
</style>
<script>
function toDou(iNum){
 return iNum<10?'0' iNum:'' iNum;
}
function d2a(n){
 return n*Math.PI/180;
}
window.onload = function(){
 var oC = document.getElementById('c1');
 var gd = oC.getContext('2d');
 var cx = oC.width/2,
 cy = oC.height/2;
 function clock(){
 gd.clearRect(0,0,oC.width,oC.height);
 var oDate = new Date();
 var H = oDate.getHours();
 var M = oDate.getMinutes();
 var S = oDate.getSeconds();
 var MS = oDate.getMilliseconds();
 drawArc(60,0,(H*30 M/60*30),'orange');
 drawArc(80,0,(M*6 S/60*6),'purple');
 drawArc(100,0,(S*6 MS/1000*6),'aqua');
 var str = toDou(H) ':' toDou(M) ':' toDou(S);
 gd.font = '20px 微软雅黑';
 gd.textAlign = 'center';
 gd.textBaseline = 'middle';
 gd.fillText(str,cx,cy);
 }
 clock()
 setInterval(clock,1);
 function drawArc(r,s,e,color){
 color = color||'black';
 gd.beginPath();
 gd.arc(cx,cy,r,d2a(s-90),d2a(e-90),false);
 gd.strokeStyle = color;
 gd.lineWidth = 20;
 gd.stroke();
 }
};
</script>
</head>
<body>
 <canvas id="c1" width="800" height="600"></canvas>
</body>
</html>

先看html代码:

本文实例为大家分享了使用canvas绘制时钟的具体代码,供大家参考,具体内容如下

本文实例为大家分享了Canvas绘制动态时钟效果展示的具体代码,供大家参考,具体内容如下

var dom=document.getElementById('clock');
var cxt=dom.getContext("2d");
var width=cxt.canvas.width;
var height=cxt.canvas.height;
var r=width/2;

function drawBackground(){
  cxt.save();
  cxt.translate(r,r);
  cxt.beginPath();
  cxt.lineWidth=10;
  cxt.arc(0,0,r-5,0,2*Math.PI,false);
  cxt.stroke();
  cxt.font="18px Arial";
  cxt.textAlign='center'
  cxt.textBaseline='middle'
  var hourNums=[3,4,5,6,7,8,9,10,11,12,1,2];
  hourNums.forEach(function(number,i){

    var rad=2*Math.PI/12*i;
    var x=Math.cos(rad)*(r-30);
    var y=Math.sin(rad)*(r-30);
    cxt.fillText(number,x,y);

  });

  for(var i=0;i<60;i  ){

    var rad=2*Math.PI/60*i;
    var x=Math.cos(rad)*(r-18);
    var y=Math.sin(rad)*(r-18);
    cxt.beginPath();
    if(i % 5===0){
      cxt.fillStyle="#000"
      cxt.arc(x,y,2,0,2*Math.PI,false);
    }
    else{
      cxt.fillStyle="#ccc"
      cxt.arc(x,y,2,0,2*Math.PI,false);
    }
    cxt.fill(); 
  }

}

function drawHour(hour,minute){
  cxt.save();
  cxt.beginPath();
  var rad=2*Math.PI/12*hour;
  var mrad=2*Math.PI/12/60*minute
  cxt.rotate(rad mrad);
  cxt.lineWidth=6;
  cxt.lineCap='round'
  cxt.moveTo(0,10);
  cxt.lineTo(0,-r/2);
  cxt.stroke();
  cxt.restore();
}

function drawMinute(minute){
  cxt.save();
  cxt.beginPath();
  var rad=2*Math.PI/60*minute;
  cxt.rotate(rad);
  cxt.lineWidth=3;
  cxt.lineCap='round'
  cxt.moveTo(0,10);
  cxt.lineTo(0,-r 30);
  cxt.stroke();
  cxt.restore();
}

function drawSecond(second){
  cxt.save();
  cxt.beginPath();
  cxt.fillStyle='#c14543'
  var rad=2*Math.PI/60*second;
  cxt.rotate(rad);  
  cxt.moveTo(-2,20);
  cxt.lineTo(2,20);
  cxt.lineTo(1,-r 18);
  cxt.lineTo(-1,-r 18);
  cxt.fill();
  cxt.restore();
}

function drawDot(){

  cxt.beginPath();
  cxt.fillStyle='#fff'
  cxt.arc(0,0,3,0,2*Math.PI,false);
  cxt.fill();
}

function draw(){

  cxt.clearRect(0,0,width,height);
  var now=new Date();
  var hour=now.getHours();
  var minute=now.getMinutes();
  var second=now.getSeconds();
  drawBackground();
  drawHour(hour,minute);
  drawMinute(minute);
  drawSecond(second);
  drawDot();
  cxt.restore();
}
draw();
setInterval(draw,1000);

代码如下:

效果图:

<!doctype html>
<html lang="en">
 <head>
 <meta charset="UTF-8">
 <meta name="Description" content="">
 <title>canvas时钟</title>
 </head>
 <body>
  <canvas id="canvas" width="300" height="300"></canvas>
  <script>
    var canvas=document.getElementById("canvas");
    var context=canvas.getContext('2d');
    //draw();
    function draw(){
      //得到当前系统的时分秒
      var now=new Date();
      var sec=now.getSeconds();
      var min=now.getMinutes();
      var hour=now.getHours();
      hour>=12&&(hour=hour-12);


      var radius = Math.min(canvas.width/2,canvas.height/2);
      //初始化画布
      context.save();
      context.clearRect(0,0,canvas.width,canvas.height);
       context.translate(canvas.width/2,canvas.height/2); 
      context.rotate(-Math.PI/2);
      context.save();

      //表框


      //小时刻度
      context.strokeStyle="black";
      context.fillStyle="black";
      context.lineWidth=3;
      context.lineCap="round";
      context.beginPath();
      for(var i=0;i<12;i  ){
        context.rotate(Math.PI/6);
        context.moveTo(radius-30,0);
        context.lineTo(radius-10,0);

      }
      context.stroke();
      context.restore();
      context.save();

      //分钟刻度
      context.lineWidth=2;
      context.beginPath();
      for(var i=0;i<60;i  ){
        if(!i%5==0){
        context.moveTo(radius-15,0);
        context.lineTo(radius-10,0);
        }
        context.rotate(Math.PI/30);
      }
      context.stroke();
      context.restore();
      context.save();

      //画上时针

       context.rotate((Math.PI/6)*hour (Math.PI/360)*min (Math.PI/21600)*sec);
       context.lineWidth=6;
       context.beginPath();
       context.moveTo(-10,0);
       context.lineTo(radius*0.5,0);
       context.stroke();
      context.restore();
      context.save();

       context.rotate((Math.PI/30)*min (Math.PI/1800)*sec);
   context.strokeStyle="#29A8DE";
   context.lineWidth=4;
   context.lineCap="butt";
   context.beginPath();
   context.moveTo(-20,0);
   context.lineTo(radius*0.7,0);
   context.stroke();
   context.restore();
   context.save();

   context.rotate(sec*Math.PI/30);
   context.strokeStyle="red";
   context.lineWidth=2;
   context.lineCap="butt";
   context.beginPath();
   context.moveTo(-30,0);
   context.lineTo(radius*0.9,0);
   context.stroke();
   context.restore();
   context.save();

      context.lineWidth=4;
       context.strokeStyle="gray";
      context.beginPath();
         context.arc(0,0,radius,0,Math.PI*2,true);
     context.stroke();
     context.restore();


     context.restore();
    }

    window.onload=function(){
      setInterval(draw,1000)
    }
  </script>
 </body>
</html>

您可能感兴趣的文章:

  • js Canvas绘制圆形时钟效果
  • js Canvas实现圆形时钟教程
  • JavaScript学习小结之使用canvas画“哆啦A梦”时钟
  • JS Canvas绘制时钟效果
  • js html5实现canvas绘制网页时钟的方法
  • JavaScript html5 canvas绘制时钟效果(二)
  • JavaScript html5 canvas绘制时钟效果
  • javascript结合Canvas 实现简易的圆形时钟
  • html5 canvas js(数字时钟)实例代码
  • JS H5 Canvas实现时钟效果

您可能感兴趣的文章:

  • JS Canvas绘制动态时钟效果
  • js Canvas实现圆形时钟教程
  • JavaScript学习小结之使用canvas画“哆啦A梦”时钟
  • JS Canvas绘制时钟效果
  • js html5实现canvas绘制网页时钟的方法
  • JavaScript html5 canvas绘制时钟效果(二)
  • JavaScript html5 canvas绘制时钟效果
  • javascript结合Canvas 实现简易的圆形时钟
  • html5 canvas js(数字时钟)实例代码
  • JS H5 Canvas实现时钟效果

本文由江苏福彩快三发布,转载请注明来源

关键词: 江苏福彩快三