Skip to main content

基础 Tweening 教程

要使用 Konva 对属性进行 tween,我们可以实例化一个 Konva.Tween 对象,然后通过调用 play() 开始 tween 任何 Shape、Group、Layer 或 Stage 的数值属性都可以进行过渡,例如:

  • x、y(位置)
  • rotation
  • width、height、radius
  • strokeWidth
  • opacity
  • scaleX、scaleY
  • offsetX、offsetY

有关属性和方法的完整列表,请查看 Konva.Tween 文档。

说明:点击圆形以启动一个简单的线性动画。

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

circle.on('click tap', () => {
// simple tween
const tween = new Konva.Tween({
node: circle,
duration: 1,
x: width - 100,
easing: Konva.Easings.Linear,
});
tween.play();
});