HTML5 canvas Label Tutorial

To create a text label with Konva, which can be used for creating text with backgrounds, simple tooltips, or tooltips with pointers, we can instantiate a Konva.Label() object.

For a full list of attributes and methods, check out the Konva.Label documentation.

Konva Label Demoview raw
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/[email protected]/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Label Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}
</style>
</head>

<body>
<div id="container"></div>
<script>
var stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
});

var layer = new Konva.Layer();

// tooltip
var tooltip = new Konva.Label({
x: 170,
y: 75,
opacity: 0.75,
});

tooltip.add(
new Konva.Tag({
fill: 'black',
pointerDirection: 'down',
pointerWidth: 10,
pointerHeight: 10,
lineJoin: 'round',
shadowColor: 'black',
shadowBlur: 10,
shadowOffsetX: 10,
shadowOffsetY: 10,
shadowOpacity: 0.5,
})
);

tooltip.add(
new Konva.Text({
text: 'Tooltip pointing down',
fontFamily: 'Calibri',
fontSize: 18,
padding: 5,
fill: 'white',
})
);

// label with left pointer
var labelLeft = new Konva.Label({
x: 20,
y: 130,
opacity: 0.75,
});

labelLeft.add(
new Konva.Tag({
fill: 'green',
pointerDirection: 'left',
pointerWidth: 20,
pointerHeight: 28,
lineJoin: 'round',
})
);

labelLeft.add(
new Konva.Text({
text: 'Label pointing left',
fontFamily: 'Calibri',
fontSize: 18,
padding: 5,
fill: 'white',
})
);

// simple label
var simpleLabel = new Konva.Label({
x: 180,
y: 150,
opacity: 0.75,
});

simpleLabel.add(
new Konva.Tag({
fill: 'yellow',
})
);

simpleLabel.add(
new Konva.Text({
text: 'Simple label',
fontFamily: 'Calibri',
fontSize: 18,
padding: 5,
fill: 'black',
})
);

// add the labels to layer
layer.add(tooltip).add(labelLeft).add(simpleLabel);

// add the layer to the stage
stage.add(layer);
</script>
</body>
</html>
Enjoying Konva? Please consider to support the project.