From bddb1f1887915af88c352245305d59ded6668947 Mon Sep 17 00:00:00 2001 From: VDawg Date: Tue, 14 Nov 2023 11:04:47 +0100 Subject: [PATCH] improve pattern mouse follow --- .eslintrc.cjs | 3 ++- src/lib/PatternBackground.svelte | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 44c93ca..8201ed7 100755 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -14,5 +14,6 @@ module.exports = { rules: { 'no-unused-vars': ['off', { varsIgnorePattern: '.*' }], 'svelte/no-at-html-tags': 'off' - } + }, + globals: { globalThis: true } } diff --git a/src/lib/PatternBackground.svelte b/src/lib/PatternBackground.svelte index bdfb665..dfe65f4 100644 --- a/src/lib/PatternBackground.svelte +++ b/src/lib/PatternBackground.svelte @@ -60,6 +60,12 @@ hasJustMounted = false }) + $: { + if (!$isMouseOver$) { + globalThis.document?.removeEventListener('mousemove', track) + } + } + function resizeGradient() { if (hasJustMounted || !isMouseOver$) return @@ -69,7 +75,7 @@ function startTrackingMouse() { if ($isMouseOver$) return - document.addEventListener('mousemove', track) + globalThis.document?.addEventListener('mousemove', track) } function track({ clientX, clientY }) { @@ -77,7 +83,7 @@ } onDestroy(() => { - document.removeEventListener('mousemove', track) + globalThis.document?.removeEventListener('mousemove', track) })