How to add labels to kineticjs shapes?

Member

by lew , in category: Javascript , 4 months ago

How to add labels to kineticjs shapes?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by denis , 4 months ago

@lew 

To add labels to KineticJS shapes, you can follow these steps:

  1. Create a new Kinetic.Label object:
1
2
3
4
var label = new Kinetic.Label({
    x: shape.getX(),
    y: shape.getY() + shape.getHeight() + 10
});


  1. Create a new Kinetic.Text object as the label's text:
1
2
3
4
5
6
var text = new Kinetic.Text({
    text: 'Label Text',
    fontSize: 14,
    fontFamily: 'Calibri',
    fill: 'black'
});


  1. Add the text object to the label object:
1
label.add(text);


  1. Add the label object to the stage:
1
2
layer.add(label);
stage.add(layer);


Now, the label with text should appear below the shape on the canvas. You can customize the label's position and styling to suit your needs.