<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>html5时钟</title> </head> <body> <canvas id="clock" width="500" height="500" style="background-color:black;">你的浏览器不支持canvas</canvas> <script type="text/javascript"> var canvas = document.getElementById("clock"); var cxt = canvas.getContext("2d"); function drawClock() { var now = new Date(); var sec = now.getSeconds(); var min = now.getMinutes(); var hour = now.getHours(); hour > 12 ? hour - 12 : hour; hour += (min / 60); //先清空画布 cxt.clearRect(0, 0, canvas.width, canvas.height); //美女图片作为表盘背景 var img = new Image(); img.src = "http://t-1.tuzhan.com/ebd0af03090a/p-2/l/2013/11/30/17/515a5c953dc94172bc2c96953cf6a8e2.jpg"; cxt.drawImage(img, 0, 0); cxt.strokeStyle = "#00FFFF"; cxt.lineWidth = 10; cxt.beginPath(); cxt.arc(250, 250, 200, 0, 360); cxt.stroke(); cxt.closePath(); //时刻度 for (var i = 0; i < 12; i++) { cxt.save();//保存当前状态 cxt.lineWidth = 7; cxt.strokeStyle = "#FFFF00"; //设置原点 cxt.translate(250, 250); //设置旋转角度 cxt.rotate(30 * i * Math.PI / 180);//弧度 角度*Math.PI/180 cxt.beginPath(); cxt.moveTo(0, -175); cxt.lineTo(0, -195); cxt.stroke(); cxt.closePath(); cxt.restore();//把原来状态恢复回来 } //分刻度 for (var i = 0; i < 60; i++) { cxt.save(); cxt.lineWidth = 5; cxt.strokeStyle = "#FFFF00"; cxt.translate(250, 250); cxt.rotate(i * 6 * Math.PI / 180); cxt.beginPath(); cxt.moveTo(0, -185); cxt.lineTo(0, -195); cxt.stroke(); cxt.closePath(); cxt.restore(); } cxt.save(); cxt.lineWidth = 7; cxt.strokeStyle = "#00FFFF"; cxt.translate(250, 250); cxt.rotate(hour * 30 * Math.PI / 180);//每小时旋转30度 cxt.beginPath(); cxt.moveTo(0, -130); cxt.lineTo(0, 10); cxt.stroke(); cxt.closePath(); cxt.restore(); cxt.save(); cxt.lineWidth = 5; cxt.strokeStyle = "#FFFF00"; cxt.translate(250, 250); cxt.rotate(min * 6 * Math.PI / 180);//每分钟旋转6度 cxt.beginPath(); cxt.moveTo(0, -150); cxt.lineTo(0, 10); cxt.stroke(); cxt.closePath(); cxt.restore(); cxt.save(); cxt.lineWidth = 3; cxt.strokeStyle = "#FF0000"; cxt.translate(250, 250); cxt.rotate(sec * 6 * Math.PI / 180);//每秒旋转6度 cxt.beginPath(); cxt.moveTo(0, -170); cxt.lineTo(0, 10); cxt.stroke(); cxt.closePath(); //美化表盘,画中间的小圆 cxt.beginPath(); cxt.arc(0, 0, 7, 0, 360); cxt.fillStyle = "#FFFF00"; cxt.fill(); cxt.strokeStyle = "#FF0000"; cxt.stroke(); cxt.closePath(); //秒针上的小圆 cxt.beginPath(); cxt.arc(0, -140, 7, 0, 360); cxt.fillStyle = "#FFFF00"; cxt.fill(); cxt.stroke(); cxt.closePath(); cxt.restore(); //显示时间 cxt.font = "18px 微软雅黑"; cxt.lineWidth = 2; cxt.fillStyle = "#0000FF"; hour=now.getHours(); var str = hour > 10 ? hour : ("0" + hour) + ":" + (min > 10 ? min : ("0" + min)) cxt.fillText(str, 225, 380); //中国制造 cxt.font = "12px 宋体"; cxt.lineWidth = 1; cxt.fillText("Made In China", 210, 400); } drawClock(); setInterval(drawClock, 1000); </script> </body> </html>
详细解决方案
html5+js钟表效果
热度:489 发布时间:2014-01-03 00:30:09.0
相关解决方案
- 求“HTML5”实现的图表/报表插件解决办法
- 求“HTML5”兑现的图表/报表插件
- HTML5 3D图,该怎么解决
- html5+.net联合开发的有关问题
- HTML5 离线效能详解 - 构建 Offline Web Application
- HTML5,该如何解决
- HTML5+css3的学习网站
- HTML5/CSS3培训课程
- HTML5 Canvas双缓存范例
- html5 在线制造ppt
- Html5 Canvas的充分运用:适用示例
- 学习:HTML5 游戏《银河系的掠取》图片加载进度条
- HTML5 Canvas简略动画:圆周运动
- HTML5 aria- and role
- 小编者 HTML5 的 placeholder属性
- JavaScript模拟QQ签字(HTML5 contenteditable属性)
- 8款独具一格的 jQuery/HTML5 应用插件
- HTML5 Canvas之猜数目字游戏
- 您应该知道的10个奇特的 HTML5 单页网站
- html5 上传适用小例子
- HTML5+CSS3课程1
- HTML5+CSS3课程2
- HTML5 Canvas 流程图作图
- HTML5 Canvas遇到的几个小问题
- 图表作图 HTML5
- HTML5 兑现拖拽
- HTML5 App实战(六):拼图游戏
- HTML5 window/iframe跨域传接消息 API
- HTML5 服务器发送事件(Server-Sent Events)引见
- HTML5 Canvas中兑现文字链接