I clone the selected object on the canvas in Fabric.js using a simple function.
function duplicateObj() { var obj = canvas.getActiveObject(); var clone = fabric.util.object.clone(obj); clone.set({left: 100,top: 100}); canvas.add(clone); }
It works absolutely fine. Now, if I work with an object and the clone is no longer required, and I select and delete it, both objects, the clone and the original object are deleted. Delete function:
function deleteObj() { var obj = canvas.getActiveObject(); canvas.fxRemove(obj); }
The objects are the same. Is there a way to clone objects and make the clone independent of the original? I tried this:
function duplicateObj() { var obj = canvas.getActiveObject(); var clone = fabric.util.object.clone(obj); clone.initialize(); $.extend(clone, obj); fabric.util.object.extend(clone, obj); clone.set({left: 100,top: 100}); canvas.add(clone); }
It works, however the objects are repeated again, and if I use only initialization, I end the object that now has properties.
html5-canvas fabricjs
Benick
source share