Search code examples
konvajs

How do I add an id to a konvajs node?


I'm creating a set of circles and would like to iterate through them and highlight based on an identifier of some kind.

It would great if I could just set circle.id = 1, circle.id = 2, ...


Solution

  • // create circle
    var circle = new Konva.Circle({
      radius: 40,
      fill: 'red',
      stroke: 'black',
      strokeWidth: 5
    });
    
    // set id
    circle.id('foo');
    
    // get id
    var name = circle.id();
    

    Note: id = type String, inherited from Konva.Node#id

    Link: https://konvajs.org/api/Konva.Circle.html#id__anchor