Skip to main content

Tween Blur Filter Tutorial

To tween a filter using Konva, we can simply tween the properties associated with the filter. In this tutorial, we'll tween the blurRadius property, which controls the amount of blur applied to the image.

Instructions: Mouseover or touch the image to focus it (reduce blur).

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

// create image
const imageObj = new Image();
imageObj.onload = () => {
const lion = new Konva.Image({
x: 50,
y: 50,
image: imageObj,
draggable: true,
});

layer.add(lion);

// add blur filter
lion.cache();
lion.filters([Konva.Filters.Blur]);
lion.blurRadius(10);

// create blur tween
const tween = new Konva.Tween({
node: lion,
duration: 0.5,
blurRadius: 0,
easing: Konva.Easings.EaseInOut,
});

// bind events
lion.on('mouseenter touchstart', () => {
tween.play();
});

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

};
imageObj.src = '/assets/lion.png';
imageObj.crossOrigin = 'anonymous';

stage.add(layer);