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

Adding a ratio option to Drag class #1169

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions Docs/Drag/Drag.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Drag Method: constructor
* handle - (*element*: defaults to the element passed in) The Element to act as the handle for the draggable element.
* invert - (*boolean*: defaults to false) Whether or not to invert the values reported on start and drag.
* limit - (*object*: defaults to false) An object with an x and a y property, both an array containing the minimum and maximum limit of movement of the Element.
* ratio - (*number*: defaults to false) Add a ratio constraint between x and y properties (ratio = x / y).
* modifiers - (*object*: defaults to {'x': 'left', 'y': 'top'}) An object with x and y properties used to indicate the CSS modifiers (i.e. 'left').
* snap - (*number*: defaults to 6) The distance to drag before the Element starts to respond to the drag.
* style - (*boolean*: defaults to true) Whether or not to set the modifier as a style property of the element.
Expand Down
11 changes: 10 additions & 1 deletion Source/Drag/Drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var Drag = new Class({
grid: false,
style: true,
limit: false,
ratio: false,
handle: false,
invert: false,
preventDefault: false,
Expand Down Expand Up @@ -182,7 +183,15 @@ var Drag = new Class({
this.value.now[z] = this.limit[z][0];
}
}


if(options.ratio) {
if(this.value.now.x / this.value.now.y < options.ratio) {
this.value.now.y = this.value.now.x / options.ratio;
} else {
this.value.now.x = options.ratio * this.value.now.y;
}
}

if (options.grid[z]) this.value.now[z] -= ((this.value.now[z] - (this.limit[z][0]||0)) % options.grid[z]);

if (options.style) this.element.setStyle(options.modifiers[z], this.value.now[z] + options.unit);
Expand Down