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

用JavaScript+Canvas来实现最简单的时钟效果,供大家参考,具体内容如下

本文实例为大家分享了js
Canvas圆形时钟的具体实现代码,供大家参考,具体内容如下

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

效果图:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Canvas Clock</title>
    <style type="text/css">

      div{
        text-align: center;
        margin-top: 250px;
      }
      #clock{

        border: 1px solid #ccc;
      }
    </style>
  </head>
  <body>
    <div>
      <canvas id="clock" height="200px" width="200px"></canvas>
    </div>

    <script type="text/javascript" src="js/clock.js"></script>
  </body>
</html>

1. clock.html   

图片 1

js

<!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>

先看html代码:

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);

2. JavaScript代码

<html>
 <head>
 <meta charset="UTF-8">
 <title></title>
 <script type="text/javascript" src="js/demo3.js" ></script>
 </head>
  <body>
 <canvas id = "canvas"></canvas>
  </body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

<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>

JavaScript代码:

您可能感兴趣的文章:

  • 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实现时钟效果

以上就是本文的全部内容,希望对大家学习javascript程序设计有所帮助。

var canvas,context;
function draw(){//定义划时钟的方法
 var data = new Date();
 var hHoure = data.getHours();
 var mMin = data.getMinutes();
 var sSec = data.getSeconds();
 var hValue = (hHoure*30+mMin/2-90)*Math.PI/180; 
 var mValue = (mMin*6-90)*Math.PI/180;
 var sValue = (sSec*6 -90)*Math.PI/180;
 var x = 200,y = 200,r = 150;

 context.clearRect(0,0,canvas.width,canvas.height);
 context.moveTo(x,y);
 context.arc(x,y,r,0,6*Math.PI/180,false);
 //
 context.beginPath();
 context.lineWidth = 1;
 for(var i = 0;i<60;i++){
 context.moveTo(x,y);
 context.arc(x,y,r,6*i*Math.PI/180,6*(i+1)*Math.PI/180,false);
 }
 context.closePath();
 context.stroke();
 //
 context.beginPath();
 context.fillStyle = "white";
 context.moveTo(x,y);
 context.arc(x,y,r/1.1,-0,2*Math.PI,false);
 context.closePath();
 context.fill();
 // 
 context.beginPath();
 context.lineWidth = 3;
 for(var i = 0;i<12;i++){
 context.moveTo(x,y);
 context.arc(x,y,r,30*i*Math.PI/180,30*(i+1)*Math.PI,false);
 }
 context.closePath();
 context.stroke();
 //
 context.beginPath();
 context.fillStyle = "white";
 context.moveTo(x,y);
 context.arc(x,y,r/1.12,0,2*Math.PI,false);
 context.closePath();
 context.fill();

 context.beginPath();
 context.fillStyle = "black";
 context.moveTo(x,y);
 context.arc(x,y,r/30,0,2*Math.PI,false);
 context.fill(); 
 //
 context.beginPath();
 context.lineWidth = 5;
 context.moveTo(x,y);
 context.arc(x,y,r/2.5,hValue,hValue,false);
 context.stroke();
 //
 context.beginPath();
 context.lineWidth = 3;
 context.moveTo(x,y);
 context.arc(x,y,r/2,mValue,mValue,false);
 context.stroke(); 
 //
 context.beginPath();
 context.lineWidth = 2;
 context.moveTo(x,y);
 context.arc(x,y,r/1.6,sValue,sValue,false);
 context.stroke();
}
window.onload = function(){
 canvas = document.getElementById('canvas');
 context = canvas.getContext('2d');
 canvas.height = 500;
 canvas.width = 500;
 setInterval(draw,1000); 
 draw(); 
}

您可能感兴趣的文章:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

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