HolyThunder
HolyThunder

Reputation: 463

"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.

Upvotes: 1

Views: 105

Answers (1)

mindandmedia
mindandmedia

Reputation: 6825

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()

Upvotes: 1

Related Questions