admin管理员组文章数量:1022997
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
1 Answer
Reset to default 3The 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
.
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
1 Answer
Reset to default 3The 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
.
本文标签: javascriptCanvas drawImage not workingStack Overflow
版权声明:本文标题:javascript - Canvas drawImage not working - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745577553a2157113.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论