To animate a shape’s rotation with Konva, we can create a new animation with Konva.Animation, and define a function which modifies the shape’s rotation with each animation frame.
In this tutorial, we’ll rotate a blue rectangle about the top left corner, a yellow rectangle about its center, and a red rectangle about an outside point.
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 the top left * corner of the rectangle */
var blueRect = new Konva.Rect({ x: 50, y: 75, width: 100, height: 50, fill: '#00D2FF', stroke: 'black', strokeWidth: 4 });
/* * move center point to the center * of the rectangle with offset */ var yellowRect = new Konva.Rect({ x: 220, y: 75, width: 100, height: 50, fill: 'yellow', stroke: 'black', strokeWidth: 4, offset: { x: 50, y: 25 } });
/* * move center point outside of the rectangle * with offset */
// one revolution per 4 seconds var angularSpeed = 90; var anim = new Konva.Animation(function(frame){ var angleDiff = (frame.timeDiff * angularSpeed) / 1000; blueRect.rotate(angleDiff); yellowRect.rotate(angleDiff); redRect.rotate(angleDiff); }, layer);