-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-joystick.js
More file actions
291 lines (272 loc) · 9.8 KB
/
angular-joystick.js
File metadata and controls
291 lines (272 loc) · 9.8 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict';
/*
* Joystick Directive for AngularJS.
* A fork of Scott Lobdell's joystick initially for backbone (https://github.com/slobdell/joystick).
*/
/**
* @namespace angular-joystick
*/
angular.module('angular-joystick', [
]);
'use strict';
/*
* Joystick Directive for AngularJS.
* A fork of Scott Lobdell's joystick initially for backbone (https://github.com/slobdell/joystick).
*/
angular.module('angular-joystick').directive('angularJoystick', function(JoystickService) {
return {
restrict: 'EA',
scope: {
coords: '=',
onMove: '&'
},
template: '<div '+
//'ng-mousedown="joystick.startControl()" ' +
//'ng-mouseup="joystick.endControl()" ' +
//'ng-mousemove="joystick.move($event)" ' +
'ng-touchstart="joystick.startControl()" ' +
'ng-touchend="joystick.endControl()" ' +
'ng-touchmove="joystick.move($event)" ' +
//'hmPanstart"="joystick.startControl()" ' +
//'hmPanmove"="joystick.move($event)" ' +
//'hmPanend"="joystick.endCotrol()" ' +
'><canvas id="joystickCanvas" ' +
//'width="100%" height="100%" style="width: 100%; height: 100%;"></canvas>'+
'width="{{joystick.squareSize}}" height="{{joystick.squareSize}}" style="width: {{joystick.squareSize}}px; height: {{joystick.squareSize}}px;"></canvas>'+
'</div>',
controller: ['$scope',
function($scope) {
$scope.joystick = JoystickService.get($scope.coords, $scope.onMove);
$scope.$on('$destroy', function () {
$scope.joystick = {};
});
}
]
};
});
'use strict';
/*
* Joystick Directive for AngularJS.
* A fork of Scott Lobdell's joystick initially for backbone (https://github.com/slobdell/joystick).
*/
angular.module('angular-joystick').provider('JoystickService', function () {
this.$get = ['$timeout', function($timeout) {
var INACTIVE = 0;
var ACTIVE = 1;
var SECONDS_INACTIVE = 0.5;
function loadSprite(src, callback) {
var sprite = new Image();
sprite.onload = callback;
sprite.src = src;
return sprite;
}
var squareSize = 150;
var state = INACTIVE;
var _coords = {x: 0, y: 0};
var _callback = null;
var canvas = null;
var context = null;
var radius = (squareSize / 2) * 0.5;
var joyStickLoaded = false;
var backgroundLoaded = false;
var lastTouch = new Date().getTime();
var sprite;
var background;
var self = {
get: function(coords, callback) {
if(coords) {
_coords = coords;
}
if (callback) {
_callback = callback;
}
return self;
},
coords: _coords,
squareSize: squareSize,
_retractJoystickForInactivity: function(){
var framesPerSec = 15;
$timeout(function(){
var currentTime = new Date().getTime();
if(currentTime - lastTouch >= SECONDS_INACTIVE * 1000){
self._retractToMiddle();
self.renderSprite();
}
self._retractJoystickForInactivity();
}, parseInt(1000 / framesPerSec, 10));
},
_tryCallback: function(){
if (background && joyStickLoaded) {
if (!context) {
self.render();
}
self.renderSprite();
}
},
startControl: function(){
console.log('startControl');
state = ACTIVE;
},
endControl: function(){
console.log('endControl');
state = INACTIVE;
_coords.x = 0;
_coords.y = 0;
self.renderSprite();
},
move: function(evt){
if(state === INACTIVE){
return;
}
lastTouch = new Date().getTime();
var x, y;
if(evt.originalEvent && evt.originalEvent.touches){
evt.preventDefault();
var left = 0;
var fromTop = 0;
var elem = $(canvas)[0];
while(elem) {
left = left + parseInt(elem.offsetLeft);
fromTop = fromTop + parseInt(elem.offsetTop);
elem = elem.offsetParent;
}
x = evt.originalEvent.touches[0].clientX - left;
y = evt.originalEvent.touches[0].clientY - fromTop;
} else {
x = evt.offsetX;
y = evt.offsetY;
}
self._mutateToCartesian(x, y);
self._triggerChange();
},
_triggerChange: function(){
var xPercent = _coords.x / radius;
var yPercent = _coords.y / radius;
if(Math.abs(xPercent) > 1.0){
xPercent /= Math.abs(xPercent);
}
if(Math.abs(yPercent) > 1.0){
yPercent /= Math.abs(yPercent);
}
_callback();
//self.trigger('horizontalMove', xPercent);
//self.trigger('verticalMove', yPercent);
},
_mutateToCartesian: function(x, y){
x -= (squareSize) / 2;
y *= -1;
y += (squareSize) / 2;
if(isNaN(y)){
y = squareSize / 2;
}
_coords.x = x;
_coords.y = y;
if(self._valuesExceedRadius(_coords.x, _coords.y)){
self._traceNewValues();
}
self.renderSprite();
},
_retractToMiddle: function(){
var percentLoss = 0.1;
var toKeep = 1.0 - percentLoss;
var xSign = 1;
var ySign = 1;
if(_coords.x !== 0){
xSign = _coords.x / Math.abs(_coords.x);
}
if(_coords.y !== 0) {
ySign = _coords.y / Math.abs(_coords.y);
}
_coords.x = Math.floor(toKeep * Math.abs(_coords.x)) * xSign;
_coords.y = Math.floor(toKeep * Math.abs(_coords.y)) * ySign;
},
_traceNewValues: function(){
var slope = _coords.y / _coords.x;
var xIncr = 1;
var y;
if(_coords.x < 0){
xIncr = -1;
}
for(var x=0; x<squareSize / 2; x+=xIncr){
y = x * slope;
if(self._valuesExceedRadius(x, y)){
break;
}
}
_coords.x = x;
_coords.y = y;
},
_cartesianToCanvas: function(x, y){
var newX = x + squareSize / 2;
var newY = y - (squareSize / 2);
newY = newY * -1;
return {
x: newX,
y: newY
};
},
_valuesExceedRadius: function(x, y){
if(x === 0){
return y > radius;
}
return Math.pow(x, 2) + Math.pow(y, 2) > Math.pow(radius, 2);
},
renderSprite: function(){
//console.log('renderSprite');
var originalWidth = 89;
var originalHeight = 89;
var spriteWidth = 50;
var spriteHeight = 50;
var pixelsLeft = 0; //ofset for sprite on img
var pixelsTop = 0; //offset for sprite on img
var coords = self._cartesianToCanvas(_coords.x, _coords.y);
if(context === null){
console.log('context is null :(');
return;
}
// hack dunno why I need the 2x
context.clearRect(0, 0, squareSize * 2, squareSize);
var backImageSize = 300;
context.drawImage(background,
0,
0,
backImageSize,
backImageSize,
0,
0,
squareSize,
squareSize
);
context.drawImage(sprite,
pixelsLeft,
pixelsTop,
originalWidth,
originalHeight,
coords.x - spriteWidth / 2,
coords.y - spriteHeight / 2,
spriteWidth,
spriteHeight
);
},
render: function(){
canvas = $('#joystickCanvas')[0];
console.log('render ' + canvas);
context = canvas.getContext('2d');
self.renderSprite();
return this;
}
};
sprite = loadSprite('static/assets/button.png', function(){
joyStickLoaded = true;
self._tryCallback();
});
background = loadSprite('static/assets/canvas.png', function(){
backgroundLoaded = true;
self._tryCallback();
});
/*$timeout(function() {
self._retractJoystickForInactivity();
}, 1000);*/
return self;
}]; //$get
}); // provider