-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoyStick.js
89 lines (80 loc) · 3.23 KB
/
JoyStick.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
class JoyStick{
constructor(options){
const circle = document.createElement("div");
circle.style.cssText = "position:absolute; bottom:35px; width:80px; height:80px; background:rgba(126, 126, 126, 0.5); border:#444 solid medium; border-radius:50%; left:50%; transform:translateX(-50%);";
const thumb = document.createElement("div");
thumb.style.cssText = "position: absolute; left: 20px; top: 20px; width: 40px; height: 40px; border-radius: 50%; background: #fff;";
circle.appendChild(thumb);
document.body.appendChild(circle);
this.domElement = thumb;
this.maxRadius = options.maxRadius || 40;
this.maxRadiusSquared = this.maxRadius * this.maxRadius;
this.onMove = options.onMove;
this.app = options.app;
this.origin = { left:this.domElement.offsetLeft, top:this.domElement.offsetTop };
this.rotationDamping = options.rotationDamping || 0.06;
this.moveDamping = options.moveDamping || 0.01;
if (this.domElement!=undefined){
const joystick = this;
if ('ontouchstart' in window){
this.domElement.addEventListener('touchstart', function(evt){ evt.preventDefault(); joystick.tap(evt); });
}else{
this.domElement.addEventListener('mousedown', function(evt){ evt.preventDefault(); joystick.tap(evt); });
}
}
}
getMousePosition(evt){
let clientX = evt.targetTouches ? evt.targetTouches[0].pageX : evt.clientX;
let clientY = evt.targetTouches ? evt.targetTouches[0].pageY : evt.clientY;
return { x:clientX, y:clientY };
}
tap(evt){
evt = evt || window.event;
// get the mouse cursor position at startup:
this.offset = this.getMousePosition(evt);
const joystick = this;
if ('ontouchstart' in window){
document.ontouchmove = function(evt){ evt.preventDefault(); joystick.move(evt); };
document.ontouchend = function(evt){ evt.preventDefault(); joystick.up(evt); };
}else{
document.onmousemove = function(evt){ evt.preventDefault(); joystick.move(evt); };
document.onmouseup = function(evt){ evt.preventDefault(); joystick.up(evt); };
}
}
move(evt){
evt = evt || window.event;
const mouse = this.getMousePosition(evt);
// calculate the new cursor position:
let left = mouse.x - this.offset.x;
let top = mouse.y - this.offset.y;
//this.offset = mouse;
const sqMag = left*left + top*top;
if (sqMag>this.maxRadiusSquared){
//Only use sqrt if essential
const magnitude = Math.sqrt(sqMag);
left /= magnitude;
top /= magnitude;
left *= this.maxRadius;
top *= this.maxRadius;
}
// set the element's new position:
this.domElement.style.top = `${top + this.domElement.clientHeight/2}px`;
this.domElement.style.left = `${left + this.domElement.clientWidth/2}px`;
const forward = -(top - this.origin.top + this.domElement.clientHeight/2)/this.maxRadius;
const turn = (left - this.origin.left + this.domElement.clientWidth/2)/this.maxRadius;
if (this.onMove!=undefined) this.onMove.call(this.app, forward, turn);
}
up(evt){
if ('ontouchstart' in window){
document.ontouchmove = null;
document.touchend = null;
}else{
document.onmousemove = null;
document.onmouseup = null;
}
this.domElement.style.top = `${this.origin.top}px`;
this.domElement.style.left = `${this.origin.left}px`;
this.onMove.call(this.app, 0, 0);
}
}
export { JoyStick };