Skip to content

Commit a6580dd

Browse files
committed
feat(collection): add initial frontPosition option
Add the option to be able to indicate the initial position of the joystick front Currently the position is set to x: 0 and: 0 and does not allow modification via options when creating the nipple close yoannmoinet#133
1 parent d3bea82 commit a6580dd

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

example/joystick-frontPosition.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>NippleJS</title>
6+
<meta name="viewport" content="width=device-width, initial-scale=0.5, maximum-scale=0.5">
7+
<style>
8+
html, body {
9+
position: absolute;
10+
top: 0;
11+
left: 0;
12+
right: 0;
13+
bottom: 0;
14+
padding: 0;
15+
margin: 0;
16+
}
17+
18+
#left {
19+
position: absolute;
20+
left: 0;
21+
top: 0;
22+
height: 100%;
23+
width: 100%;
24+
background: rgba(1,77,129, 0.1);
25+
}
26+
</style>
27+
</head>
28+
<body>
29+
<div id="left"></div>
30+
<script src="/dist/nipplejs.js"></script>
31+
<script>
32+
var joystickL = nipplejs.create({
33+
zone: document.getElementById('left'),
34+
mode: 'static',
35+
position: { left: '50%', top: '50%' },
36+
color: '#014d81',
37+
size: 200,
38+
restJoystick: false,
39+
frontPosition: {x: 25, y: -35},
40+
restJoystick: false
41+
});
42+
</script>
43+
</body>
44+
</html>

src/collection.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ function Collection (manager, options) {
3434
restOpacity: 0.5,
3535
lockX: false,
3636
lockY: false,
37-
dynamicPage: false
37+
dynamicPage: false,
38+
frontPosition: {x: 0, y: 0}
3839
};
3940

4041
self.config(options);
@@ -154,6 +155,20 @@ Collection.prototype.createNipple = function (position, identifier) {
154155
};
155156
}
156157

158+
//limit the position of the front to the size of the joystick.
159+
var size = opts.size / 2;
160+
var frontPosition = opts.frontPosition;
161+
var defaultFrontPostion = self.defaults.frontPosition;
162+
var dist = u.distance(frontPosition, defaultFrontPostion);
163+
var angle = u.angle(frontPosition, defaultFrontPostion);
164+
165+
// If distance is bigger than nipple's size
166+
// we clamp the position.
167+
if (dist > size) {
168+
dist = size;
169+
frontPosition = u.findCoord(defaultFrontPostion, dist, angle);
170+
}
171+
157172
var nipple = new Nipple(self, {
158173
color: opts.color,
159174
size: opts.size,
@@ -166,10 +181,7 @@ Collection.prototype.createNipple = function (position, identifier) {
166181
identifier: identifier,
167182
position: position,
168183
zone: opts.zone,
169-
frontPosition: {
170-
x: 0,
171-
y: 0
172-
}
184+
frontPosition: frontPosition
173185
});
174186

175187
if (!opts.dataOnly) {

0 commit comments

Comments
 (0)