Я использую gsap
для создания анимации.
При нажатии кнопки создается анимация пузыря.
Когда анимация завершена, она разрушается.
Я думаю вопрос в использовании карты на React component
, но я не могу найти другой случай
Вот мой код React и пример js скрипка:
https://jsfiddle.net/xiaowang/ueqsg83j/58/
const { useState, useEffect, useRef } = React;
gsap.registerPlugin(MotionPathPlugin)
const Bubble = ({ onClose, data }) => {
const pointRef = useRef(null)
useEffect(() => {
const path = []
let offsetY = 0
for(let i = 0; i < 10; i++) {
const y = offsetY - Math.floor(Math.random() * 20 + 30)
offsetY = y
path.push({ x: Math.floor(Math.random() * 40 - 20), y })
}
gsap.to(pointRef.current, 5, {
motionPath: {
path,
type: 'cubic'
},
onComplete: () => onClose()
})
return () => {}
}, [])
return (<span className="bubble" ref={pointRef}>{data.id}</span>)
}
const App = () => {
const [count, setCount] = useState(0)
const [bubbles, setBubbles] = useState([])
const handleCreate = () => {
setBubbles([...bubbles, {id: count}])
setCount(count + 1)
}
const handleClose = index => {
const newBubbles = [...bubbles]
newBubbles.splice(index, 1)
setBubbles(newBubbles)
}
return (
<div className="wrap">
{
bubbles.map((item, index) => (
<Bubble
key={item.id}
data={item}
onClose={() => handleClose(index)} />
))
}
<button type="button" onClick={handleCreate}>Click Me</button>
</div>
)
}
ReactDOM.render(<App />, document.getElementById('app'))