Dave's Brain

Browse - Programming Tips - How can I keep some of my own data in a Konva shape?

Date: 2018apr5
Library: Konva
Language: javaScript

Q.  How can I keep some of my own data in a Konva shape?

A.  Use setAttr() and getAttr() like this:

// This is just the normal way to add a circle... var circle = new Konva.Circle({ x: gStage.getWidth() / 2, y: gStage.getHeight() / 2, radius: 70, fill: 'red', stroke: 'black', strokeWidth: 4, preventDefault: true }); gLayer.add(circle); // Now get and set my data specific to my app. circle.setAttr('something', 'yes'); // <--- var something = circle.getAttr('something'); // <---
There's also setAttrs() getAttrs() for setting/getting a bunch at once. More info https://konvajs.github.io/api/Konva.Node.html

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.