old.nicco.io/src/util.ts

57 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-03-03 16:13:57 +01:00
import { useEffect, useState } from 'react'
const getSize = () => {
const { innerHeight, innerWidth, outerHeight, outerWidth } = window
return { innerHeight, innerWidth, outerHeight, outerWidth }
}
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(() => {
const enter = ()=> setPresent(true)
const leave = ()=> setPresent(false)
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