0

I made a for loop which draws 40 rectangles , they all need have height: 400px; but I want to start at height: 0; and animate to 400 with requestAnimationFrame This is my code :

function draw(){

    height += 5;
        if(height > 1){
            for (var i = 0; i < dataSales.length; i++) {

                    geel += 6;
                    blauw += 6;
                    groen -= 6;
                    xPos += 23;
                    ctx.beginPath();
                    ctx.fillStyle= "#89C349";
                    ctx.rect(xPos,595,20,-height);
                    ctx.fill();
            }
        }
    window.requestAnimationFrame(draw);
    }
4
  • 1
    whats the issue? what part is/isnt working? what do you need help on? Commented May 14, 2015 at 13:35
  • if i do log i can see my height is going up, but my rectangles just draw at 5px height and stop. Commented May 14, 2015 at 13:41
  • can you show the full code? or better yet recreate in a jsfiddle? Commented May 14, 2015 at 13:51
  • jsfiddle.net/dun1ezrx/1 Commented May 14, 2015 at 14:06

1 Answer 1

1

The issue is that the second time draw gets called, xPos = 907, the third time draw gets called xPos = 1804, etc.

You want xPos, geel, blauw and groen to all be defined inside draw so that they reset every time. Here is a fiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot! , Now i still have a little problem , actually the bars should go as high according to the dataSales[] , like height = dataSales[i] / 800 , is there a way to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.