使用fabric.js在我的项目中出了点问题

时间:2015-12-01 07:28:40

标签: canvas fabricjs

function loadimg(src){
        var newww=new fabric.Image.fromURL(src,function(img){
            window.canvas.centerObject(img);
            window.canvas.setActiveObject(img);
            window.canvas.add(img);
        },{
            H_PADDING: 20,
            V_PADDING: 50,
        });

    // window.canvas_obj.add(neww);
    add_layer(src);
}

这是我的包函数,用于将图像加载到画布,我想选择由API canvas.setActiveObject(canvas.item(i))放入画布的图像对象 但它只能找到item(0),1,2,3。再一次,它无法找到它。

1 个答案:

答案 0 :(得分:0)

您需要的是加载图像并将其激活?

如果是这样,我创建了一个jsfiddle示例,用于加载图像并激活它。

var canvas = new fabric.Canvas('c');
canvas.backgroundColor = 'yellow';

var site_url =  'http://fabricjs.com/assets/1.svg';

//i use loadSVGFromURL to load an svg image but it is the same if you would like to use fabric.Image.fromURL() to load your image
fabric.loadSVGFromURL(site_url, function(obj) { 
          var img = new fabric.PathGroup(obj, { 
          left: 165, 
          top: 100, 
          width: 295, 
          height: 211 
        }); 
//add the loaded image on the canvas
        canvas.add(img); 
//activate(select) the image
    canvas.setActiveObject(img);
//render canvas to show the borders on the image
        canvas.renderAll(); 
          }); 

jsfiddle:http://jsfiddle.net/tornado1979/v4qzuw9w/

希望有所帮助,祝你好运。