Skip to content

Commit

Permalink
Force movement in single axis when shift is pressed. Fixes issue bgri…
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrins committed Jun 11, 2013
1 parent 4a31f45 commit 012d1fb
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions spectrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@
paletteArray = $.isArray(palette[0]) ? palette : [palette],
selectionPalette = opts.selectionPalette.slice(0),
maxSelectionSize = opts.maxSelectionSize,
draggingClass = "sp-dragging";
draggingClass = "sp-dragging",
shiftMovementDirection = null;

var doc = element.ownerDocument,
body = doc.body,
Expand Down Expand Up @@ -309,10 +310,32 @@
move();
}, dragStart, dragStop);

draggable(dragger, function (dragX, dragY) {
currentSaturation = parseFloat(dragX / dragWidth);
currentValue = parseFloat((dragHeight - dragY) / dragHeight);
draggable(dragger, function (dragX, dragY, e) {

// shift+drag should snap the movement to either the x or y axis.
if (!e.shiftKey) {
shiftMovementDirection = null;
}
else if (!shiftMovementDirection) {
var oldDragX = currentSaturation * dragWidth;
var oldDragY = dragHeight - (currentValue * dragHeight);
var furtherFromX = Math.abs(dragX - oldDragX) > Math.abs(dragY - oldDragY);

shiftMovementDirection = furtherFromX ? "x" : "y";
}

var setSaturation = !shiftMovementDirection || shiftMovementDirection === "x";
var setValue = !shiftMovementDirection || shiftMovementDirection === "y";

if (setSaturation) {
currentSaturation = parseFloat(dragX / dragWidth);
}
if (setValue) {
currentValue = parseFloat((dragHeight - dragY) / dragHeight);
}

move();

}, dragStart, dragStop);

if (!!initialColor) {
Expand Down Expand Up @@ -428,6 +451,7 @@
reflow();
}
container.addClass(draggingClass);
shiftMovementDirection = null;
}

function dragStop() {
Expand Down

0 comments on commit 012d1fb

Please sign in to comment.