-
Notifications
You must be signed in to change notification settings - Fork 6
feat(site): lead the hero with the desktop app and add a cursor-aware particle field #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f4d5012
feat(site): lead the hero with the desktop app and add a cursor-aware…
elkaix 14e6eda
fix(site): size the particle canvas to the viewport
elkaix a4411f1
Merge branch 'main' into feat/site-desktop-first
elkaix b3bbd28
Merge branch 'main' into feat/site-desktop-first
elkaix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| <script setup> | ||
| import { nextTick, onMounted, onUnmounted, ref } from 'vue'; | ||
|
|
||
| const canvas = ref(null); | ||
| const enabled = ref(false); | ||
|
|
||
| let context; | ||
| let animationFrame; | ||
| let particles = []; | ||
| let viewportWidth = 0; | ||
| let viewportHeight = 0; | ||
| let mounted = false; | ||
|
|
||
| const pointer = { x: Infinity, y: Infinity }; | ||
| const pointerRadius = 120; | ||
|
|
||
| function createParticles() { | ||
| const count = Math.max(40, Math.min(90, Math.round((viewportWidth * viewportHeight) / 16000))); | ||
| particles = Array.from({ length: count }, () => { | ||
| const homeX = Math.random() * viewportWidth; | ||
| const homeY = Math.random() * viewportHeight; | ||
| return { | ||
| homeX, | ||
| homeY, | ||
| x: homeX, | ||
| y: homeY, | ||
| velocityX: 0, | ||
| velocityY: 0, | ||
| driftX: (Math.random() - 0.5) * 0.08, | ||
| driftY: (Math.random() - 0.5) * 0.08, | ||
| radius: 0.8 + Math.random(), | ||
| color: Math.random() < 0.06 ? 'rgba(43, 137, 255, 0.35)' : 'rgba(17, 17, 19, 0.22)', | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| function resize() { | ||
| viewportWidth = window.innerWidth; | ||
| viewportHeight = window.innerHeight; | ||
| const pixelRatio = Math.min(window.devicePixelRatio || 1, 2); | ||
|
|
||
| context.setTransform(1, 0, 0, 1, 0, 0); | ||
| canvas.value.width = Math.round(viewportWidth * pixelRatio); | ||
| canvas.value.height = Math.round(viewportHeight * pixelRatio); | ||
| context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0); | ||
| createParticles(); | ||
| } | ||
|
|
||
| function moveParticles() { | ||
| for (const particle of particles) { | ||
| particle.homeX += particle.driftX; | ||
| particle.homeY += particle.driftY; | ||
| if (particle.homeX < 0 || particle.homeX > viewportWidth) particle.driftX *= -1; | ||
| if (particle.homeY < 0 || particle.homeY > viewportHeight) particle.driftY *= -1; | ||
|
|
||
| const deltaX = particle.x - pointer.x; | ||
| const deltaY = particle.y - pointer.y; | ||
| const distance = Math.hypot(deltaX, deltaY); | ||
|
|
||
| if (distance < pointerRadius) { | ||
| const force = ((pointerRadius - distance) / pointerRadius) * 0.8; | ||
| const safeDistance = Math.max(distance, 0.01); | ||
| particle.velocityX += (deltaX / safeDistance) * force; | ||
| particle.velocityY += (deltaY / safeDistance) * force; | ||
| } else { | ||
| particle.velocityX += (particle.homeX - particle.x) * 0.008; | ||
| particle.velocityY += (particle.homeY - particle.y) * 0.008; | ||
| } | ||
|
|
||
| particle.velocityX *= 0.92; | ||
| particle.velocityY *= 0.92; | ||
| particle.x += particle.velocityX; | ||
| particle.y += particle.velocityY; | ||
| } | ||
| } | ||
|
|
||
| function drawParticles() { | ||
| context.clearRect(0, 0, viewportWidth, viewportHeight); | ||
| moveParticles(); | ||
|
|
||
| for (const particle of particles) { | ||
| context.beginPath(); | ||
| context.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2); | ||
| context.fillStyle = particle.color; | ||
| context.fill(); | ||
| } | ||
| } | ||
|
|
||
| function animate() { | ||
| drawParticles(); | ||
| animationFrame = window.requestAnimationFrame(animate); | ||
| } | ||
|
|
||
| function startAnimation() { | ||
| if (animationFrame !== undefined || document.hidden) return; | ||
| animationFrame = window.requestAnimationFrame(animate); | ||
| } | ||
|
|
||
| function stopAnimation() { | ||
| if (animationFrame === undefined) return; | ||
| window.cancelAnimationFrame(animationFrame); | ||
| animationFrame = undefined; | ||
| } | ||
|
|
||
| function onPointerMove(event) { | ||
| pointer.x = event.clientX; | ||
| pointer.y = event.clientY; | ||
| } | ||
|
|
||
| function onVisibilityChange() { | ||
| if (document.hidden) stopAnimation(); | ||
| else startAnimation(); | ||
| } | ||
|
|
||
| onMounted(async () => { | ||
| mounted = true; | ||
| if ( | ||
| window.matchMedia('(prefers-reduced-motion: reduce)').matches | ||
| || !window.matchMedia('(pointer: fine)').matches | ||
| ) return; | ||
|
|
||
| enabled.value = true; | ||
| await nextTick(); | ||
| if (!mounted) return; | ||
|
|
||
| context = canvas.value?.getContext('2d'); | ||
| if (!context) return; | ||
|
|
||
| resize(); | ||
| window.addEventListener('pointermove', onPointerMove, { passive: true }); | ||
| window.addEventListener('resize', resize); | ||
| document.addEventListener('visibilitychange', onVisibilityChange); | ||
| startAnimation(); | ||
| }); | ||
|
|
||
| onUnmounted(() => { | ||
| mounted = false; | ||
| stopAnimation(); | ||
| window.removeEventListener('pointermove', onPointerMove); | ||
| window.removeEventListener('resize', resize); | ||
| document.removeEventListener('visibilitychange', onVisibilityChange); | ||
| }); | ||
| </script> | ||
|
|
||
| <template> | ||
| <canvas v-if="enabled" ref="canvas" class="particle-field" aria-hidden="true"></canvas> | ||
| </template> | ||
|
|
||
| <style scoped> | ||
| .particle-field { | ||
| position: fixed; | ||
| z-index: -1; | ||
| inset: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
| pointer-events: none; | ||
| } | ||
| </style> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.