Skip to main content

Tween 模糊滤镜教程

要使用 Konva 对滤镜进行补间,我们只需对与滤镜相关的属性进行补间。 在本教程中,我们将补间 blurRadius 属性,该属性控制应用于图像的模糊量。

步骤:将鼠标悬停或触摸图像以聚焦(减少模糊)。

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 imageObj = new Image();
imageObj.onload = () => {
const lion = new Konva.Image({
x: 50,
y: 50,
image: imageObj,
draggable: true,
});

layer.add(lion);

// 添加模糊滤镜
lion.cache();
lion.filters([Konva.Filters.Blur]);
lion.blurRadius(10);

// 创建模糊补间
const tween = new Konva.Tween({
node: lion,
duration: 0.5,
blurRadius: 0,
easing: Konva.Easings.EaseInOut,
});

// 绑定事件
lion.on('mouseenter touchstart', () => {
tween.play();
});

lion.on('mouseleave touchend', () => {
tween.reverse();
});

};
imageObj.src = 'https://konvajs.org/assets/lion.png';
imageObj.crossOrigin = 'anonymous';

stage.add(layer);