0

我很难理解 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>
4

1 回答 1

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>
于 2013-07-02T16:41:22.450 回答