-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
188 lines (133 loc) · 6.04 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE HTML>
<html>
<head>
<script src="https://code.createjs.com/easeljs-0.8.1.min.js"></script>
<script>
var connection
var joystickMaxRadius = 75;
function init() {
var debug = document.getElementById("debug");
//websocket setup
var nickname = "test"
connection = new WebSocket("ws://" + window.location.hostname + ":8001")
connection.onopen = function() {
console.log("Connection opened")
connection.send(nickname)
}
connection.onclose = function() {
console.log("Connection closed")
}
connection.onerror = function() {
console.error("Connection error")
}
connection.onmessage = function(event) {
//var div = document.createElement("div")
//div.textContent = event.data
//document.body.appendChild(div)
}
//canvas
var stage = new createjs.Stage("myCanvas");
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
//load image and slap it on the canvas
var imageObj = new Image();
var logo = new createjs.Bitmap(imageObj);
stage.addChild(logo);
stage.update();
imageObj.onload = function() {
stage.update();
//context.drawImage(imageObj, 69, 50);
};
//ahmad, change this part.
imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
//the joystick images
var outerCircle = new createjs.Shape();
outerCircle.graphics.beginFill("rgba(100,50,0,0.4)").drawCircle(0, 0, joystickMaxRadius);
var innerCircle = new createjs.Shape();
innerCircle.graphics.beginFill("rgba(100,50,200,0.4)").drawCircle(0, 0, 25);
//mouse events
canvas.addEventListener("mousedown", mouseDownListener, false);
canvas.addEventListener("touchstart", mouseDownListener, false);
function mouseDownListener(evt) {
//getting mouse position correctly, being mindful of resizing that may have occured in the browser:
var bRect = canvas.getBoundingClientRect();
mouseX = evt.clientX || evt.touches[0].clientX;
mouseY = evt.clientY || evt.touches[0].clientY;
mouseX = (mouseX- bRect.left) * (canvas.width / bRect.width);
mouseY = (mouseY - bRect.top) * (canvas.height / bRect.height);
canvas.removeEventListener("mousedown", mouseDownListener, false);
canvas.removeEventListener("touchstart", mouseDownListener, false);
window.addEventListener("mousemove", mouseMoveListener, false);
window.addEventListener("touchmove", mouseMoveListener, false);
window.addEventListener("mouseup", mouseUpListener, false);
window.addEventListener("touchend", mouseUpListener, false);
outerCircle.x = innerCircle.x = mouseX;
outerCircle.y = innerCircle.y = mouseY;
stage.addChild(outerCircle);
stage.addChild(innerCircle);
stage.update();
//code below prevents the mouse down from having an effect on the main browser window:
if (evt.preventDefault) {
evt.preventDefault();
} //standard
else if (evt.returnValue) {
evt.returnValue = false;
} //older IE
return false;
}
function mouseUpListener(evt) {
canvas.addEventListener("mousedown", mouseDownListener, false);
canvas.addEventListener("touchstart", mouseDownListener, false);
window.removeEventListener("mouseup", mouseUpListener, false);
window.removeEventListener("mousemove", mouseMoveListener, false);
window.removeEventListener("touchmove", mouseMoveListener, false);
window.removeEventListener("touchend", mouseUpListener, false);
stage.removeChild(outerCircle);
stage.removeChild(innerCircle);
stage.update();
}
function mouseMoveListener(evt) {
var bRect = canvas.getBoundingClientRect();
mouseX = evt.clientX || evt.touches[0].clientX;
mouseY = evt.clientY || evt.touches[0].clientY;
mouseX = (mouseX- bRect.left) * (canvas.width / bRect.width);
mouseY = (mouseY - bRect.top) * (canvas.height / bRect.height);
//get distance from initial click. thanks pythagoras you asshole
var distanceX = mouseX - outerCircle.x;
var distanceY = mouseY - outerCircle.y;
angle = Math.atan2(distanceY, distanceX); //Remember, this is radians
var distance = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
if(distance > joystickMaxRadius){
distanceX = Math.cos(angle) * joystickMaxRadius;
distanceY = Math.sin(angle) * joystickMaxRadius;
}
var speedX = parseInt((distanceX/joystickMaxRadius)* 100, 10);
var speedY = parseInt((distanceY/joystickMaxRadius)* 100, 10);
/*
//perhaps it's better to send commands instead of coordinates to server?
//something like LEFT:ON, RIGHT:OFF, etc.
this.cursors.up = (deltaY < 0);
this.cursors.down = (deltaY > 0);
this.cursors.left = (deltaX < 0);
this.cursors.right = (deltaX > 0);
*/
debug.textContent = "Distance: " + distance + " Angle: " + angle + " Speed: " + speedX + ", " + speedY;
innerCircle.x = speedX + outerCircle.x;
innerCircle.y = speedY + outerCircle.y;
stage.update();
connection.send(speedX + ", " + speedY);
}
}
</script>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body onload="init();">
<canvas id="myCanvas" width="800" height="600"></canvas>
<div id="debug"></div>
</body>
</html>