HTML5 Canvas Konva Stop Animation Tutorial

To stop an animation with Konva, we can use the stop() method.
To restart the animation, we can again call the start().

Instructions: Click on “Start” to start the animation and “Stop” to stop the animation.

For a full list of attributes and methods, check out the Konva.Animation documentation.

Konva Stop Animation Demoview raw
<!DOCTYPE html>
<script src=""></script>
<meta charset="utf-8">
<title>Konva Stop Animation Demo</title>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #F0F0F0;
#buttons {
position: absolute;
top: 5px;
left: 10px;
#buttons > input {
padding: 10px;
display: block;
margin-top: 5px;
<div id="container"></div>
<div id="buttons">
<input type="button" id="start" value="Start">
<input type="button" id="stop" value="Stop">
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 hexagon = new Konva.RegularPolygon({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
sides: 6,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4


var amplitude = 150;
// in ms
var period = 2000;
var centerX = stage.getWidth() / 2;

var anim = new Konva.Animation(function(frame) {
hexagon.setX(amplitude * Math.sin(frame.time * 2 * Math.PI / period) + centerX);
}, layer);

document.getElementById('start').addEventListener('click', function() {
}, false);

document.getElementById('stop').addEventListener('click', function() {
}, false);