HTML5画时钟
2014-02-12 10:26
363 查看
<!doctype html> <html> <head></head> <body> <canvas id="clock" width="600" height="600" >浏览器不支持canvas标签</canvas> <script> //表盘,蓝色 //刻度:12个时刻度,60个分刻度 //指针:时针,分针,秒针(红色) //获取画布DOM(此时还不可以操作),一个页面中可能存在多个画布 var clock = document.getElementById('clock'); //设置绘图环境,新建一个2d绘图环境 var cxt = clock.getContext('2d'); function drawClock(){ //每次清除画布 cxt.clearRect(0,0,600,600); //获取时间对象,注意处理时间格式问题,12小时制 var now = new Date(); var hour = now.getHours(); var hourText = hour; var minute = now.getMinutes(); var second = now.getSeconds(); //alert(hour+'点'+minute+'分'+second+'秒'); //时针需获取浮点类型,加上对应的分钟数转化成的小时 hour = hour + minute/60; //将24小时进制转换成12小时进制 hour = hour>12?hour-12:hour; //画数字格式时间显示 //在矩形中设置文字 //画一个矩形,圈住空心字 它不属于路径 cxt.beginPath(); //重新设置画笔 cxt.lineWidth=2; cxt.strokeStyle="#000000"; cxt.rect(160,270,150,40); cxt.stroke(); cxt.closePath(); //实心 //字体样式 cxt.lineWidth=1; cxt.font="20px 黑体";//CSS Font属性,有顺序限制 cxt.fillText(hourText+'点'+minute+'分'+second+'秒',200,200); //空心 cxt.strokeText(hourText+'点'+minute+'分'+second+'秒',170,300); //表盘(蓝色) cxt.lineWidth = 10; cxt.strokeStyle = "BLUE"; cxt.beginPath(); cxt.arc(250,250,200,0,360,false); cxt.closePath(); cxt.stroke(); //刻度 //时刻度 for(var i = 0;i<12;i++){ cxt.save(); cxt.lineWidth = 7; cxt.strokeStyle = "BLACK" //设置0,0点 cxt.translate(250,250); //设置旋转角度:角度*Math.PI/180 cxt.rotate(i*30*Math.PI/180); cxt.beginPath(); cxt.moveTo(0,-190); cxt.lineTo(0,-170); cxt.closePath(); cxt.stroke(); cxt.restore(); } //分刻度 for(var i = 0;i<60;i++){ cxt.save(); cxt.lineWidth = 4; cxt.strokeStyle = "BLACK" //设置0,0点 cxt.translate(250,250); //设置旋转角度:角度*Math.PI/180 cxt.rotate(i*6*Math.PI/180); cxt.beginPath(); cxt.moveTo(0,-190); cxt.lineTo(0,-180); cxt.closePath(); cxt.stroke(); cxt.restore(); } //时针,默认指向3点 cxt.save(); cxt.lineWidth = 7; cxt.strokeStyle = "BLUE"; cxt.translate(250,250); cxt.rotate(hour*30*Math.PI/180); cxt.beginPath(); cxt.moveTo(0,-140); cxt.lineTo(0,10); cxt.closePath(); cxt.stroke(); cxt.stroke(); cxt.restore(); //分针,默认指向6点 cxt.save(); cxt.lineWidth = 4; cxt.strokeStyle = "BLACK"; cxt.translate(250,250); cxt.rotate(minute*6*Math.PI/180); cxt.beginPath(); cxt.moveTo(0,-160); cxt.lineTo(0,10); cxt.closePath(); cxt.stroke(); cxt.restore(); //秒针,默认指向10点-50秒(1秒对应6度) cxt.save(); cxt.lineWidth = 1; cxt.strokeStyle = "RED"; cxt.translate(250,250); cxt.rotate(second*6*Math.PI/180); cxt.beginPath(); cxt.moveTo(0,-170); cxt.lineTo(0,20); cxt.closePath(); cxt.stroke(); //画出交叉点 cxt.beginPath(); cxt.arc(0,0,5,0,360,false); //设置填充样式 cxt.fillStyle = "YELLOW"; cxt.fill(); //设置笔触样式 cxt.strokeStyle = "RED"; cxt.stroke(); cxt.closePath(); //画秒针前端小○点 cxt.beginPath(); cxt.arc(0,-160,5,0,360,false); //设置填充样式 cxt.fillStyle = "RED"; cxt.fill(); //设置笔触样式 cxt.strokeStyle = "YELLOW"; cxt.stroke(); cxt.closePath(); cxt.restore(); } //使用setInterval(代码,周期毫秒),让时钟动起来 //为了避免刚开始时页面出现空白,先画一次 drawClock(); setInterval(drawClock,1000); </script> </body> </html>
相关文章推荐
- HTML5中在客户端验证文件上传的大小
- html5在android中的使用问题及技巧解读
- 非html5实现js版弹球游戏示例代码
- html5 canvas js(数字时钟)实例代码
- js和html5实现手机端刮刮卡抽奖效果完美兼容android/IOS
- 通过javascript把图片转化为字符画
- HTML5附件拖拽上传drop & google.gears实现代码
- javascript中download关键字的方法名调用问题
- 《HTML+CSS3权威指南》笔记摘要 - 目录
- 关于“HTML5 的未来 - HTML5 还能走多远”一文的一些看法
- html5 App phonegapcn + eclipse + Andriod 安装配置
- HTML5 canvas实现一个简易画板
- [转] HTML5 Canvas Heart
- 用nodejs 改造移动版本的osc翻译频道
- jQuery+HTML5声音提示
- HTML5 之美:推荐9款优秀的 HTML5 音乐播放器
- 突袭HTML5之SVG 2D入门10-1
- 突袭HTML5之SVG 2D入门10-2
- 突袭HTML5之SVG 2D入门10-3