From b298feff9c080a8d1969899ee71f6b283529d0fc Mon Sep 17 00:00:00 2001 From: ntr Date: Mon, 3 Jun 2019 18:57:54 +1000 Subject: [PATCH] consolidate construct imgs --- client/src/components/construct.jsx | 86 +++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 client/src/components/construct.jsx diff --git a/client/src/components/construct.jsx b/client/src/components/construct.jsx new file mode 100644 index 00000000..974b14b6 --- /dev/null +++ b/client/src/components/construct.jsx @@ -0,0 +1,86 @@ +const preact = require('preact'); +const { Component } = require('preact'); + +const anime = require('animejs').default; + +const genAvatar = name => { + let hash = 0; + if (name.length === 0) return hash; + // Probs don't need to hash using the whole string + for (let i = 0; i < name.length; i += 1) { + const chr = name.charCodeAt(i); + hash = ((hash << 5) - hash) + chr; + hash = hash % 10000; + } + return `${hash}`; +}; + +const animations = {}; +function animateConstruct(id) { + if (animations[id]) return false; + animations[id] = true; + const duration = anime.random(2000, 18000); + const target = document.getElementById(id); + return anime({ + targets: target, + translateX: () => anime.random(-20, 20), + translateY: () => anime.random(0, -40), + rotate: () => anime.random(-15, 15), + duration, + direction: 'alternate', + easing: 'linear', + loop: true, + complete: () => animations[id] = false, + }); +} + +function clearAnimation(id) { + animations[id] = false; +} + +class ConstructAvatar extends Component { + render() { + return ( +
+ ); + } + + componentDidMount() { + animateConstruct(this.props.id); + } + + componentWillUnmount() { + clearAnimation(this.props.id); + } +} + +class ConstructImg extends Component { + render() { + return ( + event.target.setAttribute('src', '/molecules/726.svg')} + /> + ); + } + + componentDidMount() { + animateConstruct(this.props.id); + } + + componentWillUnmount() { + clearAnimation(this.props.id); + } +} + +module.exports = { + ConstructAvatar, + ConstructImg, +};