Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Fabric js,我尝试将加载的图像添加到阵列中以供以后使用,但它们没有被存储_Javascript_Fabricjs - Fatal编程技术网

Javascript Fabric js,我尝试将加载的图像添加到阵列中以供以后使用,但它们没有被存储

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;

我从控制台知道“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;
     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
加载图像。