Home > Web Front-end > H5 Tutorial > Use canvas to draw a cool clock_html5 tutorial skills

Use canvas to draw a cool clock_html5 tutorial skills

WBOY
Release: 2016-05-16 15:47:05
Original
1487 people have browsed it

First the rendering:


Copy code
The code is as follows:





canvas clock





If you see this text, it means your browser is weak!
<script><br>window.onload=draw;<br>function draw() {<br>var canvas=document.getElementById('canvas');<br>var context= canvas.getContext('2d');<br>context.save(); ////////////////////////////////// ///Save<br>context.translate(200,200);<br>var deg=2*Math.PI/12; <br>/////////////////// /////////////////////////////////Dial<br>context.save(); <br>context.beginPath(); <br>for(var i=0;i<13;i ){<br />var x=Math.sin(i*deg);<br />var y=-Math.cos(i*deg);<br />context.lineTo(x*150,y*150); <br />}<br />var c=context.createRadialGradient(0,0,0,0,0,130);<br />c.addColorStop(0,"# 22f");<br />c.addColorStop(1,"#0ef")<br />context.fillStyle=c;<br />context.fill();<br />context.closePath(); <br />context. restore(); <br />////////////////////////////////////////////// ///////Number<br />context.save();<br />context.beginPath();<br />for(var i=1;i<13;i ){<br />var x1=Math .sin(i*deg);<br />var y1=-Math.cos(i*deg);<br />context.fillStyle="#fff";<br />context.font="bold 20px Calibri";<br />context.textAlign='center';<br />context.textBaseline='middle';<br />context.fillText(i,x1*120,y1*120); <br />}<br />context.closePath( ); <br />context.restore(); <br />////////////////////////////////////// ///////////////Large scale<br />context.save();<br />context.beginPath(); <br />for(var i=0;i<12;i ) {<br />var x2=Math.sin(i*deg);<br />var y2=-Math.cos(i*deg);<br />context.moveTo(x2*148,y2*148);<br />context.lineTo(x2*135,y2*135); <br />} <br />context.strokeStyle='#fff';<br />context.lineWidth=4;<br />context.stroke();<br />context.closePath();<br />context.restore(); <br />//////////////////////////////// ////////////////////Small scale<br />context.save();<br />var deg1=2*Math.PI/60;<br />context.beginPath (); <br />for(var i=0;i<60;i ){<br />var x2=Math.sin(i*deg1);<br />var y2=-Math.cos(i*deg1) ;<br />context.moveTo(x2*146,y2*146);<br />context.lineTo(x2*140,y2*140); <br />} <br />context.strokeStyle='#fff';<br />context.lineWidth=2;<br />context.stroke();<br />context.closePath(); <br />context.restore(); <br />//////////// /////////////////////////////////////////Text<br />context.save();<br />context.strokeStyle="#fff";<br />context.font=' 34px sans-serif';<br />context.textAlign='center';<br />context.textBaseline='middle';<br /> context.strokeText('canvas',0,65); <br />context.restore(); <br />////////////////////////// /////////////////////////new Date<br />var time=new Date();<br />var h=(time.getHours() )*2*Math.PI/12;<br />var m=time.getMinutes()*2*Math.PI/60;<br />var s=time.getSeconds()*2*Math.PI/60; </p> <p>///////////////////////////////////////////////// //Hour hand<br>context.save();<br>context.rotate( h m/12 s/720) ;<br>context.beginPath();<br>context.moveTo(0,6);<br>context.lineTo(0,-85);<br>context.strokeStyle="#fff";<br>context.lineWidth=6;<br>context.stroke();<br>context.closePath(); <br>context.restore();<br>/////////////////////////////////////// ////////Minute hand<br>context.save();<br>context.rotate( m s/60 ) ;<br>context.beginPath();<br>context.moveTo(0,8) ;<br>context.lineTo(0,-105);<br>context.strokeStyle="#fff";<br>context.lineWidth=4;<br>context.stroke();<br>context.closePath ();<br>context.restore();<br>///////////////////////////////////// //////////Second hand<br>context.save();<br>context.rotate( s ) ;<br>context.beginPath();<br>context.moveTo(0,10) ;<br>context.lineTo(0,-120);<br>context.strokeStyle="#fff";<br>context.lineWidth=2;<br>context.stroke();<br>context.closePath ();<br>context.restore(); <br>context.restore();////////////////////////////// Stack out<br>setTimeout(draw, 1000);//////////////////////////////Timer</p> <p>}</p> <p></script>


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template