HTML5 Canvas Force Update Tutorial

Konva.Transformer automatically track properties of attached nodes.
So it will adopt its own properties automatically.

But is some cases Konva.Transformer can’t do this. Currently Konva.Transformer can not track deep changes inside Konva.Group node. In this case you will need to use forceUpdate method to reset transforming tools

Instructions: Click the button. See how transformer is changed.

Transformer Force Update Demoview raw
<!DOCTYPE html>
<html>

<head>
<!-- USE DEVELOPMENT VERSION -->
<script src="https://unpkg.com/konva@2.4.2/konva.min.js"></script>
<meta charset="utf-8">
<title>Konva Transform Events Demo</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #F0F0F0;
}

#button {
position: absolute;
top: 5px;
left: 10px;
}
</style>
</head>

<body>

<div id="container"></div>
<input type="button" id="button" value="Add random shape">
<script>
var width = window.innerWidth;
var height = window.innerHeight;

var stage = new Konva.Stage({
container: 'container',
width: width,
height: height
});

var layer = new Konva.Layer();
stage.add(layer);


var group = new Konva.Group({
x: 50,
y: 50,
draggable: true
});
layer.add(group)




// create new transformer
var tr = new Konva.Transformer();
layer.add(tr);
tr.attachTo(group);
layer.draw();

document.getElementById('button').addEventListener('click', addShape);
addShape();


function addShape() {
group.add(new Konva.Circle({
x: Math.random() * 100,
y: Math.random() * 100,
radius: Math.random() * 100,
fill: Konva.Util.getRandomColor(),
stroke: 'black',
strokeWidth: Math.random() * 10
}));
// force update manually
tr.forceUpdate();
layer.draw();
}

</script>

</body>

</html>