javascript - 如何在js中将时间转换为deg或rad?

标签 javascript jquery

我在js上制作时钟。如何根据时间旋转时针?我需要将时间转换为度或拉德?我现在的代码:

$(document).ready(function(){

    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');

    var img = new Image();
    var bee = new Image();
    var i = 40;
    img.src = "images/clock.jpg";


    img.onload = function(){
        bee.src = "images/bee.jpg";
        ctx.drawImage(img, 0, 0);
        ctx.drawImage(bee, 95, 90);

        setInterval(function(){
            i = i+1;
            ctx.save(); //saves the state of canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height); //clear the canvas
            ctx.drawImage(img, 0, 0);
            ctx.translate(canvas.width/2,canvas.height/2);
            ctx.rotate(225*Math.PI/180);
            ctx.drawImage(bee, -85, -90);

            ctx.restore(); 

        }, 100);


    }
});

我的时钟屏幕: Imgur

最佳答案

数学...

对于分针

  • 60 分钟为一整轮
  • 完整旋转为 360 度或 2*pi
  • 60 分钟的任何分数都是 360 度或 2*pi 的分数
  • 即23 分钟为 (23/60)*2*pi 弧度或 (23/60)*360 度
  • 如果您想要连续移动的分针,请将秒添加为分钟的 1/60

对于时针

  • 12 小时是一个完整的循环
  • 通过算出每分钟是一小时的 1/60 来添加分钟
  • 使用与上述相同的规则转换为度数或弧度

关于javascript - 如何在js中将时间转换为deg或rad?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825646/

相关文章:

javascript - 以编程方式创建 Bootstrap 选项卡

javascript - 如何向 "The Wall"moo 工具插件 JSON 数据提供数据?

Javascript理解回调函数和执行

javascript - XMLHttpRequest 多次启动但未启动 onreadystatechange 函数

javascript - 不要在点击时删除类(class)

php - 来自 mysql/JSON 的 twitter bootstrap typeahead 源代码

jquery - 在 div 中使用唯一的 id 在 jQuery 中显示/隐藏

javascript - 为什么这个函数返回 NaN?

jQuery:如果我的元素是 $(this),如何使用 :checked

jquery - 日历 td 的自定义背景颜色