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

Update jquery.imgareaselect.dev.js #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
54 changes: 35 additions & 19 deletions jquery.imgareaselect.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,13 @@ $.imgAreaSelect = function (img, options) {
* Check if selection area is within image boundaries, adjust if
* necessary
*/
if (selection.x2 > imgWidth || selection.y2 > imgHeight)
if (selection.x2 > imgWidth || selection.y2 > imgHeight) {
x1 = min(max(x1, left), left + imgWidth);
y1 = min(max(y1, top), top + imgHeight);
x2 = x1 + selection.x2;
y2 = y1 + selection.y2;
doResize();
}
}

/**
Expand Down Expand Up @@ -570,27 +575,35 @@ $.imgAreaSelect = function (img, options) {
* image boundaries (it might not if the image source was dynamically
* changed).
*/
x1 = min(x1, left + imgWidth);
y1 = min(y1, top + imgHeight);
x1 = min(max(x1, left), left + imgWidth);
y1 = min(max(y1, top), top + imgHeight);

if (abs(x2 - x1) < minWidth) {
/* Selection width is smaller than minWidth */
x2 = x1 - minWidth * (x2 < x1 || -1);

if (x2 < left)
x1 = left + minWidth;
else if (x2 > left + imgWidth)
x1 = left + imgWidth - minWidth;
if (x2 < left) {
x1 = min(left + imgWidth, left + minWidth);
x2 = left;
}
else if (x2 > left + imgWidth) {
x1 = max(left, left + imgWidth - minWidth);
x2 = left + imgWidth;
}
}

if (abs(y2 - y1) < minHeight) {
/* Selection height is smaller than minHeight */
y2 = y1 - minHeight * (y2 < y1 || -1);

if (y2 < top)
y1 = top + minHeight;
else if (y2 > top + imgHeight)
y1 = top + imgHeight - minHeight;
if (y2 < top) {
y1 = min(top + imgHeight, top + minHeight);
y2 = top;
}
else if (y2 > top + imgHeight) {
y1 = max(top, top + imgHeight - minHeight);
y2 = top + imgHeight;
}
}

x2 = max(left, min(x2, left + imgWidth));
Expand Down Expand Up @@ -704,14 +717,17 @@ $.imgAreaSelect = function (img, options) {
function cancelSelection() {
$(document).unbind('mousemove', startSelection)
.unbind('mouseup', cancelSelection);
hide($box.add($outer));

setSelection(selX(x1), selY(y1), selX(x1), selY(y1));

/* If this is an API call, callback functions should not be triggered */
if (!(this instanceof $.imgAreaSelect)) {
options.onSelectChange(img, getSelection());
options.onSelectEnd(img, getSelection());
if (!options.disableCancelSelection) {
hide($box.add($outer));

setSelection(selX(x1), selY(y1), selX(x1), selY(y1));

/* If this is an API call, callback functions should not be triggered */
if (!(this instanceof $.imgAreaSelect)) {
options.onSelectChange(img, getSelection());
options.onSelectEnd(img, getSelection());
}
}
}

Expand Down Expand Up @@ -763,7 +779,7 @@ $.imgAreaSelect = function (img, options) {
onSelectEnd: function () {}
}, options));

$box.add($outer).css({ visibility: '' });
$box.add($outer).css({ visibility: '' , position: ($parent[0].tagName !== 'BODY'? 'absolute' : undefined) });

if (options.show) {
shown = true;
Expand Down