用canvas画时钟

用canvas实现时钟每秒钟旋转一次效果,效果图如下

时钟2

对应代码:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>时钟</title>
  <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <style>
    div {
      text-align: center;
      margin-top: 50px;
    }
  </style>
</head>
<body>
  <header>
  </header>
  <div>
    <canvas id="clock" width="500" height="500"></canvas>
  </div>
  <script>
    var dom = document.getElementById('clock');
    var ctx = dom.getContext('2d');
    var width = ctx.canvas.width;
    var height = ctx.canvas.height;
    var r = width / 2;
    var rem = width / 200;
    function drawBackground() {
      ctx.save();
      ctx.translate(r, r);
      ctx.beginPath();
      ctx.stroke();
      var hourNumbers = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2];
      ctx.font = 18 * rem + 'px Arial';
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      hourNumbers.forEach(function (number, i) {
        var rad = 2 * Math.PI / 12 * i;
        var x = Math.cos(rad) * (r - 40 * rem);
        var y = Math.sin(rad) * (r - 40 * rem);
        ctx.fillText(number, x, y);
      });
      for (var i = 0; i < 60; i++) {
        var rad = 2 * Math.PI / 60 * i;
        var x = Math.cos(rad) * (r - 18 * rem);
        var y = Math.sin(rad) * (r - 18 * rem);
        ctx.beginPath();
        if (i % 5 === 0) {
          ctx.lineWidth = 4 * rem;
          ctx.lineCap = "round";
          ctx.moveTo(x, y);
          ctx.lineTo(Math.cos(rad) * (r - 26 * rem), Math.sin(rad) * (r - 26 * rem)); //画出这条线
          ctx.stroke();
          ctx.arc(x, y, 2 * rem, 0, 2 * Math.PI, false);
        } else {
          ctx.fillStyle = '#000';
          ctx.arc(x, y, 2 * rem, 0, 2 * Math.PI, false);
        }
        ctx.fill()
      }
    }
    //画时针
    function drawHour(hour, minute) {
      ctx.save(); //保存当前环境的状态
      ctx.beginPath(); //画出竖着的那个时针的线
      var rad = 2 * Math.PI / 12 * hour; //时针旋转的弧度  每个小时的弧度
      var mrad = 2 * Math.PI / 12 / 60 * minute; //分针旋转的弧度
      ctx.rotate(rad + mrad);
      ctx.lineWidth = 4 * rem; //定义时针线的宽度
      ctx.lineCap = "round"; //设置时针线端点为圆的
      ctx.moveTo(0, 10 * rem); //移动划线的原点到圆点下面一点 
      ctx.lineTo(0, -r / 2); //画出这条线
      ctx.stroke(); //最终画出这条时针线
      ctx.restore(); //返回之前保存过的路径状态和属性
    }
    //画分针		
    function drawMinute(minute) {
      ctx.save(); //保存当前环境的状态
      ctx.beginPath(); //画出竖着的那个时针的线
      var rad = 2 * Math.PI / 60 * minute ; //需要旋转的弧度
      ctx.rotate(rad);
      ctx.lineWidth = 3 * rem; //定义分针线的宽度
      // ctx.lineCap = "round"; //设置时针线端点为圆的
      ctx.moveTo(0, 10 * rem); //移动划线的原点到圆点下面一点 
      ctx.lineTo(0, -r + 40 * rem); //画出这条线
      // ctx.lineTo(3,)
      ctx.stroke(); //最终画出这条时针线
      ctx.restore(); //返回之前保存过的路径状态和属性
    }
    // 画秒针	每秒旋转
    function drawSecond(second) {
      ctx.save(); //保存当前环境的状态
      ctx.beginPath(); //画出竖着的那个时针的线
      ctx.fillStyle = '#663300';
      var rad = 2 * Math.PI / 60 * second ; //秒针需要旋转的弧度
      ctx.rotate(rad);
      ctx.moveTo(-2 * rem, 20 * rem); //移动划线的原点到圆点下面一点 
      ctx.lineTo(2 * rem, 20 * rem);
      ctx.lineTo(1, -r + 25 * rem); //画出这条线
      ctx.lineTo(-1, -r + 18 * rem); //画出这条线
      ctx.fill(); //最终画出这条时针线
      ctx.restore(); //返回之前保存过的路径状态和属性
    }  
    function draw() {
      ctx.clearRect(0, 0, width, height)
      var now = new Date();
      var hour = now.getHours();
      var minute = now.getMinutes();
      var second = now.getSeconds();
      var millisecond = now.getMilliseconds();
      console.log(hour,minute,second)
      drawBackground();
      drawHour(hour, minute);
      drawMinute(minute);
      drawSecond(second);
      // drawMillisecond(millisecond,second);
      // console.log(millisecond);
      ctx.restore();
    }
    draw();
    setInterval(draw, 50);
  </script>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Canvas是在HTML5中用于绘制图形的一种技术,可以用来创建各种动态效果,比如动态时钟。要绘制一个动态时钟,你可以按照以下步骤操作: 1. **获取Canvas元素**: 首先,在HTML中创建一个`<canvas>`标签,并给它一个id,如`<canvas id="myClock"></canvas>`。 2. **JavaScript处理**: 使用JavaScript获取canvas元素,并创建绘图上下文(context)。例如: ```javascript const canvas = document.getElementById('myClock'); const ctx = canvas.getContext('2d'); ``` 3. **设置定时器**: 创建一个`setInterval`函数,每隔一段时间更新一次时钟。这里通常会计算当前时间并将其转换为显示在时钟上的文本。 4. **绘制时钟**: - **背景圆**:使用`arc()`方法绘制圆形背景,表示时钟盘面。 - **指针**:为每个小时和分钟创建小矩形,表示时针和分针,然后根据时间位置移动它们。 ```javascript function drawTime() { // 获取当前时间 const now = new Date(); // 设置时钟盘面中心和大小 const centerX = canvas.width / 2; const centerY = canvas.height / 2; const radius = Math.min(centerX, centerY) * 0.8; // 清除上一次绘制 ctx.clearRect(0, 0, canvas.width, canvas.height); // 背景圆 ctx.beginPath(); ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); ctx.fillStyle = 'white'; ctx.fill(); // 小时和分钟指针 let hourAngle = ((now.getHours() % 12) + now.getMinutes() / 60) * (2 * Math.PI / 12); // 弧度 let minuteAngle = now.getMinutes() * (2 * Math.PI / 60); // 弧度 ctx.save(); // 保存当前状态 ctx.translate(centerX, centerY); ctx.rotate(hourAngle); // 转动到小时角度 ctx.fillRect(-radius * 0.7, -radius * 0.15, radius * 1.4, radius * 0.3); // 小时指针 ctx.restore(); ctx.rotate(minuteAngle); // 转回原点并旋转到分钟角度 ctx.fillRect(-radius * 0.9, -radius * 0.2, radius * 0.6, radius * 0.4); // 分钟指针 } // 开始定时器 setInterval(drawTime, 1000); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值