import React, { useMemo, useEffect, useState } from 'react' import { Canvas } from '@react-three/fiber' import { AdditiveBlending } from 'three' import Particle from './Particle' import useParticlesConfig from './hooks/useParticlesConfig' const ParticlesCanvas = () => { const isWindowUndefined = typeof window === 'undefined' if (isWindowUndefined) return null const canvasRef = React.useRef(null) const [animate, setAnimate] = useState(true) const { config, particles } = useParticlesConfig() // stop animation if canvas if is not in viewport // to stop unnecessary computations const handleScroll = () => { if (canvasRef.current && typeof window !== 'undefined') { const rect = (canvasRef.current as HTMLCanvasElement)?.getBoundingClientRect() const isInView = rect.bottom > 0 setAnimate(isInView) } } useEffect(() => { window.addEventListener('scroll', handleScroll) return () => { window.removeEventListener('scroll', handleScroll) } }, []) /* ThreeJs setup */ const Geometry = useMemo( () => () => , [] ) const GoldGeometry = useMemo( () => () => , [] ) const Material = () => useMemo( () => ( ), [] ) const GoldMaterial = () => useMemo( () => ( ), [] ) return ( {particles?.map((user: any, index: number) => ( {config.showGold && Math.random() <= 0.5 ? ( <> ) : ( <> )} ))} ) } export default ParticlesCanvas