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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:28:51+00:00 2026-05-31T09:28:51+00:00

I am trying to draw an image on HTML canvas: var mainCanvas = document.getElementById(‘mainCanvas’);

  • 0

I am trying to draw an image on HTML canvas:

var mainCanvas = document.getElementById('mainCanvas');
var ctx = mainCanvas.getContext('2d');

I make an ajax request, and parse xml data that I get from it (works perfectly), and later when I draw different shapes on canvas it also works 100%.
What does not work is an image drawing in the following piece of code:

$(data).find('Object').each(function(){
  type = $(this).attr('type');
  x = $(this).attr('X');
  y = $(this).attr('Y');
  switch(type){
    case '2':
    height = h_panel;
    width = w_panel;
    ctx.fillStyle = sColor;
    ctx.fillRect(x,y,width,height);
    break;
    case '1':
    var powerFactoryImg = new Image();
    powerFactoryImg.onload = function(){
      alert('test');
      ctx.drawImage(powerFactoryImg,x,y,90,80);
    };
    powerFactoryImg.src = 'images/power_factory.png';
    break;

   //Other cases go here - they draw rectangles - all of them work

  }
});

I checked with Chrome Developer Tools that image is loading; also, alert in .onload is being called. The code is not working in both Chrome and FF.
What might be the problem here?

Thank you

  • 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-31T09:28:52+00:00Added an answer on May 31, 2026 at 9:28 am

    The bug is likely caused by the absence of var at your assignments. In the loop, you keep overwriting the type, x and y variables. Prefix them by var to solve your problem.

    See also: What is the purpose of the var keyword and when to use it (or omit it)?

    $(data).find('Object').each(function(){
      var type = $(this).attr('type');//<-- var
      var x = $(this).attr('X');      //<-- var
      var y = $(this).attr('Y');      //<-- var
      switch(type){
        case '2':
           var height = h_panel;  // <-- var
           var width = w_panel;   // <-- var
           ctx.fillStyle = sColor;
           ctx.fillRect(x,y,width,height);
        break;
        case '1':
           var powerFactoryImg = new Image();
           powerFactoryImg.onload = function(){
               alert('test: ' + [x, y]); //<-- "test" is not very useful. Add [x,y]
               ctx.drawImage(powerFactoryImg,x,y,90,80);
           };
           powerFactoryImg.src = 'images/power_factory.png';
        break;
    
        //Other cases go here - they draw rectangles - all of them work
    
       }
    });
    

    PS: For debugging purposes, I recommend to use console.log over alert.

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

Sidebar

Related Questions

I'm trying to draw curved arrows in a html canvas. I have no problem
I'm trying to draw an image in an ImageView, but i want it to
Working in Visual Studio 2008. I'm trying to draw on a PNG image and
I'm trying to draw over the whole screen by using the desktop canvas and
I am trying to load an svg image into canvas for pixel manipulation I
I'm trying to draw grayscale image in color as texture in OpenGL using two
I'm trying to draw a bmp image file from a 2 dimensional Boolean array.
I'm trying to draw a line (Red line in the image) over multiple panels,
I'm trying to draw an image with a certain color. To get this effect,
I'm using MFC, and I'm trying to draw an image to the screen. I've

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.