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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:52:14+00:00 2026-06-13T11:52:14+00:00

I have 2 functions( js ) in a webpage for attaching an image to

  • 0

I have 2 functions(js) in a webpage for attaching an image to a canvas element:

 function attachImage(tile, x, y)
    {
      base_image = new Image();
      base_image.src = 'Images/tiles/'+(tile-1)+'.png';
      base_image.onload = function(){
        context.drawImage(base_image, 32*x,32*y);
      }
    }

and i literally copy and pasted it and duplicated it for another canvas

  function attachImage2(tile2, x2, y2)
    {
        base_image2 = new Image();
        base_image2.src = 'Images/tiles/'+(tile2-1)+'.png';
        base_image2.onLoad = function(){
            context2.drawImage(base_image2, 32*x2,32*y2);
      }
    }

On firefox, if i make one onload and the other onLoad, then it works fine. but no matter what i do for chrome, it doesn’t load the images properly.

edit: here is and image comparison between firefox and chrome:

FIREFOX: https://i.stack.imgur.com/l5sMt.jpg

CHROME: https://i.stack.imgur.com/u12H6.jpg

  • 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-13T11:52:15+00:00Added an answer on June 13, 2026 at 11:52 am

    You should use the onload (lowercase).

    Some general notes

    • When you need local variables use var.
    • You should set the onload before actually setting the src because otherwise (for cached) images the onload might not get called.
    • Since your methods do the same thing, you should use only one, and pass the context as a parameter

    So

    <script>
    var canvas = document.getElementById('map');
    context = canvas.getContext('2d');
    
    var canvas2 = document.getElementById('map2');
    context2 = canvas2.getContext('2d');
    
    function attachImage(tile, x, y, canvasContext)
    {
      var base_image = new Image();
      base_image.onload = function(){
        canvasContext.drawImage(base_image, 32*x,32*y);
      }
      base_image.src = 'Images/tiles/'+(tile-1)+'.png';
    }
    </script>
    

    and you should not create a script tag for each call

    <?php
    echo("<script>");
    for($tr = 0; $tr < count($mapArray)-1; $tr++) {
        for($tc = 0; $tc < count($mapArray[$tr])-1; $tc++) {
            $tile = $mapArray[$tr][$tc];
            echo "attachImage(" . $tile . "," . $tc . "," . $tr . ",context);";
        }
    }
    
    for($tr = 0; $tr < count($mapArrayy)-1; $tr++) {
        for($tc = 0; $tc < count($mapArrayy[$tr])-1; $tc++) {
            if($mapArrayy[$tr][$tc]!=0){
                echo "attachImage(" . $mapArrayy[$tr][$tc]. "," . $tc . "," . $tr . ",context2);";
            }
        }
    }
    echo("</script>");
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that is advancing scrolling images on a webpage. It can
I have a webpage where I want the user to see a new image
in writing a scripting engine, I have functions like (psuedo-code) function is_whitespace?(char c){ return
I have two functions that return simple strings. Both are registered. $.views.helpers({ parseDate: function
I have two functions here function Preloader() {} Preloader.prototype = { init:function() { //
I have two functions written in VB.NET: 1) The first function (call it GetValues())
Hello StackOverflow, I have an ASP.NET/C# webpage that calls functions from a managed .dll
I have a webpage with the following structure: <html> <head>...</head> <frameset> <frame name=frame1 src=/index.jsp/>
I have a webpage with a lot of javscript functions. It was a requirement
I have a function in the code behind of an ASP.NET webpage that creates

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.