Something wrong in my project, using fabric.js

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);
}

that is my package function to load an image to the canvas and I want to select the image object that was put into canvas by the API canvas.setActiveObject(canvas.item(i)) but it only finds the item(0), 1, 2, 3. Once more, it can't find it.

Answers


what you need is to load an image and make it active ?

If so, i created a jsfiddle example that loads an image and activates it.

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/

hope helps ,good luck.


Need Your Help

jqPlot bar graph with multiseries showing empty data series

javascript asp.net-mvc jqplot

I´m building a dynamic 6 series bar graph with jqPlot. In my implementation, the user will choose which data will go into each serie and the system will load data according to his choice. The funct...