[HTML5] 用CANVAS画时钟

本篇文章的所有代码转自 ACdreamers 的《第一个Canvas实例-钟表》 。原文地址为http://blog.csdn.net/acdreamers/article/details/17634209

本文主要是对代码中所使用的方法进行详细说明,虽然原作者在代码已经注释得很清楚了。


一、.获取上下文对象

    var cxt = document.getElementById('元素名').getContect('2d'); 

  IE8或更早的浏览器不支持<canvas>元素。

二、 drawClock()

   drawClock()实现画时钟的目的。

    1. clearRect() 清空给定矩形内的指定像素。

context.clearRect(x,y,width,height);

  x,y : 要清除的矩形左上角点的(x,y)坐标

width,height: 要清除的矩形宽度和高度,单位为像素

 2.new Date() --- 得到系统时间

  

 
 
  1. var sec = now.getSeconds();  
  2. var min = now.getMinutes();  
  3. var hour = now.getHours();  


 3.画时钟的形状

 
 
  1. cxt.beginPath();  
  2. cxt.lineWidth = 10;  
  3. cxt.strokeStyle = "blue";  
  4. cxt.arc(550, 310, 300, 0, 360, false);  
  5. cxt.closePath();  
  6. cxt.stroke(); 


      beginPath()的作用是canvas的绘制方法,都会以上一次beginPath之后的所有路径为基础进行绘制。

      closepath()是关闭路径,而不是结束路径,它会试图从当前路径的终点连一条路径到七点,让整个路径闭合起来。

      cxt.lineWidth() : 画笔的宽度

      cxt.strokeStyle() : 设置或返回用于笔触的颜色、渐变或模式。

      属性值:color      指示绘图笔触颜色的 CSS 颜色值。默认值是 #000000。

               gradient  用于填充绘图的渐变对象(线性放射性

               pattern    用于创建 pattern 笔触的 pattern 对象

stroke ()绘制已定义的路径 


   arc() 方法创建弧/曲线(用于创建圆或部分圆)。如需通过 arc() 来创建圆,请把起始角设置为 0,结束角设置为 2*Math.PI。

	context.arc(x,y,r,sAngle,eAngle,counterclockwise);

参数 描述
x 圆的中心的 x 坐标。
y 圆的中心的 y 坐标。
r 圆的半径。
sAngle 起始角,以弧度计。(弧的圆形的三点钟位置是 0 度)。
eAngle 结束角,以弧度计。
counterclockwise 可选。规定应该逆时针还是顺时针绘图。False = 顺时针,true = 逆时针。


4)drawScale --- 自定义函数画刻度


  • function drawPointer(width, color, value, angle, startx, starty, endx, endy){  
  •     cxt.save();                 //先保存当前画布  
  •     cxt.lineWidth = width;      //设置画笔的宽度  
  •     cxt.strokeStyle = color;    //设置画笔的颜色  
  •     cxt.translate(550, 310);    //重置异次元空间的原点坐标  
  •     cxt.rotate(value * angle * Math.PI / 180);  //设置旋转的角度,参数是弧度  
  •     cxt.beginPath();  
  •     cxt.moveTo(startx, starty);  
  •     cxt.lineTo(endx, endy);  
  •     cxt.closePath();            //先闭合路径,再画线  
  •     cxt.stroke();               //开始画线  
  •     cxt.restore();              //将旋转后的线段返回给画布  
  • }     

    translate() 方法重新映射画布上的 (0,0) 位置。

    rotate()方法旋转当前的绘图

        context.rotate(angle);

    angle为旋转角度,以弧度计。如需将角度转换为弧度,请使用 degrees*Math.PI/180 公式进行计算。

            举例:如需旋转 5 度,可规定下面的公式:5*Math.PI/180。

    save()   保存当前环境的状态

    restore()   返回之前保存过的路径状态和属性



    //获取上下文文档对象  
    var clock = document.getElementById('clock');  
    var cxt = clock.getContext('2d');  
      
    //画指针  
    function drawPointer(width, color, value, angle, startx, starty, endx, endy){  
        cxt.save();                 //先保存当前画布  
        cxt.lineWidth = width;      //设置画笔的宽度  
        cxt.strokeStyle = color;    //设置画笔的颜色  
        cxt.translate(550, 310);    //重置异次元空间的原点坐标  
        cxt.rotate(value * angle * Math.PI / 180);  //设置旋转的角度,参数是弧度  
        cxt.beginPath();  
        cxt.moveTo(startx, starty);  
        cxt.lineTo(endx, endy);  
        cxt.closePath();            //先闭合路径,再画线  
        cxt.stroke();               //开始画线  
        cxt.restore();              //将旋转后的线段返回给画布  
    }  
      
    //画刻度  
    function drawScale(size, width, color, value, startx, starty, endx, endy){  
        for(var i = 0; i < size; i++){  
            drawPointer(width, color, value, i, startx, starty, endx, endy);  
        }  
    }  
      
    //为表盘的中心填充颜色  
    function drawFill(){  
        cxt.save();  
        cxt.beginPath();  
        cxt.arc(550, 310, 7, 0, 360, false);  
        cxt.closePath();  
        cxt.fillStyle = "red";  
        cxt.fill();  
        cxt.restore();  
    }  
      
    //画时钟  
    function drawClock(){  
        cxt.clearRect(0, 0, 1350, 620);  //清空整个画布  
      
        var now = new Date();            //获取系统时间,取出时,分,秒  
        var sec = now.getSeconds();  
        var min = now.getMinutes();  
        var hour = now.getHours();  
        min += sec / 60;  
        hour += min / 60;  
        if(hour > 12) hour -= 12;  
      
        cxt.beginPath();  
        cxt.lineWidth = 10;  
        cxt.strokeStyle = "blue";  
        cxt.arc(550, 310, 300, 0, 360, false);  
        cxt.closePath();  
        cxt.stroke();  
      
        drawScale(12, 7, "pink", 30, 0, -280, 0, -260);      //画时刻度  
        drawScale(60, 5, "pink", 6,  0, -280, 0, -270);      //画分刻度  
        drawPointer(7, "purple", hour, 30, 0, 12, 0, -210);  //画时针  
        drawPointer(5, "yellow", min, 6, 0, 15, 0, -240);    //画分针  
        drawPointer(4, "red", sec, 6, 0, 17, 0, -250);       //画秒针  
      
        //细化秒针,为秒针加箭头  
        drawPointer(3, "red", sec, 6, -7, -235, 0, -255);  
        drawPointer(3, "red", sec, 6, 7, -235, 0, -255);  
        drawFill();  
    }  
      
    drawClock();  
    setInterval(drawClock, 1000);   //setInterval()方法中表示每隔1000ms,就执行drawClock一次  


    参数 描述
    x 圆的中心的 x 坐标。
    y 圆的中心的 y 坐标。
    r 圆的半径。
    sAngle 起始角,以弧度计。(弧的圆形的三点钟位置是 0 度)。
    eAngle 结束角,以弧度计。
    counterclockwise 可选。规定应该逆时针还是顺时针绘图。False = 顺时针,true = 逆时针。
  • function drawPointer(width, color, value, angle, startx, starty, endx, endy){  
  •     cxt.save();                 //先保存当前画布  
  •     cxt.lineWidth = width;      //设置画笔的宽度  
  •     cxt.strokeStyle = color;    //设置画笔的颜色  
  •     cxt.translate(550, 310);    //重置异次元空间的原点坐标  
  •     cxt.rotate(value * angle * Math.PI / 180);  //设置旋转的角度,参数是弧度  
  •     cxt.beginPath();  
  •     cxt.moveTo(startx, starty);  
  •     cxt.lineTo(endx, endy);  
  •     cxt.closePath();            //先闭合路径,再画线  
  •     cxt.stroke();               //开始画线  
  •     cxt.restore();              //将旋转后的线段返回给画布  
  • }  
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值