const preact = require('preact'); const { Component } = require('preact'); const times = require('lodash/times'); const anime = require('animejs').default; const { TIMES } = require('../../constants'); class Electrify extends Component { constructor() { super(); this.animations = []; } render() { return ( ); } componentDidMount() { this.animations.push(anime({ targets: ['#electrify'], opacity: [ { value: 1, delay: TIMES.TARGET_DELAY_MS, duration: TIMES.TARGET_DURATION_MS * 0.2 }, { value: 0, delay: TIMES.TARGET_DURATION_MS * 0.6, duration: TIMES.TARGET_DURATION_MS * 0.2 }, ], easing: 'easeInOutSine', })); const path = () => { const startX = anime.random(420, 440); const startY = anime.random(180, 200); return ` M ${startX},${startY} L ${anime.random(387, 407)},${anime.random(300, 320)} L ${anime.random(300, 320)},${anime.random(387, 407)} L ${anime.random(180, 200)},${anime.random(420, 440)} L ${anime.random(60, 80)},${anime.random(387, 407)} L ${anime.random(-27, 7)},${anime.random(300, 320)} L ${anime.random(-20, -40)},${anime.random(180, 200)} L ${anime.random(-27, 7)},${anime.random(60, 80)} L ${anime.random(60, 80)},${anime.random(-27, 7)} L ${anime.random(180, 200)},${anime.random(-20, -40)} L ${anime.random(300, 320)},${anime.random(-27, 7)} L ${anime.random(387, 407)},${anime.random(60, 80)} L ${startX},${startY} `; }; this.animations.push(anime({ targets: ['#electrify path'], d: times(10, path), easing: 'easeInOutSine', loop: true, duration: TIMES.TARGET_DURATION_MS / 5, })); this.animations.push(anime({ targets: ['#electrify path.white'], strokeWidth: [2, 1], easing: 'easeInOutSine', direction: 'alternate', loop: true, duration: TIMES.TARGET_DURATION_MS, })); } // 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 = Electrify;