Smooth javascript animation - Stack Overflow

Here is some code:<!DOCTYPE HTML PUBLIC "-W3CDTD HTML 4.01EN" ".dtd">&

Here is some code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ".dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style>
body { margin:0; padding:0; }
#a {
    position:absolute;
    background:#0FF;
    left:0;
    height:300px;
    width:120px;
}
input, #a {
    margin:10px;
}
</style>
<script>
function foo() {
    box = document.getElementById('a');
    var putedStyle = box.currentStyle || window.getComputedStyle(box, null);
    box.style.left = parseInt(putedStyle.left) + 10 + 'px';
    setTimeout("foo()",20);
}
</script>
</head>

<body>
<input type="button" value="RUN, FORREST, RUN!" onClick="setTimeout('foo()',20)">
<div id="a"></div>
</body>
</html>

As you can see, it animates DIV at page, but animation isn't clear and smooth — border of DIV actually deforming. Somebody know how i can make it work correctly?

Here is some code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style>
body { margin:0; padding:0; }
#a {
    position:absolute;
    background:#0FF;
    left:0;
    height:300px;
    width:120px;
}
input, #a {
    margin:10px;
}
</style>
<script>
function foo() {
    box = document.getElementById('a');
    var putedStyle = box.currentStyle || window.getComputedStyle(box, null);
    box.style.left = parseInt(putedStyle.left) + 10 + 'px';
    setTimeout("foo()",20);
}
</script>
</head>

<body>
<input type="button" value="RUN, FORREST, RUN!" onClick="setTimeout('foo()',20)">
<div id="a"></div>
</body>
</html>

As you can see, it animates DIV at page, but animation isn't clear and smooth — border of DIV actually deforming. Somebody know how i can make it work correctly?

Share Improve this question asked Aug 24, 2009 at 12:54 everm1ndeverm1nd 2
  • Could you please define "deformed" ... it behaves as expected on my machine. – JustLoren Commented Aug 24, 2009 at 12:58
  • Looks like "VSync turned off" in 3D Games – everm1nd Commented Aug 24, 2009 at 14:19
Add a ment  | 

4 Answers 4

Reset to default 9

Ditto JustLoren: it works fine on my machine. I'm not sure what you mean by the border ‘deforming’... maybe you're talking about tearing? If so, I'm afraid there is nothing you can do about it.

The traditional solution to tearing is to wait for vsync to draw your next frame, but that ability is not available in JavaScript. No framework can fix it. (Framework fans: please stop suggesting “Use my_favourite_framework! It solves all problems!” to JavaScript questions you don't understand.)

As mck89 suggests, you can certainly make the animation smoother (which can reduce the impact of tearing too) by drawing more frames, at the expense of taking more CPU power to perform. You might also prefer to keep a variable to store your x value, rather than parsing it from the currentStyle every time. It would be simpler and more widely supported by browsers.

ETA re ment: There's not a concrete minimum timeout in JS (I can get it down to 1ms sometimes), but how many fps you can get out of an animation is highly dependent on the browser and the power of the machine, which is why generally it's a good idea to base position/frame on the amount of time that has elapsed since the start of the animation (using new Date().getTime()) rather than moving/changing a fixed amount each frame.

In any case, about the fastest you can practically go is using an interval of 16ms, which corresponds to one frame on a 60Hz monitor (the usual flatscreen default).

You should increment the left coordinate by 1 px and set a lower time for the interval.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style>
body { margin:0; padding:0; }
#a {
    position:absolute;
    background:#0FF;
    left:0;
    height:300px;
    width:120px;
}
input, #a {
    margin:10px;
}
</style>
<script>
function foo() {
    box = document.getElementById('a');
    var putedStyle = box.currentStyle || window.getComputedStyle(box, null);
    box.style.left = parseInt(putedStyle.left) + 1 + 'px';
    setTimeout("foo()",1);
}
</script>
</head>

<body>
<input type="button" value="RUN, FORREST, RUN!" onClick="setTimeout('foo()',20)">
<div id="a"></div>
</body>
</html>

JQuery and YUI and almost every other js library provides animation utility, perhaps you should look into those.

In my experience, mootools (http://mootools) provides the smoothest animation.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744944581a4602534.html

相关推荐

  • Smooth javascript animation - Stack Overflow

    Here is some code:<!DOCTYPE HTML PUBLIC "-W3CDTD HTML 4.01EN" ".dtd">&

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信