我使用fabric.js将3张(最多5张)图片放在画布上,现在我正尝试用带有新图片网址的数组替换这些图片。最后我生成图像输出,但由于图像是asyc加载的,因此它们不会在输出(图像)中呈现。我找了很多解决方案,但没有任何作用
这是我正在使用的代码:
var canvas_virtual = new fabric.Canvas('virtual_canvas');
function GetOutput(){
var exist_objects = canvas.getObjects();
for (var k = 0; k < exist_objects.length; k++) {
var obj = fabric.util.object.clone(exist_objects[k]);
if(obj.name.indexOf("img_icon") > -1){
fnAddImageSave(obj.top,obj.left,obj.id,obj.propertyname,obj.tempPreviewData,obj.nutrientId,listIcons[k],canvas_virtual,obj.height);
}
}
canvas_virtual.renderAll();
console.log(canvas_virtual.toDataURLWithMultiplier("image/jpeg", 1));
}
fnAddImageSave的定义:
function fnAddImageSave(top, left, image_id, property_name, previewData, nutrientValue, url,canvasObj,height)
{
var img = new Image(); //Equivalent: $(document.createElement('img'))
var URL = url;
//$('#add').append(img);
var name = image_id;
fabric.Image.fromURL(URL, function (oImg) {
//oImg.set('left', PosX).set('top',PosY);
oImg.height = height;
canvasObj.add(oImg);
canvasObj.renderAll();
}, {
"id": name,
"name": name,
"left": left,
"top": top,
"scaleX": .5,
"scaleY": .5,
"cornerSize": 5,
"propertyname": property_name,
"tempPreviewData": previewData,
"nutrientId": nutrientValue
});
}