html画表盘 随时间转动,canvas绘制表盘时钟

canvas绘制表盘

您的浏览器不支持canvas

var box = document.getElementById('box');

var cxt = box.getContext('2d');

// 时钟动起来

var timer = null;

function clock(){

var date = new Date();

var h = date.getHours();

h = h + h/60;

h = h>12? h-12:h;

var m = date.getMinutes();

var s = date.getSeconds();

// 清画布

cxt.clearRect(0,0,500,500);

// 画表盘

cxt.strokeStyle = '#f0f';

cxt.lineWidth = 6;

cxt.beginPath();

cxt.arc(250,250,100,0,2*Math.PI);

cxt.stroke();

// 画时钟刻度

for(var i=0; i<12; i++){

cxt.save();

cxt.translate(250,250);

cxt.rotate(30*i*Math.PI/180);

cxt.lineWidth = 3;

cxt.beginPath();

cxt.moveTo(0,-80);

cxt.lineTo(0,-92);

cxt.stroke();

cxt.restore();

}

//画分钟刻度

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

cxt.save();

cxt.translate(250,250);

cxt.rotate(6*i*Math.PI/180);

cxt.lineWidth = 2;

cxt.beginPath();

cxt.moveTo(0,-86);

cxt.lineTo(0,-92);

cxt.stroke();

cxt.restore();

}

// 画时针

cxt.save();

cxt.lineWidth = 5;

cxt.translate(250,250);

cxt.rotate(h*30*Math.PI/180);

cxt.beginPath();

cxt.moveTo(0,6);

cxt.lineTo(0,-40);

cxt.stroke();

cxt.restore();

// 画分针

cxt.save();

cxt.lineWidth = 3;

cxt.translate(250,250);

cxt.rotate(m*6*Math.PI/180);

cxt.beginPath();

cxt.moveTo(0,8);

cxt.lineTo(0,-60);

cxt.stroke();

cxt.restore();

// 画秒针

cxt.save();

cxt.lineWidth = 1;

cxt.translate(250,250);

cxt.rotate(s*6*Math.PI/180);

cxt.beginPath();

cxt.moveTo(0,10);

cxt.lineTo(0,-75);

cxt.stroke();

cxt.restore();

// 画中心的小圆固定三根针

cxt.save();

cxt.beginPath();

cxt.fillStyle = '#0f0';

cxt.lineWidth = 2;

cxt.translate(250,250);

cxt.arc(0,0,2,0,360,false);

cxt.stroke();

cxt.fill();

cxt.restore();

// 画秒针上的园

cxt.save();

cxt.fillStyle = '#f00';

cxt.lineWidth = 2;

cxt.translate(250,250);

cxt.rotate(s*6*Math.PI/180);

cxt.beginPath();

cxt.arc(0,-60,2,0,360,false);

cxt.stroke();

cxt.fill();

cxt.restore();

}

clock();

timer = setInterval(clock,1000);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值