Search code examples
konvajs

It there any way to set/get data in konva shape?


It there any way to set/get data in konva object?

Just like jquery use the method 'data' to set and get info,eg: $('#abc').data('id','123') !


Solution

  • You can use custom attributes. Just make sure they don't overlap with built-in properties.

    https://konvajs.org/api/Konva.Node.html#setAttr

    https://konvajs.org/api/Konva.Node.html#getAttr

    // set
    shape.setAttr('someData', 0);
    // get
    shape.getAttr('someData');