好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用纯HTML5编写一款网页上的时钟的代码分享

你需要 知道 的:

canvas标签只是图形容器,您必须使用脚本来 绘制 图形。默认大小: 宽 300px,高150px;

getContext() 方法可返回一个对象,该对象提供了用于在 画布 上绘图的方法和属性。——获取上下文对象。
getContext("2d") 对象属性和方法,可用于在画布上绘制文本、线条、矩形、 圆 形 等等 。

fillRect(l,t,w,h):默认颜色是黑色 stroke Rect(l,t,w,h):带边框的方块。默认一像素黑色边框

setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。

be gin Path():定义 开始 绘制路径, 它把当前的点设置为 (0,0)。 当一个画布的环境第一次创建,beginPath()
方法会被显式地调用。
closePath():结束绘制路径(将起点与终点进行连接)


&nbs p; 绘制圆形:
arc( x,y, 半 径,起始弧度,结束弧度,旋转 方向 )
x,y:起始位置
弧度与角度的关系:弧度=角度*Math.PI/180
旋转方向:顺时针(默认:false,逆时针:true)

代码:

XM L/HT ML Code 复制内容到剪贴板

<!DOCTY PE  HTML >    < html   lang = "en-US" >    < head >             < meta   charset = "UTF-8" >             < t IT le > </ title >             < script >                     window.onload  =  function (){                             VAR   oC  =  document .getElementById('c h1 ');                            var  oGC  =  oC .getContext('2d');                               function draw clock (){                                    var  x  =  200 ;   //指定坐标                                    var  y  =  200 ;                                    var  r  =  150 ;  //指定钟表半径                                       oGC.clearRect(0,0,oC.width,oC.h ei ght);//清空画布                                       var  oDate  =  new  Date();      //创建日期对象                                    var  oHours  =  oDate .getHours();//获取时间                                    var  oMin  =  oDate .getMinutes();                                    var  oSen  =  oDate .getSeconds();                                       var  oHoursValue  = (-90 + oHours*30 + oMin/2)*Math.PI/180; //设置时针的值                                    var  oMinValue  = (-90 + oMin*6)*Math.PI/180;                                    var  oS env alue  = (-90 + oSen*6)*Math.PI/180;                                       oGC.beginPath();//开始                                       for(var  i = 0 ;i < 60 ; i++ ){         //i为60,代表着时钟的6 0个 小刻度                                            oGC.moveTo(x,y);                                            oGC.arc(x,y,r,6*i*Math.PI/180,6*(i+1)*Math.PI/180,false); //循环从6度到12度                                    }                                    oGC.closePath();                                    oGC.stroke();                                        oGC.fillStyle  = 'white' ; //覆盖住小刻度的黑色线                                    oGC.beginPath();                                    oGC.moveTo(x,y);                                    oGC.arc(x,y,r*19/20,0, 360 *(i+1)*Math.PI/180,false);                                       oGC.closePath();//结束                                    oGC.fill();                                        oGC.lineWidth  =  3 ; //设置时钟圆盘大刻度的粗细值                                    oGC.beginPath();  //开始画大的时钟刻度                                       for( i = 0 ;i < 12 ;i++){              //i为12,代表着时钟刻度的12大格                                            oGC.moveTo(x,y);                                            oGC.arc(x,y,r,30*i*Math.PI/180,30*(i+1)*Math.PI/180,false); // 间隔为30度,弧度=角度*Math.PI/180                                    }                                    oGC.closePath();                                    oGC.stroke();                                        oGC.fillStyle  = 'white' ; //覆盖住大刻度的黑色线                                    oGC.beginPath();                                    oGC.moveTo(x,y);                                    oGC.arc(x,y,r*18/20,360*(i+1)*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*14/20,oMinValue,oMinValue,false);//设置分针大小和弧度                                    oGC.closePath();                                    oGC.stroke();                                        oGC.lineWidth  =  1 ;//设置秒针宽度                                    oGC.beginPath();//开始绘制秒针                                    oGC.moveTo(x,y);                                       oGC.arc(x,y,r*19/20,oSenValue,oSenValue,false);//设置秒针大小和弧度                                    oGC.closePath();                                    oGC.stroke();                            }                            setInterval(drawClock,1000);//设置定时器,让时钟运转起来                                    drawClock();                    };             </ script >    </ head >    < body >             < canvas   id  =  "ch1"   width  =  "400px"   height  =  "400px" > </ canvas >    </ body >    </ html >   

点击下方result查看演示:
http://jsfiddle.net/eh02450b/2/

总结

以上是 为你收集整理的 使用纯HTML5编写一款网页上的时钟的代码分享 全部内容,希望文章能够帮你解决 使用纯HTML5编写一款网页上的时钟的代码分享 所遇到的问题。

如果觉得 网站内容还不错, 推荐好友。

查看更多关于使用纯HTML5编写一款网页上的时钟的代码分享的详细内容...

  阅读:22次