const preact = require('preact'); const { Component } = require('preact'); const anime = require('animejs').default; class Noise extends Component { constructor() { super(); this.animations = []; } render() { return ( ); } componentDidMount() { this.animations.push(anime({ targets: ['#noiseFilter feTurbulence', '#noiseFilter feDisplacementMap'], easing: 'linear', loop: true, keyframes: [ { baseFrequency: 0.5, duration: () => anime.random(1000, 2000), }, ], })); this.animations.push(anime({ targets: ['#noiseFilter feDisplacementMap'], easing: 'linear', loop: true, keyframes: [ { scale: 2, duration: () => anime.random(2000, 5000), }, { scale: 4, duration: () => anime.random(150, 250), }, { scale: 2, duration: () => anime.random(100, 150), }, { scale: 4, duration: () => anime.random(150, 250), }, ], })); } // this is necessary because // skipping / timing / unmounting race conditions // can cause the animations to cut short, this will ensure the values are reset // because preact will recycle all these components componentWillUnmount() { for (let i = this.animations.length - 1; i >= 0; i--) { this.animations[i].reset(); } } } module.exports = Noise;