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 9019139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:42:16+00:00 2026-06-16T04:42:16+00:00

In an application I’m writing there is a page where I load multiple base64-encoded

  • 0

In an application I’m writing there is a page where I load multiple base64-encoded images from my database and place them inside a JavaScript array. This is happening on the server via EJS templating, so when the user receives the HTML page the base64-encoded images are present (I checked this).

Next step is using JavaScript on the client-side to loop over the canvases and then fill each canvas with its corresponding image data. However, I’m getting some strange behaviour. Only the last canvas is filled each time.

for (var i = 1; i < 13; i++) {
    var ctx = document.getElementById('canvas-' + i).getContext('2d'),
        image = new Image();

    image.onload = function() {
        ctx.drawImage(image, 0, 0);
    }
    image.src = images[(i - 1)];
}

The images array is defined right above this code (in the same scope). If I execute the code like this only the last canvas (with the id ‘canvas-12’) will be filled. If I lower the for loop end condition to for example i < 11, only canvas with id ‘canvas-10’ will be filled.

Is there something I’m missing?

  • 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-06-16T04:42:18+00:00Added an answer on June 16, 2026 at 4:42 am

    Seems like a closure issue; each time you go through to loop, you are changing the object ctx included in the onload function. You need to make sure that you aren’t updating the objects during the loop by using a closure:

    for (var i = 1; i < 13; i++) {
        var ctx = document.getElementById('canvas-' + i).getContext('2d'),
            image = new Image();
        (function(ctx, image) {
            image.onload = function() {
                ctx.drawImage(image, 0, 0);
            }
            image.src = images[(i - 1)];
        })(ctx, image);
    }
    

    NOTE: I have not tested this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application is running in my local system app.config file <add key=dbconn value=Data Source=(local);database=VTech;User ID=sa;Password=sa;
My application has one activity which starts two services but does not bind them.
My application is connecting to a web service rpc/encoded. Im using Axis 1.4. When
The application prepared by me is using GCM to get push notifications from the
Application uses Entity Framework 4.1 with database first approach. I have in database a
Application.Run(form); Actually I tried to call this from my project. I got this exception.
My application requires an on-drive map as there will be no internet access during
Application flow An input file consists of multiple logical documents. Extract one input logical
Application.Run(new Main()); This line gives TypeInitializationException was unhandled after I switched from 3.5 to
// Application Drupal 7 // Problem Background My website imports product data from a

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.