Added throttle for performance issue

This commit is contained in:
Alexis
2024-08-07 14:33:43 +02:00
parent cf561d2c88
commit afed5ce889

View File

@@ -52,28 +52,45 @@ import Layout from '../layouts/Layout.astro';
</Layout>
<script>
const shape = document.getElementById('shape')
const spotlight = document.getElementById('spotlight')
const shape = document.getElementById('shape');
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) {
shape.animate({
left: `${clientX}px`,
top: `${clientY}px`
}, { duration: 2500, fill: "forwards" })
}
if (shape) {
shape.animate({
left: `${clientX}px`,
top: `${clientY}px`
}, { duration: 2500, fill: "forwards" });
}
if (spotlight) {
spotlight.animate({
left: `calc(33% + ${clientX / 3}px)`,
top: `calc(33% + ${clientY / 3}px)`
}, { duration: 2500, fill: "forwards" })
}
}, { passive: true })
if (spotlight) {
spotlight.animate({
left: `calc(33% + ${clientX / 3}px)`,
top: `calc(33% + ${clientY / 3}px)`
}, { duration: 2500, fill: "forwards", easing: "ease-in-out" });
}
}
// 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>
<style lang="scss">
@@ -112,4 +129,4 @@ if (shape || spotlight) {
aspect-ratio: 1;
animation: shape-spin 13s infinite;
}
</style>
</style>