简体   繁体   中英

“Animating” background on canvas

I've been trying to animate a "background" for a project I'm doing on a canvas. I'm using raphaelJS, and what I'm doing is

function bg(){

    h = 0;

    var terra = paper.rect(0, 500 + h, 900, 100);
    terra.attr({'fill': '#00E639', 'stroke': 'none'});

    var troposfera = paper.rect(0, -100 + h, 900, 600);
    troposfera.attr({'fill': '#C0866D', 'stroke': 'none'});

    var estratosfera = paper.rect(0, -700 + h, 900, 600);
    estratosfera.attr({'fill': '#4497BF', 'stroke': 'none'});

    var ozono = paper.rect(0, -900 + h, 900, 600);
    ozono.attr({'fill': '#E8EAE9', 'stroke': 'none'});

    var mesosfera = paper.rect(0, -1500 + h, 900, 600);
    mesosfera.attr({'fill': '#434392', 'stroke': 'none'});

    var termosfera = paper.rect(0, -2100 + h, 900, 600);
    termosfera.attr({'fill': '#3A435A', 'stroke': 'none'});

    var exosfera = paper.rect(0, -2700 + h, 900, 300);
    exosfera.attr({'fill': '#000000', 'stroke': 'none'});
}

and then calling it under a setInterval as such

var drbg = setInterval(function(){
    bg();
    h = h + 1;
}, 40);

However, it's not working. It paints the background, but then does not move up as I wanted it to.

because the first line in bg() is h=0; , which resets h to 0 every time you call it...

to make it work, try moving h=0; out of bg()

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