Skip to main content

All Tween Controls Tutorial

To control tweens with Konva, we can use the following methods:

  • play() - Start or resume the tween
  • pause() - Pause the tween
  • reverse() - Reverse the tween direction
  • reset() - Reset to initial state
  • finish() - Jump to final state
  • seek() - Jump to specific position

Instructions: Use the buttons to control the tween animation of the circle.

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();

const circle = new Konva.Circle({
x: 100,
y: height / 2,
radius: 70,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
});

layer.add(circle);
stage.add(layer);

const tween = new Konva.Tween({
node: circle,
duration: 2,
x: width - 100,
easing: Konva.Easings.EaseInOut,
});

// create buttons

const controls = ['play', 'pause', 'reverse', 'reset', 'finish'];
controls.forEach(control => {
const button = document.createElement('button');
button.textContent = control;
button.addEventListener('click', () => {
tween[control]();
});
document.body.appendChild(button);
});

// seek control

const seekBtn = document.createElement('button');
seekBtn.textContent = 'Seek to 50%';
seekBtn.addEventListener('click', () => {
tween.seek(1); // seek to 1 second

});
document.body.appendChild(seekBtn);


</rewritten_file>