0

アニメーションにキャンバスと webkitrequestAnimation フレームを使用しています。ただし、アニメーションは瞬時に表示され、スムーズではありません。非常に高速で、アニメーションが表示されません。アニメーションをスムーズにするにはどうすればよいですか?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Wave 2</title>

</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>

<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
angle = 0,
range = 50,
centerY = canvas.height / 2,
xspeed = 1,
yspeed = 0.05,
xpos = 0,
ypos = centerY;
context.lineWidth = 2;
(function drawFrame () {
window.webkitrequestAnimationFrame(drawFrame, canvas);

context.beginPath();
context.moveTo(xpos, ypos);
//Calculate the new position.
xpos += xspeed;
angle += yspeed;
ypos = centerY + Math.sin(angle) * range;
context.lineTo(xpos, ypos);
context.stroke();
}());
};
</script>
</body>
</html>
4

1 に答える 1

1

24行目の構文エラーを除けば、これは期待どおりに機能します。

window.webkitrequestAnimationFrame(drawFrame, canvas);

window.webkitRequestAnimationFrame(drawFrame, canvas);

このページの回答を確認してください: requestAnimationFrame の使用方法

Webkit で構築されたものだけでなく、互換性のあるすべての Web ブラウザーで機能するリクエスト アニメーション関数を作成する方法について説明します。Safari または Chrome 以外のブラウザを使用していた場合、異常な動作の原因となる可能性があります。

createjs.comは HTML5 キャンバスとアニメーションを操作するための優れた API を提供しているため、ここではcreatejs.comを推奨したいと思います。デモとソースをチェックしてください!

于 2012-05-27T07:03:45.547 に答える