How to get an object identifier in fabricjs? For example
var text = new fabric.IText('hello world', { left: mouse1X, top: mouse1Y });
ID = text.get???
This identifier should be present in
JSON.stringify(canvas)
canvas.loadFromJSON()
identifier should be present in the JSON.stringify(canvas) canvas.loadFromJSON()
When you create an object, pass along the options an id
attribute:
var obj = fabric.Object({id: 'myid'});
or add at runtime:
obj.id = 'myid';
When saving to JSON use the propertiesToInclude
parameter to specify you want to save it:
obj.toJSON(['myid', 'others...']);
or
canvas.toJSON(['myid', 'others...']);
Loading back will be automatic.
There are not id attributes on the objects, but you can create your own. like this:
var msg = 'hello world';
var myId = 1;
var text = new fabric.IText(msg,
{
left: mouse1X,
top: mouse1Y,
id: myId } //my custom property
);
canvas.add(text);
//get id value
console.log(text.get('id'));
//OR
console.log(text.id));
//change id value
text.set('id',5);
//OR
text.id = 5;
canvas.renderAll();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With