diff options
| author | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-07-24 13:10:02 +0200 |
|---|---|---|
| committer | Samlon <stankiewiczgracjan2019@gmail.com> | 2025-07-24 13:10:02 +0200 |
| commit | 42e1d00c76897819abceeba40f24cf8c4a81364f (patch) | |
| tree | fbab0ceb769d11f9609910bf1674737fceddf58a /js/gradient.js | |
| parent | add pictures (diff) | |
| download | site-42e1d00c76897819abceeba40f24cf8c4a81364f.tar.gz site-42e1d00c76897819abceeba40f24cf8c4a81364f.zip | |
add js to background
Diffstat (limited to '')
| -rw-r--r-- | js/gradient.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/js/gradient.js b/js/gradient.js new file mode 100644 index 0000000..1fe1e63 --- /dev/null +++ b/js/gradient.js | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | document.addEventListener('DOMContentLoaded', () => { | ||
| 2 | // Get the body element to update the cursor position | ||
| 3 | const body = document.documentElement; | ||
| 4 | let mouseX = 0; | ||
| 5 | let mouseY = 0; | ||
| 6 | let smoothX = 0; | ||
| 7 | let smoothY = 0; | ||
| 8 | |||
| 9 | // Smoothing factor (lower = smoother, higher = snappier) | ||
| 10 | const smoothing = 0.1; | ||
| 11 | |||
| 12 | // Handle mouse movement | ||
| 13 | const handleMouseMove = (e) => { | ||
| 14 | mouseX = e.clientX; | ||
| 15 | mouseY = e.clientY; | ||
| 16 | }; | ||
| 17 | |||
| 18 | // Smooth the cursor movement | ||
| 19 | const updateCursorLight = () => { | ||
| 20 | // Apply smoothing | ||
| 21 | smoothX += (mouseX - smoothX) * smoothing; | ||
| 22 | smoothY += (mouseY - smoothY) * smoothing; | ||
| 23 | |||
| 24 | // Update CSS variables for the cursor position | ||
| 25 | body.style.setProperty('--cursor-x', `${smoothX}px`); | ||
| 26 | body.style.setProperty('--cursor-y', `${smoothY}px`); | ||
| 27 | |||
| 28 | // Continue the animation loop | ||
| 29 | requestAnimationFrame(updateCursorLight); | ||
| 30 | }; | ||
| 31 | |||
| 32 | // Add event listeners | ||
| 33 | window.addEventListener('mousemove', handleMouseMove); | ||
| 34 | |||
| 35 | // Handle touch events for mobile | ||
| 36 | window.addEventListener('touchmove', (e) => { | ||
| 37 | e.preventDefault(); | ||
| 38 | const touch = e.touches[0]; | ||
| 39 | handleMouseMove({ | ||
| 40 | clientX: touch.clientX, | ||
| 41 | clientY: touch.clientY | ||
| 42 | }); | ||
| 43 | }, { passive: false }); | ||
| 44 | |||
| 45 | // Initialize cursor position to center of screen | ||
| 46 | mouseX = window.innerWidth / 2; | ||
| 47 | mouseY = window.innerHeight / 2; | ||
| 48 | smoothX = mouseX; | ||
| 49 | smoothY = mouseY; | ||
| 50 | |||
| 51 | // Start the animation loop | ||
| 52 | updateCursorLight(); | ||
| 53 | }); | ||
