在下面的代码中,乘以0.002是否有任何特殊用途?为什么在这行代码中乘以0.002:new Date()。getTime()* 0.002;
var time = new Date().getTime() * 0.002;
此代码摘录是从here拍摄。我提供以下的整个代码也:
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000/60);
};
})();
var canvas, context;
init();
animate();
function init() {
canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 256;
context = canvas.getContext('2d');
document.body.appendChild(canvas);
}
function animate() {
requestAnimFrame(animate);
draw();
}
function draw() {
var time = new Date().getTime() * 0.002;
var x = Math.sin(time) * 96 + 128;
var y = Math.cos(time * 0.9) * 96 + 128;
context.fillStyle = 'rgb(245,245,245)';
context.fillRect(0, 0, 255, 255);
context.fillStyle = 'rgb(255,0,0)';
context.beginPath();
context.arc(x, y, 10, 0, Math.PI * 2, true);
context.closePath();
context.fill();
}
我也跟着你的链接,并看不出代码随处可见但我会继续说,是的,有一个特殊的目的(只是0.002乘以不是偶然发生的事情)。 – nnnnnn
转到页面上的JSFiddle窗口,然后单击JavaScript。 –
我想它会得到自1970年1月1日以来的半秒数。不能在你的链接中找到代码。 –