-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
169 lines (135 loc) · 4.8 KB
/
index.html
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html>
<head>
<title>Joystick Controls</title>
<style>
body
{
font-family: Courier, monospaced;
font-size: 16px;
font-weight: bold;
}
</style>
</head>
<body>
Joystick Controls.
<hr>
<div id="status1" style="color: red;">Joystick 1</div>
<div id="status2" style="color: blue;">Joystick 2</div>
<hr>
<div style="border: 1px solid red; width: 128px; position: absolute; left:10px; top:150px;">
<img src="images/joystick-base.png"/>
<div id="stick1" style="position: absolute; left:32px; top:32px;">
<img src="images/joystick-red.png"/>
</div>
</div>
<div style="border: 1px solid blue; width: 128px; position: absolute; left:210px; top:150px;">
<img src="images/joystick-base.png"/>
<div id="stick2" style="position: absolute; left:32px; top:32px;">
<img src="images/joystick-blue.png"/>
</div>
</div>
<script>
class JoystickController
{
// stickID: ID of HTML element (representing joystick) that will be dragged
// maxDistance: maximum amount joystick can move in any direction
// deadzone: joystick must move at least this amount from origin to register value change
constructor( stickID, maxDistance, deadzone )
{
this.id = stickID;
let stick = document.getElementById(stickID);
// location from which drag begins, used to calculate offsets
this.dragStart = null;
// track touch identifier in case multiple joysticks present
this.touchId = null;
this.active = false;
this.value = { x: 0, y: 0 };
let self = this;
function handleDown(event)
{
self.active = true;
// all drag movements are instantaneous
stick.style.transition = '0s';
// touch event fired before mouse event; prevent redundant mouse event from firing
event.preventDefault();
if (event.changedTouches)
self.dragStart = { x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY };
else
self.dragStart = { x: event.clientX, y: event.clientY };
// if this is a touch event, keep track of which one
if (event.changedTouches)
self.touchId = event.changedTouches[0].identifier;
}
function handleMove(event)
{
if ( !self.active ) return;
// if this is a touch event, make sure it is the right one
// also handle multiple simultaneous touchmove events
let touchmoveId = null;
if (event.changedTouches)
{
for (let i = 0; i < event.changedTouches.length; i++)
{
if (self.touchId == event.changedTouches[i].identifier)
{
touchmoveId = i;
event.clientX = event.changedTouches[i].clientX;
event.clientY = event.changedTouches[i].clientY;
}
}
if (touchmoveId == null) return;
}
const xDiff = event.clientX - self.dragStart.x;
const yDiff = event.clientY - self.dragStart.y;
const angle = Math.atan2(yDiff, xDiff);
const distance = Math.min(maxDistance, Math.hypot(xDiff, yDiff));
const xPosition = distance * Math.cos(angle);
const yPosition = distance * Math.sin(angle);
// move stick image to new position
stick.style.transform = `translate3d(${xPosition}px, ${yPosition}px, 0px)`;
// deadzone adjustment
const distance2 = (distance < deadzone) ? 0 : maxDistance / (maxDistance - deadzone) * (distance - deadzone);
const xPosition2 = distance2 * Math.cos(angle);
const yPosition2 = distance2 * Math.sin(angle);
const xPercent = parseFloat((xPosition2 / maxDistance).toFixed(4));
const yPercent = parseFloat((yPosition2 / maxDistance).toFixed(4));
self.value = { x: xPercent, y: yPercent };
}
function handleUp(event)
{
if ( !self.active ) return;
// if this is a touch event, make sure it is the right one
if (event.changedTouches && self.touchId != event.changedTouches[0].identifier) return;
// transition the joystick position back to center
stick.style.transition = '.2s';
stick.style.transform = `translate3d(0px, 0px, 0px)`;
// reset everything
self.value = { x: 0, y: 0 };
self.touchId = null;
self.active = false;
}
stick.addEventListener('mousedown', handleDown);
stick.addEventListener('touchstart', handleDown);
document.addEventListener('mousemove', handleMove, {passive: false});
document.addEventListener('touchmove', handleMove, {passive: false});
document.addEventListener('mouseup', handleUp);
document.addEventListener('touchend', handleUp);
}
}
let joystick1 = new JoystickController("stick1", 64, 8);
let joystick2 = new JoystickController("stick2", 64, 8);
function update()
{
document.getElementById("status1").innerText = "Joystick 1: " + JSON.stringify(joystick1.value);
document.getElementById("status2").innerText = "Joystick 2: " + JSON.stringify(joystick2.value);
}
function loop()
{
requestAnimationFrame(loop);
update();
}
loop();
</script>
</body>
</html>