Javascript Fabric js,我尝试将加载的图像添加到阵列中以供以后使用,但它们没有被存储
我从控制台知道“console.log”(“此数组为空,但应包含映像”+数组);”代码首先运行,但如果所有代码在映像完全加载之前运行,如何使用该数组Javascript Fabric js,我尝试将加载的图像添加到阵列中以供以后使用,但它们没有被存储,javascript,fabricjs,Javascript,Fabricjs,我从控制台知道“console.log”(“此数组为空,但应包含映像”+数组);”代码首先运行,但如果所有代码在映像完全加载之前运行,如何使用该数组 fabric.Image.fromURL('https://www.tokkoro.com/picsup/1434470-landscape.jpg', function(img){ img.opacity=0.3; img.scaleToWidth(canvas.getWidth()/2); img.scaleY=0.3;
fabric.Image.fromURL('https://www.tokkoro.com/picsup/1434470-landscape.jpg', function(img){
img.opacity=0.3;
img.scaleToWidth(canvas.getWidth()/2);
img.scaleY=0.3;
img.hasBorders= false,
img.hasControls= false,
img.hasRotatingPoint= false;
img.selectable=false;
canvas.add(img);
//right here
array.push(img);
console.log("this is the array after loading"+array);
//
});
//
console.log("this array is empty, but it should contain the image"+array);
//
console output:
this array is empty, but it should contain the image
this is the array after loading <fabric.Image: { src: "https://www.tokkoro.com/picsup/1434470-landscape.jpg" }>
fabric.Image.fromURL('https://www.tokkoro.com/picsup/1434470-landscape.jpg,函数(img){
不透明度=0.3;
img.scaleToWidth(canvas.getWidth()/2);
img.scaleY=0.3;
img.hasBorders=false,
img.hasControls=false,
img.hasRotatingPoint=假;
img.selective=false;
canvas.add(img);
//就在这里
阵列推送(img);
log(“这是加载后的数组”+数组);
//
});
//
log(“此数组为空,但应包含图像”+数组);
//
控制台输出:
此数组为空,但应包含图像
这是加载后的数组
我通过在加载通过图像对象的图像时生成一个函数来修复它,并将其添加到函数中的数组中创建fabric.image的对象,并将其推入数组,然后使用setSrc
加载图像。