html画表盘 随时间转动,HTML5 canvas圆形时钟指针平缓转动随机切换表盘颜色

特效描述:HTML5 canvas圆形时钟 指针平缓转动 随机切换表盘颜色。使用HTML5制作的时钟,时针分针秒针平滑转动而不是蹦着走,每次进入网页或单机表盘会随即切换表盘边框的颜色。

代码结构

1. HTML代码

您的浏览器不支持canvas标签!

var clock=document.getElementById("clock");

var cxt=clock.getContext("2d");

var colr=(200+Math.floor(Math.random()*55.99));

var colg=(200+Math.floor(Math.random()*55.99));

var colb=(200+Math.floor(Math.random()*55.99));

function resetcolor(){

colr=(200+Math.floor(Math.random()*55.99));

colg=(200+Math.floor(Math.random()*55.99));

colb=(200+Math.floor(Math.random()*55.99));

}

function drawClock(){

//获取时间

var now=new Date();

var hou=now.getHours();

var min=now.getMinutes();

var sec=now.getSeconds();

var mec=now.getMilliseconds();

//转换12小时进制

hou=hou>12?hou-12:hou;

//清空画布

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

//阴影

cxt.fill();

cxt.fillStyle="gray";

cxt.beginPath();

cxt.arc(270,260,205,0,Math.PI*2,true);

cxt.closePath();

cxt.fill();

//表盘

cxt.fill();

cxt.fillStyle="white";

cxt.beginPath();

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

cxt.closePath();

cxt.fill();

//蓝色表盘(边框)

cxt.strokeStyle="#dddddd"

cxt.lineWidth=10;

cxt.beginPath();

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

cxt.closePath();

cxt.stroke();

cxt.strokeStyle="#"+colr.toString(16)+colg.toString(16)+colb.toString(16);

cxt.lineWidth=10;

cxt.beginPath();

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

cxt.closePath();

cxt.stroke();

cxt.strokeStyle="#"+colr.toString(16)+colg.toString(16)+colb.toString(16);

cxt.lineWidth=3;

cxt.beginPath();

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

cxt.closePath();

cxt.stroke();

cxt.strokeStyle="#"+(colr-100).toString(16)+(colg-100).toString(16)+(colb-100).toString(16);

cxt.lineWidth=5;

cxt.beginPath();

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

cxt.closePath();

cxt.stroke();

//刻度(时分)

//时刻度

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

cxt.save();

cxt.lineWidth=5;

cxt.strokeStyle="black";

//设置原点

cxt.translate(250,250);

//设置旋转角度;

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

cxt.beginPath();

cxt.moveTo(0,180);

cxt.lineTo(0,160);

cxt.closePath();

cxt.stroke();

cxt.restore();

}

//分刻度

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

if(i%5!=0){

cxt.save();

cxt.lineWidth=2;

cxt.strokeStyle="black";

//设置原点

cxt.translate(250,250);

//设置旋转角度;

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

cxt.beginPath();

cxt.moveTo(0,175);

cxt.lineTo(0,165);

cxt.closePath();

cxt.stroke();

cxt.restore();

}

}

//指针(时分秒)

//时针

cxt.save();

cxt.lineWidth=12;

cxt.strokeStyle="black";

cxt.beginPath();

cxt.translate(250,250);

cxt.rotate((hou*30+min*0.5+180)/180*Math.PI);

cxt.moveTo(0,0);

cxt.lineTo(0,90);

cxt.closePath();

cxt.stroke()

cxt.restore();

//分针

cxt.save();

cxt.lineWidth=5;

cxt.strokeStyle="black";

cxt.beginPath();

cxt.translate(250,250);

cxt.rotate((min*6+sec*0.1+180)/180*Math.PI);

cxt.moveTo(0,0);

cxt.lineTo(0,130);

cxt.closePath();

cxt.stroke()

cxt.restore();

//秒针

cxt.save();

cxt.lineWidth=2;

cxt.strokeStyle="red";

cxt.beginPath();

cxt.translate(250,250);

cxt.rotate((sec*6+mec*0.006+180)/180*Math.PI);

cxt.moveTo(0,0);

cxt.lineTo(0,150);

cxt.closePath();

cxt.stroke();

cxt.fillStyle="red";

cxt.beginPath();

cxt.arc(0,148,4,0,Math.PI*2,true);

cxt.closePath();

cxt.fill();

cxt.fillStyle="white";

cxt.beginPath();

cxt.arc(0,148,2,0,Math.PI*2,true);

cxt.closePath();

cxt.fill();

cxt.restore();

//秒针装饰

cxt.fillStyle="red";

cxt.beginPath();

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

cxt.closePath();

cxt.fill();

cxt.fillStyle="black";

cxt.beginPath();

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

cxt.closePath();

cxt.fill();

cxt.fillStyle="white";

cxt.beginPath();

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

cxt.closePath();

cxt.fill();

}

drawClock();

//使用setInterval(代码,周期(毫秒制))让时钟动起来

setInterval(drawClock,10);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值