简体   繁体   中英

HTML5 Canvas Image doesn't move

I have a problem. I'm using HTML5 and Javascript to draw a box onto a canvas. This box is supposed to move up and down, but after it gets rendered for the first time, it doesn't want to re-render at the new position. This is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sl" lang="sl">
<head>
    <title>Spletna stran Portfolio - Domaca stran</title>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />

    <link rel="stylesheet" href="css/style.css" type="text/css" media="all" />      

    <script>
    var isMoving = true;

    var gradienty = 0;
    var gradientheight = 100;
    var gradientyVel = 1;

    var gradientImage = new Image();
    gradientImage.src = "grafika/gradient.gif";

    function main() {
        var c = document.getElementById("menuCanvas");
        var ctx = c.getContext("2d");
        ctx.drawImage(gradientImage, 0, gradienty);
        if(isMoving == true) {
            gradienty += gradientyVel;
            if((gradienty+gradientheight)==c,height || gradienty<=0) {
                gradientyVel *= -1;
            }
        }
    }

    function mainLoop() {
        setInterval(main(), 10);
    }

    </script>

</head>
<body onload="mainLoop()">
<canvas id="menuCanvas" width="195" height="400"></canvas>
</body>
</html>

I have no clue why the box isn't moving. It's like after I draw it for the first time it won't draw again the next time. Also, I use mainLoop() function as my main loop. Every 10 miliseconds I basically call the main() function, which does the drawing and the logic. Any help would be much appreciated. :)

A few coding problems:

You need to give your image time to load, then start the animation

var gradientImage = new Image();
gradientImage.onload=function(){
    setInterval(main, 20);
}
gradientImage.src = "house16x16.png";

setInterval takes in a function pointer (no parentheses):

setInterval will fire only as fast as 16ms, so your 10ms value is too small

    setInterval(main, 20);

Here's revised code and a Demo: http://jsfiddle.net/m1erickson/U6K9j/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>
<script>
$(function(){

    var c=document.getElementById("menuCanvas");
    var ctx=c.getContext("2d");

var isMoving = true;

var gradienty = 0;
var gradientheight = 100;
var gradientyVel = 1;

var gradientImage = new Image();
gradientImage.onload=function(){
    setInterval(main, 20);
}
gradientImage.src = "house16x16.png";

function main() {
    ctx.clearRect(0,0,c.width,c.height);
    ctx.drawImage(gradientImage, 0, gradienty);
    if(isMoving == true) {
        gradienty += gradientyVel;
        if((gradienty+gradientheight)==c.height || gradienty<=0) {
            gradientyVel *= -1;
        }
    }
}

}); // end $(function(){});
</script>
</head>
<body>
    <canvas id="menuCanvas" width=300 height=300></canvas>
</body>
</html>

You're calling main and then passing the RESULT to setInterval . Just change

function mainLoop() {
    setInterval(main(), 10);
}

to

function mainLoop() {
    setInterval(main, 10);
}

That'll get you started, but you'll also need to clear the canvas in between draws. (As you'll notice if you run this)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM