javascript - Canvas drawImage not working - Stack Overflow

I am trying to draw an image on HTML canvas:var mainCanvas = document.getElementById('mainCanvas&#

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

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

Share Improve this question asked Mar 4, 2012 at 18:54 ZenJZenJ 3114 silver badges15 bronze badges 2
  • Given that the image is being loaded either the problem lies somewhere else and not in the code you provided or you're drawing the image outside the canvas boundaires so you can't see it. Or maybe drawing something over it? – Delta Commented Mar 4, 2012 at 19:01
  • 1 check values of x and y in onload: alert('test: ' + x + ', ' + y); – Serj-Tm Commented Mar 4, 2012 at 19:06
Add a ment  | 

1 Answer 1

Reset to default 3

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 remend to use console.log over alert.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745577554a4634058.html

相关推荐

  • javascript - Canvas drawImage not working - Stack Overflow

    I am trying to draw an image on HTML canvas:var mainCanvas = document.getElementById('mainCanvas&#

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信