-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.js
113 lines (93 loc) · 3.38 KB
/
mouse.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function initMouse()
{
mouse = {};
mouse.position = new THREE.Vector3();
mouse.oldPosition = new THREE.Vector3();
mouse.delta = new THREE.Vector3();
mouse.clicking = false;
mouse.oldClicking = false;
var asynchronous = {
position: new THREE.Vector3(),
clicking: false,
}
mouse.updateFromAsync = function() {
mouse.oldClicking = mouse.clicking;
mouse.clicking = asynchronous.clicking;
if(mouse.clicking && !mouse.oldClicking)
onMouseClickFunctions.forEach(f => f())
mouse.oldPosition.copy(mouse.position);
mouse.position.copy(asynchronous.position);
mouse.delta.subVectors(mouse.position, mouse.oldPosition)
}
// let a = new THREE.Mesh(new THREE.SphereGeometry(.5))
// scene.add(a)
//We assume that you are looking directly at the xy plane, and that the renderer is the view dimensions
asynchronous.updateFromClientCoordinates = function(rawX,rawY)
{
//center
asynchronous.position.x = rawX - ( renderer.domElement.width / window.devicePixelRatio / 2. )
asynchronous.position.y = -rawY + ( renderer.domElement.height / window.devicePixelRatio / 2. )
//scale
asynchronous.position.x /= renderer.domElement.width / window.devicePixelRatio / 2.
asynchronous.position.y /= renderer.domElement.height / window.devicePixelRatio / 2.
if (camera.rotation.z !== 0.) {
let temp = asynchronous.position.y
asynchronous.position.y = asynchronous.position.x
asynchronous.position.x = -temp
}
var centerToFrameVertical = (camera.getTop() - camera.getBottom()) / 2.
var centerToFrameHorizontal = centerToFrameVertical * camera.aspect
asynchronous.position.x *= centerToFrameHorizontal
asynchronous.position.y *= centerToFrameVertical
// a.position.copy(asynchronous.position)
// a.position.z = 0.
// log(asynchronous.position)
}
{
function onMouseOrFingerDown(event) {
// event.preventDefault();
asynchronous.clicking = true;
let pos = event.changedTouches ? event.changedTouches[0] : event
asynchronous.updateFromClientCoordinates(pos.clientX, pos.clientY)
mouse.updateFromAsync()
let highestR = null
let highestZ = -Infinity
for (let i = 0; i < rectangles.length; ++i) {
let r = rectangles[i]
if (r.onClick !== undefined && r.mouseInside() && r.visible && r.position.z > highestZ) {
highestR = r
highestZ = r.position.z
}
}
if (highestR !== null && highestR.onClick !== undefined) {
let nameOfSoundToPlay = highestR.onClick()
}
}
function forTouch(event) {
document.removeEventListener('mousedown',forMouse)
onMouseOrFingerDown(event)
}
function forMouse(event) {
document.removeEventListener('touchstart',forTouch)
onMouseOrFingerDown(event)
}
document.addEventListener('touchstart', forTouch)
document.addEventListener('mousedown', forMouse)
document.addEventListener('mouseup', function (event) {
asynchronous.clicking = false;
// event.preventDefault();
})
document.addEventListener( 'touchend', function(event) {
asynchronous.clicking = false;
// event.preventDefault();
})
document.addEventListener('mousemove', function (event) {
asynchronous.updateFromClientCoordinates(event.clientX, event.clientY)
// event.preventDefault();
})
document.addEventListener( 'touchmove', function( event ) {
asynchronous.updateFromClientCoordinates(event.changedTouches[0].clientX,event.changedTouches[0].clientY)
// event.preventDefault();
})
}
}