1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas钟表</title> <style media="screen"> body { background-color: #eee; } #canvas { border: 2px solid red; display: block; margin: 0 auto; } </style> </head> <body> <canvas id="canvas" width="600" height="600"> 您的浏览器不支持 canvas! 太 low 了,赶快升级吧! </canvas> <script type="text/javascript"> var canvas = document.getElementById("canvas"); var cxt = canvas.getContext("2d"); var positions = [ ["#333",0,0,200,0,Math.PI * 2,false], ["#fff",0,0,180,0,Math.PI * 2,false], ["#333",0,0,10,0,Math.PI * 2,false] ]; var deg = 0; function draw() { deg++; cxt.clearRect(0,0,canvas.width,canvas.height); cxt.save(); cxt.translate(300,300); cxt.rotate(-Math.PI/2); for (var i = 0; i < 3; i++) { cxt.beginPath(); cxt.fillStyle = positions[i][0]; cxt.arc(positions[i][1],positions[i][2],positions[i][3],positions[i][4],positions[i][5],positions[i][6]); cxt.fill(); cxt.closePath(); }
for (var i = 0; i < 60; i++) { cxt.save(); cxt.beginPath(); cxt.rotate(Math.PI/180 * (i*6)); if(i%5 == 0){ cxt.fillRect(-180,-5,20,10); } else { cxt.fillRect(-180,-2,10,4); } cxt.closePath(); cxt.restore(); } cxt.beginPath(); cxt.rotate(Math.PI/2); cxt.font = "50px '宋体'"; cxt.fillStyle = "crimson"; cxt.textAlign = "center"; cxt.fillText("canvas",5,100); cxt.closePath(); cxt.rotate(-Math.PI/2);
cxt.restore(); } draw(); function drawZhen() { var time = new Date(); var h = time.getHours(); var m = time.getMinutes(); var s = time.getSeconds(); cxt.save(); cxt.translate(300,300); cxt.rotate(Math.PI/180 * h * 30 + Math.PI/180 * m/60*30 + Math.PI/180 * s/3600*30); cxt.fillRect(-4,-110,8,130); cxt.restore(); cxt.save(); cxt.translate(300,300); cxt.rotate(Math.PI/180 * m * 6 + Math.PI/180 * s/60*6); cxt.fillRect(-2,-130,6,150); cxt.restore(); cxt.save(); cxt.translate(300,300); cxt.rotate(Math.PI/180 * s * 6); cxt.fillStyle = "crimson"; cxt.fillRect(-2,-130,4,150); cxt.restore(); } function move(){ cxt.clearRect(0,0,canvas.width,canvas.height); draw(); drawZhen(); window.requestAnimationFrame(move); } move(); setInterval(move,1000);
</script> </body> </html>
|