用CANVAS画个时钟
2015-06-06 23:56
393 查看
<!doctype html> <html> <head> <meta charset="utf-8"> <title>html5实战</title> <style> *{padding: 0; margin: 0;} body{background: #000;} canvas{background: #fff;} </style> <script> window.onload=function(){ var oC = document.getElementById('canvas'); var oGC = oC.getContext('2d'); var x=200,y=200,r=150; function draw(){ oGC.clearRect(0,0,oC.width,oC.height); var oDate = new Date(); var oHour = oDate.getHours(); var oMin = oDate.getMinutes(); var oSec = oDate.getSeconds(); var hourValue = (oHour*30-90 + oMin/2)*Math.PI/180; var minValue = (oMin*6-90)*Math.PI/180; var secValue = (oSec*6-90)*Math.PI/180; oGC.beginPath(); for(var i=0;i<60;i++){ oGC.moveTo(x,y); oGC.arc(x,y,r,i*6*Math.PI/180,(i+1)*6*Math.PI/180,false); } oGC.closePath(); oGC.stroke(); oGC.fillStyle="#fff"; oGC.beginPath(); oGC.arc(x,y,r*19/20,0,2*Math.PI,false); oGC.closePath(); oGC.fill() oGC.lineWidth=3; oGC.beginPath(); for(var i=0;i<12;i++){ oGC.moveTo(x,y); oGC.arc(x,y,r,i*30*Math.PI/180,(i+1)*30*Math.PI/180,false); } oGC.closePath(); oGC.stroke(); oGC.fillStyle="#fff"; oGC.beginPath(); oGC.arc(x,y,r*18/20,0,2*Math.PI,false); oGC.closePath(); oGC.fill() //画时针 oGC.lineWidth=5; oGC.beginPath(); oGC.moveTo(x,y); oGC.arc(x,y,r*10/20,hourValue,hourValue,false); oGC.closePath(); oGC.stroke(); //画分针 oGC.lineWidth=3; oGC.beginPath(); oGC.moveTo(x,y); oGC.arc(x,y,r*14/20,minValue,minValue,false); oGC.closePath(); oGC.stroke(); oGC.lineWidth=1; oGC.beginPath(); oGC.moveTo(x,y); oGC.arc(x,y,r*19/20,secValue,secValue,false); oGC.closePath(); oGC.stroke(); } setInterval(function(){ draw(); },1000) } </script> </head> <body> <canvas id="canvas" width='400' height="400"></canvas> </body> </html>
相关文章推荐
- Latex 主题设置
- centos下挂载NTFS移动硬盘
- 为何需要总结
- 文章标题
- 读书笔记:机器学习实战(1)——章2的knn代码和个人改进与注释
- DICOM:Ubuntu14环境下安装dcm4chee+oviyam2.1
- 正则表达式
- OK6410开发板在make uboot时报错
- 原子性(atomicity)
- mysql 用户角色权限表建立
- 声波通讯笔记
- Piggy-Bank
- java基础Runtime类的使用
- hibernate必须的包下载以及各个包的作用
- 适合所有测试人员读的书籍
- c的部分储存类1分类说明
- Ubuntu15.04下切换openJRE7到Oracle JDK8的记录,网上其他资料不全!update-alternatives!
- 设计模式之十一:抽象工厂模式(Abstract Factory)
- 在Win7系统中配置Android SDK环境变量
- android equalizer