Skip to main content

HTML5 Canvas Stop Shape Transform

If you need to stop transforming immediately you can use stopTransform method of Konva.Transformer instance.

Instructions: Try to resize a shape. If width of the shape is bigger than 200 transforming will be stopped.

import Konva from 'konva';

const width = window.innerWidth;
const height = window.innerHeight;

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

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

const rect = new Konva.Rect({
x: 50,
y: 50,
width: 100,
height: 100,
fill: 'yellow',
stroke: 'black',
draggable: true,
});
layer.add(rect);

const tr = new Konva.Transformer({
nodes: [rect],
});
layer.add(tr);

rect.on('transform', function () {
const width = rect.width() \* rect.scaleX();
if (width > 200) {
tr.stopTransform();
}
});