canvas教程

canvas实现动态时钟

字号+ 作者:H5之家 来源:H5之家 2017-10-23 16:00 我要评论( )

!DOCTYPEhtmlhtmlheadmetacharset=UTF-8title/title/headbodycanvasid=mycanvaswidth=1000height="

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <canvas id="mycanvas" width="1000" height="1000" style="border: 1px solid;"> </canvas> <script type="text/javascript"> var can = document.getElementById("mycanvas"); var cxt = can.getContext("2d"); function drawbase(){ cxt.beginPath(); cxt.rect(100,300,400,300); cxt.stroke(); cxt.closePath(); cxt.beginPath(); cxt.moveTo(100,300); cxt.bezierCurveTo(110,30,490,30,500,300); cxt.stroke(); cxt.closePath(); cxt.beginPath(); cxt.moveTo(470,190); cxt.quadraticCurveTo(480,50,495,260); cxt.stroke(); cxt.closePath(); cxt.beginPath(); cxt.arc(300,200,90,0,Math.PI*2,true); cxt.stroke(); cxt.closePath(); //表盘刻度 时刻度 cxt.save() cxt.translate(300,200); for(var i = 0; i<12; i++){ cxt.rotate(Math.PI/6); cxt.beginPath(); cxt.moveTo(0,-90); cxt.lineTo(0,-80); cxt.stroke(); cxt.closePath(); } cxt.restore(); //分刻度 cxt.save() cxt.translate(300,200); for(var i = 0; i<60; i++){ cxt.rotate(Math.PI/30); cxt.beginPath(); cxt.moveTo(0,-90); cxt.lineTo(0,-85); cxt.stroke(); cxt.closePath(); } cxt.restore(); } //钟摆 var b = 0; function zb(){ if(b==0){ cxt.beginPath(); cxt.moveTo(300,300); cxt.lineTo(200,500); cxt.stroke(); cxt.closePath(); cxt.beginPath(); cxt.arc(200,500,10,0,Math.PI*2,true); cxt.fill(); cxt.closePath(); b = 1; }else{ cxt.beginPath(); cxt.moveTo(300,300); cxt.lineTo(400,500); cxt.stroke(); cxt.closePath(); cxt.beginPath(); cxt.arc(400,500,10,0,Math.PI*2,true); cxt.fill(); cxt.closePath(); b = 0; } } //冒烟 var y = 0; function maoy(){ if(y==0){ cxt.beginPath(); cxt.arc(480,50,10,0,Math.PI*2,true); cxt.fill(); cxt.closePath(); y = 1; }else{ cxt.beginPath(); cxt.arc(480,80,10,0,Math.PI*2,true); cxt.fill(); cxt.closePath(); y = 0; } } //指针 function clock(){ var date = new Date(); var second = date.getSeconds(); var min = date.getMinutes(); var hour = date.getHours(); //秒针 cxt.save(); cxt.translate(300,200); cxt.rotate(second*Math.PI/30); cxt.beginPath(); cxt.moveTo(0,10); cxt.lineTo(0,-75); cxt.stroke(); cxt.closePath(); cxt.restore(); //分针 cxt.save(); cxt.translate(300,200); cxt.rotate(min*Math.PI/30); cxt.beginPath(); cxt.moveTo(0,5); cxt.lineTo(0,-70); cxt.stroke(); cxt.closePath(); cxt.restore(); //时针 cxt.save(); cxt.translate(300,200); cxt.rotate((hour+min/60)*Math.PI/6); cxt.beginPath(); cxt.moveTo(0,5); cxt.lineTo(0,-65); cxt.stroke(); cxt.closePath(); cxt.restore(); } function draw(){ cxt.clearRect(0,0,1000,800); drawbase(); zb(); maoy(); clock(); } draw(); setInterval(draw,1000); </script> </body> </html>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • canvas学习笔记(中篇) 10h搞定canvas基础系列

    canvas学习笔记(中篇) 10h搞定canvas基础系列

    2017-10-23 14:27

  • Canvas事件处理

    Canvas事件处理

    2017-10-23 08:13

  • html5 canvas动画教程:亲手做一个类似windows画图软件

    html5 canvas动画教程:亲手做一个类似windows画图软件

    2017-10-23 08:04

  • 炫丽的倒计时效果Canvas绘图与动画视频的资源推荐

    炫丽的倒计时效果Canvas绘图与动画视频的资源推荐

    2017-10-22 18:01

网友点评
l