HTML5 Canvas Shape Resize and Transform Limits


How to limit size changes of a shape?

To limit or change resize and transform behavior you can use boundBoxFunc property.
It works a bit similar to dragBoundFunc.

Instructions: Try to resize a shape. You will see that its width is limited to 200.

Konva Shape transform and selection Demoview raw
<!DOCTYPE html>
<script src=""></script>
<meta charset="utf-8" />
<title>Konva Transform Limits Demo</title>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;

<div id="container"></div>
var width = window.innerWidth;
var height = window.innerHeight;

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

var layer = new Konva.Layer();

var rect = new Konva.Rect({
x: 160,
y: 60,
width: 100,
height: 90,
fill: 'red',
name: 'rect',
stroke: 'black',
draggable: true

var MAX_WIDTH = 200;
// create new transformer
var tr = new Konva.Transformer({
boundBoxFunc: function(oldBoundBox, newBoundBox) {
// "boundBox" is an object with
// x, y, width, height and rotation properties
// transformer tool will try to fit node into that box
// "width" property here is a visible width of the object
// so it equals to rect.width() * rect.scaleX()

// the logic is simple, if new width is too big
// we will return previous state
if (Math.abs(newBoundBox.width) > MAX_WIDTH) {
return oldBoundBox;

return newBoundBox;