Skip to main content

更多缓动函数教程

本教程演示了 Konva 提供的所有缓动函数集,包括:

  • 线性
  • 缓入
  • 缓出
  • 缓入缓出
  • 回弹缓入
  • 回弹缓出
  • 回弹缓入缓出
  • 弹性缓入
  • 弹性缓出
  • 弹性缓入缓出
  • 反弹缓入
  • 反弹缓出
  • 反弹缓入缓出
  • 强缓入
  • 强缓出
  • 强缓入缓出

有关所有可用的缓动,请参见 缓动文档

说明:点击“播放”来使用不同的缓动函数过渡所有文本节点。

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 easings = [
  'Linear',
  'EaseIn',
  'EaseOut',
  'EaseInOut',
  'BackEaseIn',
  'BackEaseOut',
  'BackEaseInOut',
  'ElasticEaseIn',
  'ElasticEaseOut',
  'ElasticEaseInOut',
  'BounceEaseIn',
  'BounceEaseOut',
  'BounceEaseInOut',
  'StrongEaseIn',
  'StrongEaseOut',
  'StrongEaseInOut',
];

const tweens = [];

easings.forEach((easing, i) => {
  const text = new Konva.Text({
    x: 50,
    y: 30 + i * 25,
    text: easing,
    fontSize: 16,
    fontFamily: 'Calibri',
    fill: 'black',
  });
  layer.add(text);

  tweens.push(
    new Konva.Tween({
      node: text,
      duration: 2,
      x: width - 200,
      easing: Konva.Easings[easing],
    })
  );
});

stage.add(layer);

// 创建按钮
const button = document.createElement('button');
button.textContent = '播放';
button.style.position = 'absolute';
button.style.top = '0px';
button.style.left = '0px';
button.addEventListener('click', () => {
  tweens.forEach((tween) => {
    tween.reset();
    tween.play();
  });
});
document.body.appendChild(button);