欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

JavaScript Canvas编写炫彩的网页时钟

时间:2021-07-06 10:10:38|栏目:JavaScript代码|点击:

本文实例为大家分享了JavaScript Canvas编写炫彩网页时钟的具体代码,供大家参考,具体内容如下

只是利用了Canvas制作的。

示意图如下:

<!DOCTYPE html>
<html>
<head>
 <meta http-equiv = "Content-Type" content = "text/html"; charsert = "utf-8" />
 <title> 网页时钟 </title>
</head>
<body>
 <h2> Web时钟 </h2>
 <canvas id = "hello" width = "400" height = "400"
 style = "border:1px solid black"> </canvas>
 <script languagetype = "text/javascript">
 var myCavas = document.getElementById('hello');
 var c = myCavas.getContext('2d');
 function clock() {
  c.clearRect(0, 0, 400, 400);
  //获取当前时间
  var data = new Date();
  //获取秒
  var sec = data.getSeconds();
  //获取分钟
  var min = data.getMinutes();
  //获取小时
  var hour = data.getHours();
  c.save();
  c.translate(200, 200);
  c.rotate(-Math.PI/2);
  //分针刻度线
  for (var i = 0; i < 60; i++) { //画60个刻度线
  c.beginPath();
  c.strokeStyle = "yellowgreen";
  c.lineWidth = 5;
  c.moveTo(117, 0);
  c.lineTo(120, 0);
  c.stroke();
  //每6deg画一个分钟刻度线
  c.rotate(Math.PI/30);
  c.closePath();
  }
  //时钟刻度线
  for (var i = 0; i < 12; i++) { //画60个刻度线
  c.beginPath();
  c.strokeStyle = "green";
  c.lineWidth = 8;
  c.moveTo(100, 0);
  c.lineTo(120, 0);
  c.stroke();
  //每6deg画一个分钟刻度线
  c.rotate(Math.PI/6);
  c.closePath();
  }
  //外表盘
  c.beginPath();
  c.strokeStyle = "pink";
  c.arc(0, 0, 145, 0, Math.PI*2);
  c.lineWidth = 12;
  c.stroke();
  c.closePath();
  //画时针
  hour = hour > 12 ? hour-12 : hour;
  //console.log(hour);
  c.beginPath();
  c.save();
  //设置旋转角度,参数是弧度,角度0-360 弧度角度*Math.PI/180
  c.rotate(Math.PI/6*hour + Math.PI/6*min/60 + Math.PI/6*sec/3600);
  c.strokeStyle = "yellowgreen";
  c.lineWidth = 4;
  c.moveTo(-20, 0);
  c.lineTo(50, 0);
  c.stroke();
  c.restore();
  c.closePath();
  //画分针
  //console.log(min);
  c.beginPath();
  c.save();
  c.rotate(Math.PI/30*min + Math.PI/30*sec/60);
  c.strokeStyle = "springgreen";
  c.lineWidth = 3;
  c.moveTo(-30, 0);
  c.lineTo(70, 0);
  c.stroke();
  c.restore();
  c.closePath();
  //画秒针
  c.beginPath();
  c.save();
  c.rotate(Math.PI/30*sec);
  c.strokeStyle = "red";
  c.lineWidth = 2;
  c.moveTo(-40, 0);
  c.lineTo(120, 0);
  c.stroke();
  c.restore();
  c.closePath();
  c.restore();
 }
 clock();
 setInterval(clock, 1000);
 </script>
</body>
</html>

更多JavaScript时钟特效点击查看:JavaScript时钟特效专题

上一篇:js加入收藏夹代码(兼容ie/ff/op)

栏    目:JavaScript代码

下一篇:纯JS实现AJAX局部刷新功能

本文标题:JavaScript Canvas编写炫彩的网页时钟

本文地址:http://www.codeinn.net/misctech/153590.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有