Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clone() is not working for Path Object with the latest version of Fabricjs

Tags:

fabricjs

If a fabric.Path object is cloned with clone() method ,the path object is not duplicated. I have seen this issue here https://github.com/kangax/fabric.js/issues/330 but the Version of Fabric js is different. Could some one please help me on this.

  var obj = canvas.getActiveObject();
    if (!obj) return;
    var clone = obj.clone();
    clone.set({
        top: clone.get('top') + 150
    });
    canvas.add(clone);
    canvas.renderAll();

Below is the error I am getting.

 else {
  fabric.util.enlivenObjects(object.paths, function(enlivenedObjects) {
    delete object.paths;
    callback(new fabric.PathGroup(enlivenedObjects, object));
   ****Uncaught TypeError: undefined is not a function****
  });
 }
};

The above code is working well for all the objects but the code is not working for Path Object

like image 776
John Avatar asked Oct 28 '25 14:10

John


1 Answers

fabric.Path and fabric.PathGroup object's are async since fabric.js version 1.2.2 (https://github.com/kangax/fabric.js/commit/c8cab03aace5510554cd02fa143248ab7497f6c2).

So you have to differentiate between async and sync objects.

var obj = canvas.getActiveObject();

if (!obj) return;

if (fabric.util.getKlass(obj.type).async) {
  obj.clone(function (clone) {
    clone.set({left: 200, top: 100});
    canvas.add(clone);
  });
}
else {
  canvas.add(obj.clone().set({left: 100, top: 100}));
}

Here you can see it in action: http://jsfiddle.net/Kienz/73Cta/

like image 119
Kienz Avatar answered Oct 31 '25 11:10

Kienz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!