-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer.js
56 lines (45 loc) · 1.53 KB
/
pointer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const setupPointer = () => {
const body = document.body
body.classList.add('customCursor')
window.addEventListener('contextmenu', event => {
event.preventDefault()
})
const pointerImage = document.querySelector('#pointer > img')
window.addEventListener('mousemove', () => {
pointerImage.classList.remove('hidden')
}, {once: true})
const pointer = document.getElementById('pointer')
const pointerMaxRotation = 25 // in degrees
const pointingHandPointerOffset = { // in pixels
x: 70,
y: 15
}
pointer.style.transformOrigin = `
${pointingHandPointerOffset.x}px
${pointingHandPointerOffset.y}px
`
window.addEventListener('mousemove', event => {
const viewportWidth = window.innerWidth
const {clientX: x, clientY: y} = event
const xFromCenter = ((x / viewportWidth) * 2) - 1 // within [-1, 1]
const translateX = `${x - pointingHandPointerOffset.x}px`
const translateY = `${y - pointingHandPointerOffset.y}px`
const rotation = `${xFromCenter * pointerMaxRotation}deg`
const vmin = Math.min(window.innerWidth, window.innerHeight)
const scaleFactor = vmin / 1440
pointer.style.transform = `
translate(${translateX}, ${translateY})
rotate(${rotation})
scale(${scaleFactor.toFixed(4)})
`
})
window.addEventListener('mousedown', () => {
pointerImage.src = "./assets/tapping-hand.webp"
})
window.addEventListener('mouseup', () => {
pointerImage.src = "./assets/pointing-hand.webp"
})
}
if (!('ontouchstart' in window)) {
setupPointer()
}