Added throttle for performance issue
This commit is contained in:
@@ -52,28 +52,45 @@ import Layout from '../layouts/Layout.astro';
|
|||||||
</Layout>
|
</Layout>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const shape = document.getElementById('shape')
|
const shape = document.getElementById('shape');
|
||||||
const spotlight = document.getElementById('spotlight')
|
const spotlight = document.getElementById('spotlight');
|
||||||
|
|
||||||
if (shape || spotlight) {
|
/**
|
||||||
document.addEventListener('pointermove', (event) => {
|
*
|
||||||
const { clientX, clientY } = event;
|
* @param {PointerEvent} event - The event to pass the function
|
||||||
|
*/
|
||||||
|
function updateSpotlights(event) {
|
||||||
|
const { clientX, clientY } = event;
|
||||||
|
|
||||||
if (shape) {
|
if (shape) {
|
||||||
shape.animate({
|
shape.animate({
|
||||||
left: `${clientX}px`,
|
left: `${clientX}px`,
|
||||||
top: `${clientY}px`
|
top: `${clientY}px`
|
||||||
}, { duration: 2500, fill: "forwards" })
|
}, { duration: 2500, fill: "forwards" });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spotlight) {
|
if (spotlight) {
|
||||||
spotlight.animate({
|
spotlight.animate({
|
||||||
left: `calc(33% + ${clientX / 3}px)`,
|
left: `calc(33% + ${clientX / 3}px)`,
|
||||||
top: `calc(33% + ${clientY / 3}px)`
|
top: `calc(33% + ${clientY / 3}px)`
|
||||||
}, { duration: 2500, fill: "forwards" })
|
}, { duration: 2500, fill: "forwards", easing: "ease-in-out" });
|
||||||
}
|
}
|
||||||
}, { passive: true })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Throttles the animation to avoid performance issues
|
||||||
|
function throttle(cb, wait = 100) {
|
||||||
|
let timeout
|
||||||
|
return function(e) {
|
||||||
|
if (timeout) return;
|
||||||
|
timeout = setTimeout(() => (cb(e), timeout=undefined), wait)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('pointermove', throttle((event) => {
|
||||||
|
console.log("proced")
|
||||||
|
requestAnimationFrame(updateSpotlights.bind(updateSpotlights, event))
|
||||||
|
}));
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
@@ -112,4 +129,4 @@ if (shape || spotlight) {
|
|||||||
aspect-ratio: 1;
|
aspect-ratio: 1;
|
||||||
animation: shape-spin 13s infinite;
|
animation: shape-spin 13s infinite;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user