Files
supabase/apps/www/components/LaunchWeek/8/AnimatedParticles/ParticlesCanvas.tsx
Francesco SansalvadoreandAnt Wilson accf7618eb Eagle (#22425)
* lw11 ga ticketing

* add @types/animejs

* build new game word

* og ticket layout

* ticket og layout

* lw11 ticketing flow

* remove unused assets

* clean up old LW code

* smol cleanup

* update lw11 og function

* update lw11 og function

* prepare

* main layout

* layout

* update naming

* fix lw11 canvas resize

* lw announcement countdown banner

* hp badge

* update lw page

* cleanup

* update banners

* update ticket shape

* update share string

* update launch time

* avatar in ticket

* update lw11-og handler

* mask ticket glow

* update game with input-otp

* clean up game

* update ticketing page title

* update

* update awards

* special event

* update constants

* fix mobile inputs bug

* update announcements

* dots grid

* remove 2024

* gst

* fix ticket user page

* less stacked layout

* lw

* update secret ticket

* update sharing layout

* max width to ticket container

* update tweet text

* clean up merge

* npm i

* update hero announcement

* update footer

* update types

* fix types

* Update apps/www/lib/constants.ts

* Update apps/www/lib/constants.ts

* Update apps/www/lib/constants.ts

* Apply suggestions from code review

* update og

* og

* special announcement

* special announcement

* fix favicon

* url timestamp for cache

* cache

* favicon

* remove file

* fix title

* game only when signed in

* user ticket nme

* day

* switch to /special-announcent

* update /special-announcement

* update /special-announcement

* skip build

* redirect

* fix theme in /special-announcement

* fix mobile ticket form

* image not draggable

* cleanup

* restore latest manifest

* update secret ticket tweet

* secret share

* update text

* fix secret share

* typo

* remove comment

* update deprecated icon

* restore input.stories

---------

Co-authored-by: Ant Wilson <awalias@users.noreply.github.com>
2024-04-04 13:09:21 +00:00

102 lines
2.6 KiB
TypeScript

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<boolean>(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(
() => () => <circleGeometry args={[config.particlesSize, config.particlesSides]} />,
[]
)
const GoldGeometry = useMemo(
() => () => <circleGeometry args={[config.goldParticlesSize, config.particlesSides]} />,
[]
)
const Material = () =>
useMemo(
() => (
<meshPhysicalMaterial
color={config.color}
blending={config.particlesBlending ? AdditiveBlending : undefined}
/>
),
[]
)
const GoldMaterial = () =>
useMemo(
() => (
<meshPhysicalMaterial
color={config.colorGold}
metalness={35}
blending={config.particlesBlending ? AdditiveBlending : undefined}
/>
),
[]
)
return (
<Canvas
ref={canvasRef}
dpr={[1, 2]}
camera={{ fov: 75, position: [0, 0, 500] }}
className="relative z-30"
>
<ambientLight intensity={config.lightIntensity} />
<group position={[0, 0, 0]} scale={[1, 1, 1]}>
{particles?.map((user: any, index: number) => (
<Particle
key={`particle-${user.username ?? index}`}
user={user}
config={config}
animate={animate}
>
{config.showGold && Math.random() <= 0.5 ? (
<>
<GoldGeometry />
<GoldMaterial />
</>
) : (
<>
<Geometry />
<Material />
</>
)}
</Particle>
))}
</group>
</Canvas>
)
}
export default ParticlesCanvas