Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collection): add initial frontPosition option #134

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions example/joystick-frontPosition.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>NippleJS</title>
<meta name="viewport" content="width=device-width, initial-scale=0.5, maximum-scale=0.5">
<style>
html, body {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding: 0;
margin: 0;
}

#left {
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
background: rgba(1,77,129, 0.1);
}
</style>
</head>
<body>
<div id="left"></div>
<script src="/dist/nipplejs.js"></script>
<script>
var joystickL = nipplejs.create({
zone: document.getElementById('left'),
mode: 'static',
position: { left: '50%', top: '50%' },
color: '#014d81',
size: 200,
restJoystick: false,
frontPosition: {x: 25, y: -35},
restJoystick: false
});
</script>
</body>
</html>
22 changes: 17 additions & 5 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function Collection (manager, options) {
restOpacity: 0.5,
lockX: false,
lockY: false,
dynamicPage: false
dynamicPage: false,
frontPosition: {x: 0, y: 0}
};

self.config(options);
Expand Down Expand Up @@ -154,6 +155,20 @@ Collection.prototype.createNipple = function (position, identifier) {
};
}

//limit the position of the front to the size of the joystick.
var size = opts.size / 2;
var frontPosition = opts.frontPosition;
var defaultFrontPostion = self.defaults.frontPosition;
var dist = u.distance(frontPosition, defaultFrontPostion);
var angle = u.angle(frontPosition, defaultFrontPostion);

// If distance is bigger than nipple's size
// we clamp the position.
if (dist > size) {
dist = size;
frontPosition = u.findCoord(defaultFrontPostion, dist, angle);
}

var nipple = new Nipple(self, {
color: opts.color,
size: opts.size,
Expand All @@ -166,10 +181,7 @@ Collection.prototype.createNipple = function (position, identifier) {
identifier: identifier,
position: position,
zone: opts.zone,
frontPosition: {
x: 0,
y: 0
}
frontPosition: frontPosition
});

if (!opts.dataOnly) {
Expand Down