这行代码为什么要乘以0.002:new Date().getTime() * 0.002;

发布于 2024-12-18 14:29:56 字数 1373 浏览 1 评论 0原文

下面的代码中乘以0.002有什么特殊用途吗?

var time = new Date().getTime() * 0.002;

此代码摘录自此处。我还提供了下面的完整代码:

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();
}

Is there any special purpose for multiplying by 0.002 in the following code?

var time = new Date().getTime() * 0.002;

This code excerpt was taken from here. I have provided the entire code below also:

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦回梦里 2024-12-25 14:29:56

该代码使用 Math.sin( time ) * 96 + 128 作为 x 坐标,使用 Math.cos( time * 0.9 ) * 96 + 128 作为 y 坐标协调。如果 time 是毫秒数(如 new Date().getTime() 所示),那么 x 坐标和 y 坐标都会随着每个连续的时间而剧烈波动呼叫,这个点似乎不会“移动”,而是“任意跳跃”——每秒六十次,速度比眼睛跟踪它的速度快。将毫秒数乘以 0.002 会导致点的 x 和 y 坐标以更平滑的方式振荡,其方式(在人眼看来)就像运动一样。

The code uses Math.sin( time ) * 96 + 128 as an x-coordinate, and Math.cos( time * 0.9 ) * 96 + 128 as a y-coordinate. If time were a number of milliseconds (as new Date().getTime() is), then the x-coordinate and y-coordinate would both vacillate wildly with each successive call, and the dot would not seem to "move", but rather "jump arbitrarily" — sixty times a second, faster than the eye can track it. Multiplying the number of milliseconds by 0.002 causes the x- and y-coordinates of the dot to oscillate in a much smoother fashion, in a way that looks (to the human eye) like motion.

我很OK 2024-12-25 14:29:56
var time = new Date().getTime() * 0.002;
var x = Math.sin( time ) * 96 + 128;
var y = Math.cos( time * 0.9 ) * 96 + 128;

getTime 方法返回的值是自 1970 年 1 月 1 日以来的毫秒数。该值用于计算圆的下一个 x 和 y 坐标。

var time = new Date().getTime() * 0.002;
var x = Math.sin( time ) * 96 + 128;
var y = Math.cos( time * 0.9 ) * 96 + 128;

The value returned by the getTime method is the number of milliseconds since 1 January 1970. That value is used to calculate the next x and y co-ordinates for the circle.

怂人 2024-12-25 14:29:56

该值控制绘制圆圈的位置(作为当前时间的一部分)。数字越小,动画显示越慢(并且更清晰) - 对于较大的数字,反之亦然。

您可以通过单击 jsFiddle 小部件右上角的加号图标来玩该数字 - 这将带您进入 jsFiddle 站点,您可以在其中编辑和运行 javascript。

注意 - 看起来有问题的脚本与 IE 9 不兼容 - 在 FF 中工作正常,但尚未测试任何其他浏览器...

The value controls the location where the circle is drawn as a fraction of the current time. The smaller the number the slower the animation appears (and more crisp) - the inverse is true for larger numbers.

You can play with the number by clicking the plus icon in the top right corner of the jsFiddle widget - this will take you to the jsFiddle site where you can edit and run the javascript.

Note - It looks like the script in question is not compatible with IE 9 - works fine for me in FF but haven't tested any other browsers...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文