@@ -195,6 +195,21 @@ function createUniforms(width: number, height: number) {
195195 } ;
196196}
197197
198+ function createRenderer ( canvas : HTMLCanvasElement , width : number , height : number , dpr = 2 ) {
199+ return new Renderer ( {
200+ alpha : true ,
201+ antialias : true ,
202+ canvas,
203+ depth : false ,
204+ dpr,
205+ height,
206+ powerPreference : "high-performance" ,
207+ premultipliedAlpha : true ,
208+ webgl : 2 ,
209+ width,
210+ } ) ;
211+ }
212+
198213export type EffectLayerProps = PropsWithChildren < {
199214 className ?: string ;
200215} > ;
@@ -203,6 +218,7 @@ export function EffectLayer({ children, className }: EffectLayerProps) {
203218 const rRaf = useRef < number > ( null ) ;
204219 const rRoot = useRef < HTMLDivElement > ( null ) ;
205220 const rCanvas = useRef < HTMLCanvasElement > ( null ) ;
221+ const isActive = useRef < boolean > ( false ) ;
206222
207223 useEffect ( ( ) => {
208224 if ( rRoot . current == null ) return ;
@@ -212,18 +228,7 @@ export function EffectLayer({ children, className }: EffectLayerProps) {
212228 const canvas = rCanvas . current ;
213229 const rect = root . getBoundingClientRect ( ) ;
214230 const uniforms = createUniforms ( rect . width , rect . height ) ;
215- const renderer = new Renderer ( {
216- alpha : true ,
217- antialias : false ,
218- canvas,
219- depth : false ,
220- dpr : window . devicePixelRatio ,
221- height : rect . height ,
222- powerPreference : "high-performance" ,
223- premultipliedAlpha : true ,
224- webgl : 2 ,
225- width : rect . width ,
226- } ) ;
231+ const renderer = createRenderer ( canvas , rect . width , rect . height , window . devicePixelRatio ) ;
227232
228233 const { gl } = renderer ;
229234 const geometry = new Triangle ( gl ) ;
@@ -236,27 +241,34 @@ export function EffectLayer({ children, className }: EffectLayerProps) {
236241 const mesh = new Mesh ( gl , { geometry, program } ) ;
237242
238243 function update ( time : number ) {
244+ if ( ! isActive . current ) return ;
239245 uniforms . uTime . value = time * 0.001 ;
240246 renderer . render ( { scene : mesh } ) ;
241247 rRaf . current = requestAnimationFrame ( update ) ;
242248 }
243249
250+ function resize ( rect : DOMRect ) {
251+ if ( ! isActive . current ) return ;
252+ uniforms . uResolution . value . set ( rect . width , rect . height ) ;
253+ renderer . setSize ( rect . width , rect . height ) ;
254+ renderer . render ( { scene : mesh } ) ;
255+ }
256+
244257 const ro = new ResizeObserver ( ( entries ) => {
245- for ( const entry of entries ) {
246- if ( entry . target !== root ) continue ;
247- const rect = entry . contentRect ;
248- uniforms . uResolution . value . set ( rect . width , rect . height ) ;
249- renderer . setSize ( rect . width , rect . height ) ;
250- renderer . render ( { scene : mesh } ) ;
251- return ;
252- }
258+ if ( ! isActive . current ) return ;
259+ if ( entries . length == null ) return ;
260+ requestAnimationFrame ( ( ) => {
261+ resize ( root . getBoundingClientRect ( ) ) ;
262+ } ) ;
253263 } ) ;
254264
255265 ro . observe ( root ) ;
256266 rRaf . current = requestAnimationFrame ( update ) ;
257267 canvas . style . opacity = "1" ;
268+ isActive . current = true ;
258269
259270 return ( ) => {
271+ isActive . current = false ;
260272 canvas . style . opacity = "0" ;
261273 if ( rRaf . current != null ) cancelAnimationFrame ( rRaf . current ) ;
262274 ro . disconnect ( ) ;
@@ -296,5 +308,6 @@ const styles = {
296308 "transition-opacity" ,
297309 "duration" ,
298310 "ease-[ease-in-out]" ,
311+ "pointer-events-none" ,
299312 ) ,
300313} ;
0 commit comments