您好,欢迎访问一九零五行业门户网

使用纯HTML5编写一款网页上的时钟的代码分享_html5教程技巧

你需要知道的:
canvas标签只是图形容器,您必须使用脚本来绘制图形。默认大小:宽300px,高150px;
getcontext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属性。——获取上下文对象。
getcontext(2d) 对象属性和方法,可用于在画布上绘制文本、线条、矩形、圆形等等。
fillrect(l,t,w,h):默认颜色是黑色 strokerect(l,t,w,h):带边框的方块。默认一像素黑色边框
setinterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
beginpath():定义开始绘制路径, 它把当前的点设置为 (0,0)。 当一个画布的环境第一次创建,beginpath()
方法会被显式地调用。
closepath():结束绘制路径(将起点与终点进行连接)
绘制圆形:
arc( x,y,半径,起始弧度,结束弧度,旋转方向)
x,y:起始位置
弧度与角度的关系:弧度=角度*math.pi/180
旋转方向:顺时针(默认:false,逆时针:true)
代码:
xml/html code复制内容到剪贴板
nbsp;html>   html lang=en-us>   head>           meta charset=utf-8>           title>title>           script>                   window.onload = function(){                            var oc = document.getelementbyid('ch1');                            var ogc = oc.getcontext('2d');                               function drawclock(){                                    var x = 200;   //指定坐标                                    var y = 200;                                    var r = 150;  //指定钟表半径                                       ogc.clearrect(0,0,oc.width,oc.height);//清空画布                                       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 osenvalue = (-90 + osen*6)*math.pi/180;                                       ogc.beginpath();//开始                                       for(var i=0;i60;i++){         //i为60,代表着时钟的60个小刻度                                            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;i12;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/
其它类似信息

推荐信息