To animate a shape’s scale with Konva, we can create a new animation with Konva.Animation, and define a function which modifies the shape’s scale with each animation frame.
In this tutorial, we’ll scale the x and y component of a blue hexagon, the y component of a yellow hexagon, and the x component of a red hexagon about an axis positioned on the right side of the shape.
Instructions: drag and drop the hexagons as they animate
var stage = new Konva.Stage({ container: 'container', width: width, height: height });
var layer = new Konva.Layer();
/* * leave center point positioned * at the default which is at the center * of the hexagon */ var blueHex = new Konva.RegularPolygon({ x: 50, y: stage.height() / 2, sides: 6, radius: 40, fill: '#00D2FF', stroke: 'black', strokeWidth: 4, draggable: true });
var anim = new Konva.Animation(function(frame){ var scale = Math.sin((frame.time * 2 * Math.PI) / period) + 0.001; // scale x and y blueHex.scale({ x: scale, y: scale }); // scale only y yellowHex.scaleY(scale); // scale only x redHex.scaleX(scale); }, layer);