我在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/