Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:04:37+00:00 2026-05-29T07:04:37+00:00

I am having trouble drawing an animation on a HTML 5 canvas inside a

  • 0

I am having trouble drawing an animation on a HTML 5 canvas inside a loop. I have created a javascript animation function/class that stores frames and duration information and provides a get function to getFrame()[returns image] sub-function to retrieve the relevant frame. This works perfectly fine so i haven’t included the full script. If anyone wants or needs it i will send to you.

When i call the following code it displays the animation as intended.

canvas.drawImage(tile1a.getFrame(),50,60);
canvas.drawImage(tile1a.getFrame(),50,90);
canvas.drawImage(tile1a.getFrame(),50,120);
//etc

This isn’t very efficient as i want to draw a lot of tiles is column so it would be better to use a for loop. Therefore i tried the following code but the page just keeps crashing.

for (i=1;i<=10;i++)
    {
        canvas.drawImage(tile1a.getFrame(),FIELD_WIDTH,i*30);
    }

However, i then tried the following which worked:

var temp = tile1a.getFrame();
for (i=1;i<=10;i++)
    {
        canvas.drawImage(temp,FIELD_WIDTH,i*30);
    }

So the problem is definitely something to with me calling getFrame() within the for loop. I recon it is probably something really stupid but i can’t figure it out!

Any help is much appreciated.
thanks
Jason

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:04:38+00:00Added an answer on May 29, 2026 at 7:04 am

    I think I figured it out. The loop in your draw function is set up like this:

    for (i=1; i<=10; i++) {
        // ...
    }
    

    Notice that you didn’t create the i variable with the var keyword! That means that it will be a global variable!

    Now look at the loop in your getFrame function:

    for (i=0; i<this.frames.length; i++) {
        // ...
    }
    

    The i in that loop is also global…

    Your getFrame function is making sure that i will always be reset to 4 (because you only have four frames in it)!

    The quick fix is to add some vars:

    // in the `draw` function, for example
    for (var i = 1; i <= 10; i++) {
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with a gradient drawing call. I have a Form that looks
I having trouble in dividing the HTML frames. I have been using the following
I'm having trouble drawing a UIImage in front of a CAShapeLayer . I have
I'm having trouble in mouse coordinates when drawing at canvas of HTML5. I'm using
I am having trouble with the Graphics that I am drawing on a Panel
I am having trouble drawing a line within a group box in a simple
I'm trying to make a simple drawing app in c++, but i'm having trouble
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
I am having trouble drawing a very short line with actionscript3: var cSp:Sprite =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.