如何在HTML5画布中反转动画的方向

时间:2013-07-02 16:35:58

标签: javascript canvas html5-canvas

我很难理解HTML5动画的工作原理。我需要一些启发。

我想要做的是将矩形设置为画布底部的动画,然后返回到画布的顶部。似乎问题是我没有正确设置矩形的y位置。我注意到当我设置矩形的速度与当前速度不同时,它会以我想要的方式作出反应。这个矩形想要恢复,但它记住它是假设下降。因此,它试图决定做什么。

如何初始设置矩形的y位置,是否需要不断更新?

<!doctype html>
<html>
<head>
<title>canvas animation</title>
<style>
#animated {
    border: 1px solid black;  
}
</style>
</head>
<body>

<h1>canvas animation</h1>

<canvas id="animated" width="500" height="300"></canvas>

<script>
        var xLoc = 0;
        var yLoc = 0;
        var speed = 5;

        window.requestAnimFrame = (function(callback) {
            return window.requestAnimationFrame || 
            window.webkitRequestAnimationFrame || 
            window.mozRequestAnimationFrame || 
            window.oRequestAnimationFrame || 
            window.msRequestAnimationFrame ||
            function(callback) {
                window.setTimeout(callback, 1000 / 60);
            };
         })();

        function animateDown() {
            var canvas = document.getElementById("animated");
            var context = canvas.getContext("2d");

            context.clearRect(0, 0, canvas.width, canvas.height);           

            context.beginPath();
            context.rect(xLoc, yLoc, 300, 150); // yLoc-canvas.height = -300
            context.fillStyle = "rgb(247, 209, 23)";
            context.fill();

            yLoc += 4;

            if (yLoc > canvas.height - 150) {
                yLoc -= speed;
            } else if (yLoc < 0) {
                yLoc += speed;
            }

            requestAnimFrame(function() {
                animateDown();
            });
         }

        window.onload = function() {
            animateDown();
        };
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

问题在于你没有告诉它要扭转。你告诉它要退缩,它会陷入永无止境的循环中。

更改代码,让变量direction告诉它去哪里(上/下):

<script>
        var xLoc = 0;
        var yLoc = 0;
        var speed = 5;
        var direction = 1;   // Defaults to 'down'

        window.requestAnimFrame = (function(callback) {
            return window.requestAnimationFrame || 
            window.webkitRequestAnimationFrame || 
            window.mozRequestAnimationFrame || 
            window.oRequestAnimationFrame || 
            window.msRequestAnimationFrame ||
            function(callback) {
                window.setTimeout(callback, 1000 / 60);
            };
         })();

        function animateDown() {
            var canvas = document.getElementById("animated");
            var context = canvas.getContext("2d");

            context.clearRect(0, 0, canvas.width, canvas.height);           

            context.beginPath();
            context.rect(xLoc, yLoc, 300, 150); // yLoc-canvas.height = -300
            context.fillStyle = "rgb(247, 209, 23)";
            context.fill();

            yLoc += speed * direction;   // Increase by speed in the given direction

            if (yLoc > canvas.height - 150) {
                direction = -1;  // Move up again (decrease)
            } else if (yLoc < 0) {
                direction = 1;   // Move downwards
            }

            requestAnimFrame(function() {
                animateDown();
            });
         }

        window.onload = function() {
            animateDown();
        };
</script>