mirror of
https://github.com/hyprwm/hyprland-website.git
synced 2025-01-09 09:59:48 +01:00
improve patternbackground mouse follow
This commit is contained in:
parent
4fca49f8ad
commit
cfbacc3356
2 changed files with 26 additions and 20 deletions
|
@ -101,16 +101,3 @@ export function getBlurredPath(path) {
|
||||||
export function getRandom(array) {
|
export function getRandom(array) {
|
||||||
return array.at(Math.floor(Math.random() * array.length))
|
return array.at(Math.floor(Math.random() * array.length))
|
||||||
}
|
}
|
||||||
|
|
||||||
const fps = 1000 / 60 // 60 frames per second
|
|
||||||
export const mousePosition$ = new Observable((subscriber) => {
|
|
||||||
// eslint-disable-next-line no-undef
|
|
||||||
if (globalThis.document === undefined) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
document.addEventListener('mousemove', nextPostion)
|
|
||||||
function nextPostion({ clientX, clientY }) {
|
|
||||||
subscriber.next({ clientX, clientY })
|
|
||||||
}
|
|
||||||
return () => document?.removeEventListener('mousemove', nextPostion)
|
|
||||||
}).pipe(throttleTime(fps), share(), startWith({ clientX: 0, clientY: 0 }))
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<script>
|
<script>
|
||||||
import {
|
import {
|
||||||
BehaviorSubject,
|
BehaviorSubject,
|
||||||
|
Subject,
|
||||||
combineLatest,
|
combineLatest,
|
||||||
debounceTime,
|
debounceTime,
|
||||||
delay,
|
delay,
|
||||||
|
@ -14,7 +15,6 @@
|
||||||
} from 'rxjs'
|
} from 'rxjs'
|
||||||
import { onDestroy, onMount } from 'svelte'
|
import { onDestroy, onMount } from 'svelte'
|
||||||
import { spring } from 'svelte/motion'
|
import { spring } from 'svelte/motion'
|
||||||
import { mousePosition$ } from './Helper.mjs'
|
|
||||||
|
|
||||||
/** The start position of the gradient. */
|
/** The start position of the gradient. */
|
||||||
export let startPosition = [-1000, -1000]
|
export let startPosition = [-1000, -1000]
|
||||||
|
@ -23,10 +23,8 @@
|
||||||
let wrapperElement = undefined
|
let wrapperElement = undefined
|
||||||
/** @type {import('rxjs').BehaviorSubject<boolean>}*/
|
/** @type {import('rxjs').BehaviorSubject<boolean>}*/
|
||||||
const isMouseOver$ = new BehaviorSubject(false).pipe(
|
const isMouseOver$ = new BehaviorSubject(false).pipe(
|
||||||
switchMap((isTrue) => {
|
// Do not harshly stop updating the gradient when the mouse leaves, but wait a bit
|
||||||
// Prevent elements over the background to disable the movement of the gradient, as the mouse will not be over the background anymore
|
switchMap((isTrue) => (isTrue ? of(isTrue) : timer(1500).pipe(map(() => false)))),
|
||||||
return isTrue ? of(isTrue) : timer(5500).pipe(map(() => false))
|
|
||||||
}),
|
|
||||||
distinctUntilChanged()
|
distinctUntilChanged()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,6 +35,8 @@
|
||||||
map(() => wrapperElement.getBoundingClientRect().width * 3),
|
map(() => wrapperElement.getBoundingClientRect().width * 3),
|
||||||
startWith(800)
|
startWith(800)
|
||||||
)
|
)
|
||||||
|
/** @type {import('rxjs').Subject< {clientX: number, clientY: number} >}*/
|
||||||
|
const mousePosition$ = new Subject()
|
||||||
|
|
||||||
const gradientPosition$ = combineLatest([mousePosition$, gradientSize$, isMouseOver$]).pipe(
|
const gradientPosition$ = combineLatest([mousePosition$, gradientSize$, isMouseOver$]).pipe(
|
||||||
filter(([_, __, isMouseOver]) => isMouseOver),
|
filter(([_, __, isMouseOver]) => isMouseOver),
|
||||||
|
@ -65,14 +65,33 @@
|
||||||
|
|
||||||
gradientSize$.next()
|
gradientSize$.next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function startTrackingMouse() {
|
||||||
|
if ($isMouseOver$) return
|
||||||
|
|
||||||
|
document.addEventListener('mousemove', track)
|
||||||
|
}
|
||||||
|
|
||||||
|
function track({ clientX, clientY }) {
|
||||||
|
mousePosition$.next({ clientX, clientY })
|
||||||
|
}
|
||||||
|
|
||||||
|
onDestroy(() => {
|
||||||
|
document.removeEventListener('mousemove', track)
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:window on:resize={resizeGradient} />
|
<svelte:window on:resize={resizeGradient} />
|
||||||
|
|
||||||
<div
|
<div
|
||||||
class={$$props.class + ' wrapper'}
|
class={$$props.class + ' wrapper'}
|
||||||
on:mouseleave={() => isMouseOver$.next(false)}
|
on:mouseenter={startTrackingMouse}
|
||||||
on:mousemove={() => isMouseOver$.next(true)}
|
on:mouseleave={({ clientX, clientY, currentTarget }) => {
|
||||||
|
const { x, width, y, height } = currentTarget.getBoundingClientRect()
|
||||||
|
const isMouseStillOver =
|
||||||
|
x <= clientX && y <= clientY && x + width > clientX && y + height > clientY
|
||||||
|
isMouseOver$.next(isMouseStillOver)
|
||||||
|
}}
|
||||||
aria-hidden
|
aria-hidden
|
||||||
bind:this={wrapperElement}
|
bind:this={wrapperElement}
|
||||||
>
|
>
|
||||||
|
|
Loading…
Reference in a new issue