canvas时钟

var oc = document.getElementById("c1");

var ogc = oc.getContext("2d");

function toDraw() {

var x= 200;

var y= 200;

var r= 150;

ogc.clearRect(0,0,oc.width,oc.height);//每次清空画布

    var oDate= new Date();

var oHours= oDate.getHours();

var oMin= oDate.getMinutes();

var oSec= oDate.getSeconds();

//

    var oHoursValue= (-90 + oHours* 30 + oMin/ 2 + oSec/ 120)* Math.PI/ 180;

var oMinValue= (-90 + oMin* 6 + oSec/ 10)* Math.PI/ 180;

var oSecValue= (-90 + oSec* 6)* Math.PI/ 180;

// 绘制表盘  每格角度 360/60=6°  角度*π/180 = 弧度

    ogc.beginPath();

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

ogc.moveTo(x, y);

ogc.arc(x, y, r,6 * i* Math.PI/ 180,6 * (i+ 1)* Math.PI/ 180,false);

}

ogc.closePath();

ogc.stroke();

ogc.fillStyle= 'white';

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 19 / 20,0,360 * Math.PI/ 180,false);

ogc.closePath();

ogc.fill();

ogc.beginPath();

ogc.lineWidth= 3;

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

ogc.moveTo(x, y);

ogc.arc(x, y, r,30 * i* Math.PI/ 180,30 * (i+ 1)* Math.PI/ 180,false);

}

ogc.closePath();

ogc.stroke();

ogc.fillStyle= 'white';

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 18 / 20,0,360 * Math.PI/ 180,false);

ogc.closePath();

ogc.fill();

ogc.lineWidth= 5;

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 10 / 20, oHoursValue, oHoursValue,false);

ogc.closePath();

ogc.stroke();

ogc.lineWidth= 3;

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 15 / 20, oMinValue, oMinValue,false);

ogc.closePath();

ogc.stroke();

ogc.lineWidth= 1;

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 19 / 20, oSecValue, oSecValue,false);

ogc.closePath();

ogc.stroke();

}

setInterval(function () {

toDraw();

},1000);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 先来看一下效果图 下面是代码 哈哈哈 <!DOCTYPE html> 时钟 #clock{/border...
    小飞侠zzr阅读 1,652评论 0 0
  • 一:canvas简介 1.1什么是canvas? ①:canvas是HTML5提供的一种新标签 ②:HTML5 ...
    GreenHand1阅读 10,215评论 2 32
  • 打开想写点什么,有很多很多想法又不知道去写些什么。 写日记这个东西来说有很多很多的好处,但是贵在坚持。我自身有很大...
    1李一晨阅读 2,726评论 0 0
  • 脚踩在淤泥里 但心要向光明
    靖源小王子阅读 1,081评论 0 0
  • 我现在是个活在三线城市的普通女孩,而且没有多牢靠的工作。上了四年大学还跟个打工妹一样,甚至混的不如同龄的打工妹。这...
    我爱吃鱼吃火锅阅读 1,756评论 0 1

友情链接更多精彩内容