【发布时间】:2015-03-13 23:14:47
【问题描述】:
我有多个图像要绘制到画布上,但一次只能绘制一个。谁能给我解释一下为什么?
JSON
"images": [
{
"name": "carat_white",
"src": "images/CaretR-01.png",
"x": 0,
"y": 0,
"dest_x": 548,
"dest_y": 148,
"width": 40,
"height": 40
},
{
"name": "carat_black",
"src": "images/Caret-01.png",
"x": 0,
"y": 0,
"dest_x": 700,
"dest_y": 100,
"width": 40,
"height": 40
}
]
JS
var images = result[0].images;
var imageObj = new Image();
function drawImages(src, x, y, width, height, dest_x, dest_y, dest_width, dest_height){
dest_width = (width / 2);
dest_height = (height / 2);
imageObj.onload = function(){
ctx.drawImage(imageObj, x, y, width, height, dest_x, dest_y, dest_width, dest_height);
}
imageObj.src = src;
}
drawImages(images[0].src, images[0].x, images[0].y, images[0].width, images[0].height, images[0].dest_x, images[0].dest_y, images[0].width, images[0].height);
drawImages(images[1].src, images[1].x, images[1].y, images[1].width, images[1].height, images[1].dest_x, images[1].dest_y, images[1].width, images[1].height);
如果我注释掉其中一个 drawImages() 函数,则另一个会显示,但如果我将它们都“激活”,则只会显示后一个。所以基本上,一个新的图像被绘制,但旧的图像被删除。
【问题讨论】:
标签: javascript canvas