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

  • Home
  • SEARCH
  • 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 8064767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:28:05+00:00 2026-06-05T11:28:05+00:00

The following code should display an image in a grid when I click the

  • 0

The following code should display an image in a grid when I click the button. The issue is I have to click the button twice in order to see the images. I get the log output on both function calls, even when the images don’t show up. What’s going on?

EDIT: I’ve found out that the problem only occurs when chrome developer tools is open, so it’s not a javascript problem.

EDIT2: In addition to the accepted answer, this link helped me: Javascript Canvas Element – Array Of Images

<html>
    <head>
        <title>Canvas Test</title>

        <script>
            function init() {
                var canvas = document.getElementById("canvas");
                context = canvas.getContext("2d");
                images = new Array();
            }

            function drawImage() {
                for (var i = 0; i < 12; i++) {
                    for (var j = 0; j < 12; j++) {
                        var index = i*12 + j;
                        images[index] = new Image();
                        images[index].src = 'image.jpg';
                        images[index].onload = function(image, index, i, j) {
                            console.log(index);
                            context.drawImage(image, i * 20, j * 20, 20, 20);
                            console.log("Loading image: " + image.src);
                        } (images[index], index, i, j);
                    }
                }
            }
        </script>

    </head>
    <body onload="init();">
        <canvas id="canvas" width="300px" height="300px" style="border: solid;"></canvas>
        <button onclick="drawImage();">Display Image</button>
    </body>
</html>
  • 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-05T11:28:07+00:00Added an answer on June 5, 2026 at 11:28 am

    Actually your code works. But sometimes the picture is not loaded and you will have to click the button twice.

    Try it here.


    2 problems:

    • First,
    function(image, index, i, j) {
        console.log(index);
        context.drawImage(image, i * 20, j * 20, 20, 20);
        console.log("Loading image: " + image.src);
    }(images[index], index, i, j);
    

    This will fire right way, because () at the end means to run it. That’s why you have to click it twice.

    • Second, src should come last. (MDN) (but actually it doesn’t matter)

    To fix it, you have to make the function so that it won’t run right away. Also because it is inside a loop and it uses scoped variables, it needs extra care. See code in demo.

     >FIXED DEMO!<

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

Sidebar

Related Questions

I have the following code require 'test_helper' class ApplicationControllerTest < ActionController::TestCase test should display
The following code should display the id out of the JSON-object, which I got
I have the following code which should put programs startable in Bash. if [
I have the following css code .container { width:800px; background-color:yellow; margin:auto; display:table; } .cell
I have the following OpenGL code in the display function: glLoadIdentity(); gluLookAt(eyex, eyey, eyez,
I am using following code in WPF to display image in webcam usingEmguCv library
in my app, i have large images, i display it in an image view
The following code should show a header bar, a footer bar, and an image,
The following code should show a certain track in iTunes: NSString* iTunesPath = [[NSWorkspace
The following code should pop up an alert box whenever I select a file

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.