简体   繁体   English

在fabric.js中加载图像fromUrl而不回调

[英]Load image fromUrl without callback in fabric.js

I am trying to to store image in variable to add it in canvas after some event. 我试图将图像存储在变量中,以便在某些事件后将其添加到画布中。

code like this is working 像这样的代码是有效的

new fabric.Image.fromURL('http://www.w3schools.com/css/img_fjords.jpg', function (img)
       {
           console.log(img);
       });

on the console i get thing like this: 在控制台上我得到这样的事情:

klass {filters: Array[0], resizeFilters: Array[0], _element: img.canvas-img, _originalElement: img.canvas-img, width: 560…}

This is good. 这很好。 but when i am trying to store this image is variable like this. 但是当我试图存储这个图像是这样的变量。

var img = new fabric.Image.fromURL('http://www.w3schools.com/css/img_fjords.jpg');

when i console.log(img) i get this 当我console.log(img)我得到这个

f…c.I…e.fromURL {}

Please explain how to save, and what i am doing wrong. 请解释如何保存,以及我做错了什么。

You can use below code to add image into canvas. 您可以使用下面的代码将图像添加到画布中。

var imgObj = new Image();
imgObj.src = "http://www.w3schools.com/css/img_fjords.jpg";
imgObj.onload = function () 
{
    var image = new fabric.Image(imgObj);
    image.set({
        angle: 0,
        padding: 0,
        originX: "center",
        originY: "center"
    });
}

You can set other properties too while adding image. 添加图像时也可以设置其他属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM