2019-03-03 16:13:57 +01:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
|
|
|
|
|
2019-03-06 00:17:11 +01:00
|
|
|
export const useIsMousePresent = () => {
|
2019-03-06 00:16:46 +01:00
|
|
|
let [present, setPresent] = useState<boolean>(false)
|
2019-03-03 16:13:57 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2019-03-06 00:17:11 +01:00
|
|
|
const enter = () => setPresent(true)
|
|
|
|
const leave = () => setPresent(false)
|
2019-03-03 16:13:57 +01:00
|
|
|
window.document.addEventListener('mouseenter', enter)
|
|
|
|
window.document.addEventListener('mouseleave', leave)
|
|
|
|
return () => {
|
|
|
|
window.document.removeEventListener('mouseenter', enter)
|
|
|
|
window.document.removeEventListener('mouseleave', leave)
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return present
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useMousePosition = () => {
|
|
|
|
let [position, setPosition] = useState({ absolute: { x: 0, y: 0 }, relative: { x: 0, y: 0 } })
|
|
|
|
|
|
|
|
const handle = (e: MouseEvent) => {
|
|
|
|
setPosition({
|
|
|
|
absolute: {
|
|
|
|
x: e.pageX,
|
|
|
|
y: e.pageY,
|
|
|
|
},
|
|
|
|
relative: {
|
|
|
|
x: e.pageX / window.innerWidth,
|
|
|
|
y: e.pageY / window.innerHeight,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('mousemove', handle)
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('mousemove', handle)
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return position
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Rand = (from: number = 0, to: number = 1, float: boolean = false) => {
|
|
|
|
const rand = (Math.random() * (to - from)) + from
|
|
|
|
return float ? rand : rand | 0
|
|
|
|
}
|
|
|
|
|
|
|
|
export const GoldenRatio: number = (1 + Math.sqrt(5)) / 2
|