<!doctype> <html> <head> <meta charset="utf-8" /> <style> body{background-color:Blue;} #showCanvas{background-color:white;} </style> <script type="text/javascript" > window.onload = function () { var canvasElt = document.getElementById("showCanvas"); function clockdrow() { if (canvasElt && canvasElt.getContext) { var canvasContext = canvasElt.getContext("2d"); //定义圆心和半径 var x = 200; var y = 200; var r = 150; //获取时间 var oDate = new Date(); var oHour = oDate.getHours(); var oMinute = oDate.getMinutes(); var oSecond = oDate.getSeconds(); //画表盘上的分钟刻度 canvasContext.clearRect(0, 0, canvasContext.width, canvasContext.height) canvasContext.beginPath(); for (var i = 0; i < 60; i++) { canvasContext.moveTo(x, y); canvasContext.arc(x, y, r, 6 * i * Math.PI / 180, 6 * (i + 1) * Math.PI / 180, false); } canvasContext.closePath(); canvasContext.stroke(); canvasContext.beginPath(); canvasContext.fillStyle = "white"; for (var i = 0; i < 60; i++) { canvasContext.moveTo(x, y); canvasContext.arc(x, y, r * 19 / 20, 6 * i * Math.PI / 180, 6 * (i + 1) * Math.PI / 180, false); } canvasContext.closePath(); canvasContext.fill(); //画时针刻度 canvasContext.beginPath(); canvasContext.lineWidth = 3; for (var i = 0; i < 12; i++) { canvasContext.moveTo(x, y); canvasContext.arc(x, y, r, 30 * i * Math.PI / 180, 30 * (i + 1) * Math.PI / 180, false); } canvasContext.closePath(); canvasContext.stroke(); canvasContext.beginPath(); canvasContext.fillStyle = "white"; for (var i = 0; i < 60; i++) { canvasContext.moveTo(x, y); canvasContext.arc(x, y, r * 18 / 20, 6 * i * Math.PI / 180, 6 * (i + 1) * Math.PI / 180, false); } canvasContext.closePath(); canvasContext.fill(); //时针所在位置 canvasContext.beginPath(); canvasContext.lineWidth = 4; canvasContext.moveTo(x, y); canvasContext.arc(x, y, r * 10 / 20, (-90 + oHour * 30 + oMinute / 2) * Math.PI / 180, (-90 + oHour * 30 + oMinute / 2) * Math.PI / 180, false); canvasContext.closePath(); canvasContext.stroke(); //分针所在位置 canvasContext.beginPath(); canvasContext.lineWidth = 2; canvasContext.moveTo(x, y); canvasContext.arc(x, y, r * 14 / 20, (-90 + oMinute * 6) * Math.PI / 180, (-90 + oMinute * 6) * Math.PI / 180, false); canvasContext.closePath(); canvasContext.stroke(); //秒针所在位置 canvasContext.beginPath(); canvasContext.moveTo(x, y); canvasContext.arc(x, y, r * 17 / 20, (-90 + oSecond * 6) * Math.PI / 180, (-90 + oSecond * 6) * Math.PI / 180, false); canvasContext.closePath(); canvasContext.stroke(); } else { alert("您的浏览器器不支持canvas,请升级浏览器!"); } } //让表动起来 setInterval(clockdrow, 1000) clockdrow(); } </script> </head> <body> <canvas id="showCanvas" width="400px" height="400px" style="border:1px dotted"></canvas> </body> </html>
详细解决方案
Html5学习-canvas编纂简单钟表
热度:125 发布时间:2012-11-10 10:48:51.0
相关解决方案
- 关于j2me Canvas 的sizeChange()步骤的陷阱
- canvas 文字居中显示有关问题
- 高手帮看看这程序,Canvas 里如何把内部的类分出来 Canvas里东西太多了,类类之间我不太会调用
- HTML5 3D图,该怎么解决
- html5+.net联合开发的有关问题
- HTML5 离线效能详解 - 构建 Offline Web Application
- HTML5,该如何解决
- HTML5/CSS3培训课程
- html5 上传适用小例子
- HTML5+CSS3课程2
- HTML5 Canvas实战――HTML5 Canvas时间成效
- HTML5-audio跟video API
- HTML5-札记
- html5 小技能
- html5 div呈圆形 扇形显示的式样
- HTML5+CSS3制作可自动获得焦点和支持语音输入的超酷搜索框
- HTML5+CSS3制造可自动获得焦点和支持语音输入的超酷搜索框
- html5 chm 相助文档
- html5 输入框默许提示
- html5 本土存储 留言板
- html5 初探二 表单
- html5 input的type属性起动数字输入法
- html5-canvas篇
- HTML5-话音搜索框
- html5 placeholder 属性 并检测浏览器是不是支持这个属性
- HTML5 装置访问入门
- What does idl attribute mean in the W3C html5 standard document
- HTML5 入门稿件目录汇总
- html5 svg 中元素点击事件平添
- html5 canvas 画图形,改变填充颜色解决思路