@@ -45,19 +45,48 @@ export function WebGLBackground() {
45
45
let mouseX = 0 ;
46
46
let mouseY = 0 ;
47
47
48
+ // Scroll tracking
49
+ let lastScrollTop = 0 ;
50
+ let scrollDirection = 0 ;
51
+ let scrollVelocity = 0 ;
52
+ const scrollFactor = 0.0001 ; // Controls how much scroll affects particles
53
+
48
54
const handleMouseMove = ( event : MouseEvent ) => {
49
55
mouseX = event . clientX / window . innerWidth - 0.5 ;
50
56
mouseY = event . clientY / window . innerHeight - 0.5 ;
51
57
} ;
52
58
59
+ const handleScroll = ( ) => {
60
+ // Get current scroll position
61
+ const st = window . pageYOffset || document . documentElement . scrollTop ;
62
+
63
+ // Determine scroll direction and velocity
64
+ scrollDirection = st > lastScrollTop ? 1 : - 1 ;
65
+ scrollVelocity = Math . abs ( st - lastScrollTop ) * scrollFactor ;
66
+
67
+ // Remember the last scroll position
68
+ lastScrollTop = st <= 0 ? 0 : st ;
69
+ } ;
70
+
53
71
window . addEventListener ( 'mousemove' , handleMouseMove ) ;
72
+ window . addEventListener ( 'scroll' , handleScroll ) ;
54
73
55
74
// Animation
56
75
const animate = ( ) => {
57
76
requestAnimationFrame ( animate ) ;
58
77
78
+ // Base rotation
59
79
particlesMesh . rotation . y += 0.0003 ;
60
80
particlesMesh . rotation . x += 0.0003 ;
81
+
82
+ // Apply scroll effect
83
+ if ( scrollDirection !== 0 && scrollVelocity > 0 ) {
84
+ // Move particles based on scroll direction
85
+ particlesMesh . position . y += scrollDirection * scrollVelocity ;
86
+
87
+ // Optional: gradually reset scroll velocity for smoother effect
88
+ scrollVelocity *= 0.95 ;
89
+ }
61
90
62
91
// Smooth camera movement based on mouse position
63
92
camera . position . x += ( mouseX * 0.5 - camera . position . x ) * 0.05 ;
@@ -81,6 +110,7 @@ export function WebGLBackground() {
81
110
return ( ) => {
82
111
window . removeEventListener ( 'mousemove' , handleMouseMove ) ;
83
112
window . removeEventListener ( 'resize' , handleResize ) ;
113
+ window . removeEventListener ( 'scroll' , handleScroll ) ;
84
114
containerRef . current ?. removeChild ( renderer . domElement ) ;
85
115
particlesGeometry . dispose ( ) ;
86
116
particlesMaterial . dispose ( ) ;
0 commit comments