2009-09-10 18:07:33 -04:00
|
|
|
/*
|
|
|
|
* imgAreaSelect jQuery plugin
|
2012-04-25 17:49:57 -04:00
|
|
|
* version 0.9.8
|
2009-09-10 18:07:33 -04:00
|
|
|
*
|
2011-05-15 14:03:51 -04:00
|
|
|
* Copyright (c) 2008-2011 Michal Wojciechowski (odyniec.net)
|
2009-09-10 18:07:33 -04:00
|
|
|
*
|
|
|
|
* Dual licensed under the MIT (MIT-LICENSE.txt)
|
|
|
|
* and GPL (GPL-LICENSE.txt) licenses.
|
|
|
|
*
|
|
|
|
* http://odyniec.net/projects/imgareaselect/
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function($) {
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Math functions will be used extensively, so it's convenient to make a few
|
|
|
|
* shortcuts
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
var abs = Math.abs,
|
|
|
|
max = Math.max,
|
|
|
|
min = Math.min,
|
|
|
|
round = Math.round;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Create a new HTML div element
|
|
|
|
*
|
|
|
|
* @return A jQuery object representing the new element
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function div() {
|
|
|
|
return $('<div/>');
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* imgAreaSelect initialization
|
|
|
|
*
|
|
|
|
* @param img
|
|
|
|
* A HTML image element to attach the plugin to
|
|
|
|
* @param options
|
|
|
|
* An options object
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$.imgAreaSelect = function (img, options) {
|
|
|
|
var
|
2012-04-25 17:49:57 -04:00
|
|
|
/* jQuery object representing the image */
|
2009-09-10 18:07:33 -04:00
|
|
|
$img = $(img),
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Has the image finished loading? */
|
2009-09-10 18:07:33 -04:00
|
|
|
imgLoaded,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Plugin elements */
|
|
|
|
|
|
|
|
/* Container box */
|
2009-09-10 18:07:33 -04:00
|
|
|
$box = div(),
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Selection area */
|
2009-09-10 18:07:33 -04:00
|
|
|
$area = div(),
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Border (four divs) */
|
2009-09-10 18:07:33 -04:00
|
|
|
$border = div().add(div()).add(div()).add(div()),
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Outer area (four divs) */
|
2009-09-10 18:07:33 -04:00
|
|
|
$outer = div().add(div()).add(div()).add(div()),
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Handles (empty by default, initialized in setOptions()) */
|
2009-09-10 18:07:33 -04:00
|
|
|
$handles = $([]),
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Additional element to work around a cursor problem in Opera
|
|
|
|
* (explained later)
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$areaOpera,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Image position (relative to viewport) */
|
2009-09-10 18:07:33 -04:00
|
|
|
left, top,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Image offset (as returned by .offset()) */
|
2011-05-15 14:03:51 -04:00
|
|
|
imgOfs = { left: 0, top: 0 },
|
2009-09-10 18:07:33 -04:00
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Image dimensions (as returned by .width() and .height()) */
|
2009-09-10 18:07:33 -04:00
|
|
|
imgWidth, imgHeight,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* jQuery object representing the parent element that the plugin
|
|
|
|
* elements are appended to
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$parent,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Parent element offset (as returned by .offset()) */
|
2011-05-15 14:03:51 -04:00
|
|
|
parOfs = { left: 0, top: 0 },
|
2009-09-10 18:07:33 -04:00
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Base z-index for plugin elements */
|
2009-09-10 18:07:33 -04:00
|
|
|
zIndex = 0,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Plugin elements position */
|
2009-09-10 18:07:33 -04:00
|
|
|
position = 'absolute',
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* X/Y coordinates of the starting point for move/resize operations */
|
2009-09-10 18:07:33 -04:00
|
|
|
startX, startY,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Horizontal and vertical scaling factors */
|
2009-09-10 18:07:33 -04:00
|
|
|
scaleX, scaleY,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Current resize mode ("nw", "se", etc.) */
|
2009-09-10 18:07:33 -04:00
|
|
|
resize,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Selection area constraints */
|
2011-05-15 14:03:51 -04:00
|
|
|
minWidth, minHeight, maxWidth, maxHeight,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Aspect ratio to maintain (floating point number) */
|
2009-09-10 18:07:33 -04:00
|
|
|
aspectRatio,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Are the plugin elements currently displayed? */
|
2009-09-10 18:07:33 -04:00
|
|
|
shown,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Current selection (relative to parent element) */
|
2009-09-10 18:07:33 -04:00
|
|
|
x1, y1, x2, y2,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Current selection (relative to scaled image) */
|
2009-09-10 18:07:33 -04:00
|
|
|
selection = { x1: 0, y1: 0, x2: 0, y2: 0, width: 0, height: 0 },
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Document element */
|
2011-05-15 14:03:51 -04:00
|
|
|
docElem = document.documentElement,
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Various helper variables used throughout the code */
|
2009-09-10 18:07:33 -04:00
|
|
|
$p, d, i, o, w, h, adjusted;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Translate selection coordinates (relative to scaled image) to viewport
|
|
|
|
* coordinates (relative to parent element)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate selection X to viewport X
|
|
|
|
*
|
|
|
|
* @param x
|
|
|
|
* Selection X
|
|
|
|
* @return Viewport X
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function viewX(x) {
|
|
|
|
return x + imgOfs.left - parOfs.left;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Translate selection Y to viewport Y
|
|
|
|
*
|
|
|
|
* @param y
|
|
|
|
* Selection Y
|
|
|
|
* @return Viewport Y
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function viewY(y) {
|
|
|
|
return y + imgOfs.top - parOfs.top;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Translate viewport coordinates to selection coordinates
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate viewport X to selection X
|
|
|
|
*
|
|
|
|
* @param x
|
|
|
|
* Viewport X
|
|
|
|
* @return Selection X
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function selX(x) {
|
|
|
|
return x - imgOfs.left + parOfs.left;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Translate viewport Y to selection Y
|
|
|
|
*
|
|
|
|
* @param y
|
|
|
|
* Viewport Y
|
|
|
|
* @return Selection Y
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function selY(y) {
|
|
|
|
return y - imgOfs.top + parOfs.top;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Translate event coordinates (relative to document) to viewport
|
|
|
|
* coordinates
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get event X and translate it to viewport X
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event object
|
|
|
|
* @return Viewport X
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function evX(event) {
|
|
|
|
return event.pageX - parOfs.left;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Get event Y and translate it to viewport Y
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event object
|
|
|
|
* @return Viewport Y
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function evY(event) {
|
|
|
|
return event.pageY - parOfs.top;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Get the current selection
|
|
|
|
*
|
|
|
|
* @param noScale
|
|
|
|
* If set to <code>true</code>, scaling is not applied to the
|
|
|
|
* returned selection
|
|
|
|
* @return Selection object
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function getSelection(noScale) {
|
|
|
|
var sx = noScale || scaleX, sy = noScale || scaleY;
|
|
|
|
|
|
|
|
return { x1: round(selection.x1 * sx),
|
|
|
|
y1: round(selection.y1 * sy),
|
|
|
|
x2: round(selection.x2 * sx),
|
|
|
|
y2: round(selection.y2 * sy),
|
|
|
|
width: round(selection.x2 * sx) - round(selection.x1 * sx),
|
|
|
|
height: round(selection.y2 * sy) - round(selection.y1 * sy) };
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Set the current selection
|
|
|
|
*
|
|
|
|
* @param x1
|
|
|
|
* X coordinate of the upper left corner of the selection area
|
|
|
|
* @param y1
|
|
|
|
* Y coordinate of the upper left corner of the selection area
|
|
|
|
* @param x2
|
|
|
|
* X coordinate of the lower right corner of the selection area
|
|
|
|
* @param y2
|
|
|
|
* Y coordinate of the lower right corner of the selection area
|
|
|
|
* @param noScale
|
|
|
|
* If set to <code>true</code>, scaling is not applied to the
|
|
|
|
* new selection
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function setSelection(x1, y1, x2, y2, noScale) {
|
|
|
|
var sx = noScale || scaleX, sy = noScale || scaleY;
|
|
|
|
|
|
|
|
selection = {
|
2011-05-15 14:03:51 -04:00
|
|
|
x1: round(x1 / sx || 0),
|
|
|
|
y1: round(y1 / sy || 0),
|
|
|
|
x2: round(x2 / sx || 0),
|
|
|
|
y2: round(y2 / sy || 0)
|
2009-09-10 18:07:33 -04:00
|
|
|
};
|
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
selection.width = selection.x2 - selection.x1;
|
|
|
|
selection.height = selection.y2 - selection.y1;
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Recalculate image and parent offsets
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function adjust() {
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Do not adjust if image width is not a positive number. This might
|
|
|
|
* happen when imgAreaSelect is put on a parent element which is then
|
|
|
|
* hidden.
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
if (!$img.width())
|
|
|
|
return;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Get image offset. The .offset() method returns float values, so they
|
|
|
|
* need to be rounded.
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
imgOfs = { left: round($img.offset().left), top: round($img.offset().top) };
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Get image dimensions */
|
2011-05-15 14:03:51 -04:00
|
|
|
imgWidth = $img.innerWidth();
|
|
|
|
imgHeight = $img.innerHeight();
|
|
|
|
|
|
|
|
imgOfs.top += ($img.outerHeight() - imgHeight) >> 1;
|
|
|
|
imgOfs.left += ($img.outerWidth() - imgWidth) >> 1;
|
2009-09-10 18:07:33 -04:00
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Set minimum and maximum selection area dimensions */
|
|
|
|
minWidth = round(options.minWidth / scaleX) || 0;
|
|
|
|
minHeight = round(options.minHeight / scaleY) || 0;
|
|
|
|
maxWidth = round(min(options.maxWidth / scaleX || 1<<24, imgWidth));
|
|
|
|
maxHeight = round(min(options.maxHeight / scaleY || 1<<24, imgHeight));
|
2009-09-10 18:07:33 -04:00
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Workaround for jQuery 1.3.2 incorrect offset calculation, originally
|
|
|
|
* observed in Safari 3. Firefox 2 is also affected.
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
if ($().jquery == '1.3.2' && position == 'fixed' &&
|
|
|
|
!docElem['getBoundingClientRect'])
|
|
|
|
{
|
|
|
|
imgOfs.top += max(document.body.scrollTop, docElem.scrollTop);
|
|
|
|
imgOfs.left += max(document.body.scrollLeft, docElem.scrollLeft);
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Determine parent element offset */
|
|
|
|
parOfs = /absolute|relative/.test($parent.css('position')) ?
|
2009-09-10 18:07:33 -04:00
|
|
|
{ left: round($parent.offset().left) - $parent.scrollLeft(),
|
|
|
|
top: round($parent.offset().top) - $parent.scrollTop() } :
|
|
|
|
position == 'fixed' ?
|
|
|
|
{ left: $(document).scrollLeft(), top: $(document).scrollTop() } :
|
|
|
|
{ left: 0, top: 0 };
|
|
|
|
|
|
|
|
left = viewX(0);
|
|
|
|
top = viewY(0);
|
2011-05-15 14:03:51 -04:00
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Check if selection area is within image boundaries, adjust if
|
|
|
|
* necessary
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
if (selection.x2 > imgWidth || selection.y2 > imgHeight)
|
|
|
|
doResize();
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Update plugin elements
|
|
|
|
*
|
|
|
|
* @param resetKeyPress
|
|
|
|
* If set to <code>false</code>, this instance's keypress
|
|
|
|
* event handler is not activated
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function update(resetKeyPress) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* If plugin elements are hidden, do nothing */
|
2009-09-10 18:07:33 -04:00
|
|
|
if (!shown) return;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Set the position and size of the container box and the selection area
|
|
|
|
* inside it
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$box.css({ left: viewX(selection.x1), top: viewY(selection.y1) })
|
|
|
|
.add($area).width(w = selection.width).height(h = selection.height);
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Reset the position of selection area, borders, and handles (IE6/IE7
|
|
|
|
* position them incorrectly if we don't do this)
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$area.add($border).add($handles).css({ left: 0, top: 0 });
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Set border dimensions */
|
2009-09-10 18:07:33 -04:00
|
|
|
$border
|
|
|
|
.width(max(w - $border.outerWidth() + $border.innerWidth(), 0))
|
|
|
|
.height(max(h - $border.outerHeight() + $border.innerHeight(), 0));
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Arrange the outer area elements */
|
2009-09-10 18:07:33 -04:00
|
|
|
$($outer[0]).css({ left: left, top: top,
|
|
|
|
width: selection.x1, height: imgHeight });
|
|
|
|
$($outer[1]).css({ left: left + selection.x1, top: top,
|
|
|
|
width: w, height: selection.y1 });
|
|
|
|
$($outer[2]).css({ left: left + selection.x2, top: top,
|
|
|
|
width: imgWidth - selection.x2, height: imgHeight });
|
|
|
|
$($outer[3]).css({ left: left + selection.x1, top: top + selection.y2,
|
|
|
|
width: w, height: imgHeight - selection.y2 });
|
|
|
|
|
|
|
|
w -= $handles.outerWidth();
|
|
|
|
h -= $handles.outerHeight();
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Arrange handles */
|
2009-09-10 18:07:33 -04:00
|
|
|
switch ($handles.length) {
|
|
|
|
case 8:
|
2011-05-15 14:03:51 -04:00
|
|
|
$($handles[4]).css({ left: w >> 1 });
|
|
|
|
$($handles[5]).css({ left: w, top: h >> 1 });
|
|
|
|
$($handles[6]).css({ left: w >> 1, top: h });
|
|
|
|
$($handles[7]).css({ top: h >> 1 });
|
2009-09-10 18:07:33 -04:00
|
|
|
case 4:
|
|
|
|
$handles.slice(1,3).css({ left: w });
|
|
|
|
$handles.slice(2,4).css({ top: h });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resetKeyPress !== false) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Need to reset the document keypress event handler -- unbind the
|
|
|
|
* current handler
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
if ($.imgAreaSelect.keyPress != docKeyPress)
|
|
|
|
$(document).unbind($.imgAreaSelect.keyPress,
|
|
|
|
$.imgAreaSelect.onKeyPress);
|
|
|
|
|
|
|
|
if (options.keys)
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Set the document keypress event handler to this instance's
|
|
|
|
* docKeyPress() function
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$(document)[$.imgAreaSelect.keyPress](
|
|
|
|
$.imgAreaSelect.onKeyPress = docKeyPress);
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Internet Explorer displays 1px-wide dashed borders incorrectly by
|
|
|
|
* filling the spaces between dashes with white. Toggling the margin
|
|
|
|
* property between 0 and "auto" fixes this in IE6 and IE7 (IE8 is still
|
|
|
|
* broken). This workaround is not perfect, as it requires setTimeout()
|
|
|
|
* and thus causes the border to flicker a bit, but I haven't found a
|
|
|
|
* better solution.
|
|
|
|
*
|
|
|
|
* Note: This only happens with CSS borders, set with the borderWidth,
|
|
|
|
* borderOpacity, borderColor1, and borderColor2 options (which are now
|
|
|
|
* deprecated). Borders created with GIF background images are fine.
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
if ($.browser.msie && $border.outerWidth() - $border.innerWidth() == 2) {
|
|
|
|
$border.css('margin', 0);
|
|
|
|
setTimeout(function () { $border.css('margin', 'auto'); }, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Do the complete update sequence: recalculate offsets, update the
|
|
|
|
* elements, and set the correct values of x1, y1, x2, and y2.
|
|
|
|
*
|
|
|
|
* @param resetKeyPress
|
|
|
|
* If set to <code>false</code>, this instance's keypress
|
|
|
|
* event handler is not activated
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function doUpdate(resetKeyPress) {
|
|
|
|
adjust();
|
|
|
|
update(resetKeyPress);
|
|
|
|
x1 = viewX(selection.x1); y1 = viewY(selection.y1);
|
|
|
|
x2 = viewX(selection.x2); y2 = viewY(selection.y2);
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Hide or fade out an element (or multiple elements)
|
|
|
|
*
|
|
|
|
* @param $elem
|
|
|
|
* A jQuery object containing the element(s) to hide/fade out
|
|
|
|
* @param fn
|
|
|
|
* Callback function to be called when fadeOut() completes
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function hide($elem, fn) {
|
|
|
|
options.fadeSpeed ? $elem.fadeOut(options.fadeSpeed, fn) : $elem.hide();
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Selection area mousemove event handler
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event object
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function areaMouseMove(event) {
|
|
|
|
var x = selX(evX(event)) - selection.x1,
|
|
|
|
y = selY(evY(event)) - selection.y1;
|
|
|
|
|
|
|
|
if (!adjusted) {
|
|
|
|
adjust();
|
|
|
|
adjusted = true;
|
|
|
|
|
|
|
|
$box.one('mouseout', function () { adjusted = false; });
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Clear the resize mode */
|
2009-09-10 18:07:33 -04:00
|
|
|
resize = '';
|
|
|
|
|
|
|
|
if (options.resizable) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Check if the mouse pointer is over the resize margin area and set
|
|
|
|
* the resize mode accordingly
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
if (y <= options.resizeMargin)
|
2009-09-10 18:07:33 -04:00
|
|
|
resize = 'n';
|
2011-05-15 14:03:51 -04:00
|
|
|
else if (y >= selection.height - options.resizeMargin)
|
2009-09-10 18:07:33 -04:00
|
|
|
resize = 's';
|
2011-05-15 14:03:51 -04:00
|
|
|
if (x <= options.resizeMargin)
|
2009-09-10 18:07:33 -04:00
|
|
|
resize += 'w';
|
2011-05-15 14:03:51 -04:00
|
|
|
else if (x >= selection.width - options.resizeMargin)
|
2009-09-10 18:07:33 -04:00
|
|
|
resize += 'e';
|
|
|
|
}
|
|
|
|
|
|
|
|
$box.css('cursor', resize ? resize + '-resize' :
|
|
|
|
options.movable ? 'move' : '');
|
|
|
|
if ($areaOpera)
|
|
|
|
$areaOpera.toggle();
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Document mouseup event handler
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event object
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function docMouseUp(event) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Set back the default cursor */
|
2009-09-10 18:07:33 -04:00
|
|
|
$('body').css('cursor', '');
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* If autoHide is enabled, or if the selection has zero width/height,
|
|
|
|
* hide the selection and the outer area
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
if (options.autoHide || selection.width * selection.height == 0)
|
|
|
|
hide($box.add($outer), function () { $(this).hide(); });
|
|
|
|
|
|
|
|
$(document).unbind('mousemove', selectingMouseMove);
|
|
|
|
$box.mousemove(areaMouseMove);
|
2011-05-15 14:03:51 -04:00
|
|
|
|
|
|
|
options.onSelectEnd(img, getSelection());
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Selection area mousedown event handler
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event object
|
|
|
|
* @return false
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function areaMouseDown(event) {
|
|
|
|
if (event.which != 1) return false;
|
|
|
|
|
|
|
|
adjust();
|
|
|
|
|
|
|
|
if (resize) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Resize mode is in effect */
|
2009-09-10 18:07:33 -04:00
|
|
|
$('body').css('cursor', resize + '-resize');
|
|
|
|
|
|
|
|
x1 = viewX(selection[/w/.test(resize) ? 'x2' : 'x1']);
|
|
|
|
y1 = viewY(selection[/n/.test(resize) ? 'y2' : 'y1']);
|
|
|
|
|
|
|
|
$(document).mousemove(selectingMouseMove)
|
|
|
|
.one('mouseup', docMouseUp);
|
|
|
|
$box.unbind('mousemove', areaMouseMove);
|
|
|
|
}
|
|
|
|
else if (options.movable) {
|
|
|
|
startX = left + selection.x1 - evX(event);
|
|
|
|
startY = top + selection.y1 - evY(event);
|
|
|
|
|
|
|
|
$box.unbind('mousemove', areaMouseMove);
|
|
|
|
|
|
|
|
$(document).mousemove(movingMouseMove)
|
|
|
|
.one('mouseup', function () {
|
|
|
|
options.onSelectEnd(img, getSelection());
|
|
|
|
|
|
|
|
$(document).unbind('mousemove', movingMouseMove);
|
|
|
|
$box.mousemove(areaMouseMove);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$img.mousedown(event);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Adjust the x2/y2 coordinates to maintain aspect ratio (if defined)
|
|
|
|
*
|
|
|
|
* @param xFirst
|
|
|
|
* If set to <code>true</code>, calculate x2 first. Otherwise,
|
|
|
|
* calculate y2 first.
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
function fixAspectRatio(xFirst) {
|
|
|
|
if (aspectRatio)
|
|
|
|
if (xFirst) {
|
|
|
|
x2 = max(left, min(left + imgWidth,
|
|
|
|
x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1)));
|
|
|
|
y2 = round(max(top, min(top + imgHeight,
|
|
|
|
y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1))));
|
|
|
|
x2 = round(x2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
y2 = max(top, min(top + imgHeight,
|
|
|
|
y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1)));
|
|
|
|
x2 = round(max(left, min(left + imgWidth,
|
|
|
|
x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1))));
|
|
|
|
y2 = round(y2);
|
|
|
|
}
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Resize the selection area respecting the minimum/maximum dimensions and
|
|
|
|
* aspect ratio
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function doResize() {
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Make sure the top left corner of the selection area stays within
|
|
|
|
* image boundaries (it might not if the image source was dynamically
|
|
|
|
* changed).
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
x1 = min(x1, left + imgWidth);
|
|
|
|
y1 = min(y1, top + imgHeight);
|
|
|
|
|
|
|
|
if (abs(x2 - x1) < minWidth) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Selection width is smaller than minWidth */
|
2011-05-15 14:03:51 -04:00
|
|
|
x2 = x1 - minWidth * (x2 < x1 || -1);
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
if (x2 < left)
|
2011-05-15 14:03:51 -04:00
|
|
|
x1 = left + minWidth;
|
2009-09-10 18:07:33 -04:00
|
|
|
else if (x2 > left + imgWidth)
|
2011-05-15 14:03:51 -04:00
|
|
|
x1 = left + imgWidth - minWidth;
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
if (abs(y2 - y1) < minHeight) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Selection height is smaller than minHeight */
|
2011-05-15 14:03:51 -04:00
|
|
|
y2 = y1 - minHeight * (y2 < y1 || -1);
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
if (y2 < top)
|
2011-05-15 14:03:51 -04:00
|
|
|
y1 = top + minHeight;
|
2009-09-10 18:07:33 -04:00
|
|
|
else if (y2 > top + imgHeight)
|
2011-05-15 14:03:51 -04:00
|
|
|
y1 = top + imgHeight - minHeight;
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
x2 = max(left, min(x2, left + imgWidth));
|
|
|
|
y2 = max(top, min(y2, top + imgHeight));
|
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
fixAspectRatio(abs(x2 - x1) < abs(y2 - y1) * aspectRatio);
|
2009-09-10 18:07:33 -04:00
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
if (abs(x2 - x1) > maxWidth) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Selection width is greater than maxWidth */
|
2011-05-15 14:03:51 -04:00
|
|
|
x2 = x1 - maxWidth * (x2 < x1 || -1);
|
|
|
|
fixAspectRatio();
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
if (abs(y2 - y1) > maxHeight) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Selection height is greater than maxHeight */
|
2011-05-15 14:03:51 -04:00
|
|
|
y2 = y1 - maxHeight * (y2 < y1 || -1);
|
|
|
|
fixAspectRatio(true);
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
selection = { x1: selX(min(x1, x2)), x2: selX(max(x1, x2)),
|
|
|
|
y1: selY(min(y1, y2)), y2: selY(max(y1, y2)),
|
|
|
|
width: abs(x2 - x1), height: abs(y2 - y1) };
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
|
|
|
options.onSelectChange(img, getSelection());
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Mousemove event handler triggered when the user is selecting an area
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event object
|
|
|
|
* @return false
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function selectingMouseMove(event) {
|
2012-04-25 17:49:57 -04:00
|
|
|
x2 = /w|e|^$/.test(resize) || aspectRatio ? evX(event) : viewX(selection.x2);
|
|
|
|
y2 = /n|s|^$/.test(resize) || aspectRatio ? evY(event) : viewY(selection.y2);
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
doResize();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Move the selection area
|
|
|
|
*
|
|
|
|
* @param newX1
|
|
|
|
* New viewport X1
|
|
|
|
* @param newY1
|
|
|
|
* New viewport Y1
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function doMove(newX1, newY1) {
|
|
|
|
x2 = (x1 = newX1) + selection.width;
|
|
|
|
y2 = (y1 = newY1) + selection.height;
|
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
$.extend(selection, { x1: selX(x1), y1: selY(y1), x2: selX(x2),
|
|
|
|
y2: selY(y2) });
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
update();
|
|
|
|
|
|
|
|
options.onSelectChange(img, getSelection());
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Mousemove event handler triggered when the selection area is being moved
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event object
|
|
|
|
* @return false
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function movingMouseMove(event) {
|
|
|
|
x1 = max(left, min(startX + evX(event), left + imgWidth - selection.width));
|
|
|
|
y1 = max(top, min(startY + evY(event), top + imgHeight - selection.height));
|
|
|
|
|
|
|
|
doMove(x1, y1);
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Start selection
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function startSelection() {
|
2011-05-15 14:03:51 -04:00
|
|
|
$(document).unbind('mousemove', startSelection);
|
2009-09-10 18:07:33 -04:00
|
|
|
adjust();
|
|
|
|
|
|
|
|
x2 = x1;
|
|
|
|
y2 = y1;
|
|
|
|
doResize();
|
|
|
|
|
|
|
|
resize = '';
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
if (!$outer.is(':visible'))
|
|
|
|
/* Show the plugin elements */
|
2009-09-10 18:07:33 -04:00
|
|
|
$box.add($outer).hide().fadeIn(options.fadeSpeed||0);
|
|
|
|
|
|
|
|
shown = true;
|
|
|
|
|
|
|
|
$(document).unbind('mouseup', cancelSelection)
|
|
|
|
.mousemove(selectingMouseMove).one('mouseup', docMouseUp);
|
|
|
|
$box.unbind('mousemove', areaMouseMove);
|
|
|
|
|
|
|
|
options.onSelectStart(img, getSelection());
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Cancel selection
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function cancelSelection() {
|
2011-05-15 14:03:51 -04:00
|
|
|
$(document).unbind('mousemove', startSelection)
|
|
|
|
.unbind('mouseup', cancelSelection);
|
2009-09-10 18:07:33 -04:00
|
|
|
hide($box.add($outer));
|
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
setSelection(selX(x1), selY(y1), selX(x1), selY(y1));
|
2009-09-10 18:07:33 -04:00
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* If this is an API call, callback functions should not be triggered */
|
|
|
|
if (!this instanceof $.imgAreaSelect) {
|
|
|
|
options.onSelectChange(img, getSelection());
|
|
|
|
options.onSelectEnd(img, getSelection());
|
|
|
|
}
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Image mousedown event handler
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event object
|
|
|
|
* @return false
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function imgMouseDown(event) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Ignore the event if animation is in progress */
|
2009-09-10 18:07:33 -04:00
|
|
|
if (event.which != 1 || $outer.is(':animated')) return false;
|
|
|
|
|
|
|
|
adjust();
|
|
|
|
startX = x1 = evX(event);
|
|
|
|
startY = y1 = evY(event);
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Selection will start when the mouse is moved */
|
2011-05-15 14:03:51 -04:00
|
|
|
$(document).mousemove(startSelection).mouseup(cancelSelection);
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Window resize event handler
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
function windowResize() {
|
2009-09-10 18:07:33 -04:00
|
|
|
doUpdate(false);
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Image load event handler. This is the final part of the initialization
|
|
|
|
* process.
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function imgLoad() {
|
|
|
|
imgLoaded = true;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Set options */
|
2009-09-10 18:07:33 -04:00
|
|
|
setOptions(options = $.extend({
|
|
|
|
classPrefix: 'imgareaselect',
|
|
|
|
movable: true,
|
|
|
|
parent: 'body',
|
2011-05-15 14:03:51 -04:00
|
|
|
resizable: true,
|
|
|
|
resizeMargin: 10,
|
2009-09-10 18:07:33 -04:00
|
|
|
onInit: function () {},
|
|
|
|
onSelectStart: function () {},
|
|
|
|
onSelectChange: function () {},
|
|
|
|
onSelectEnd: function () {}
|
|
|
|
}, options));
|
|
|
|
|
|
|
|
$box.add($outer).css({ visibility: '' });
|
|
|
|
|
|
|
|
if (options.show) {
|
|
|
|
shown = true;
|
|
|
|
adjust();
|
|
|
|
update();
|
|
|
|
$box.add($outer).hide().fadeIn(options.fadeSpeed||0);
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Call the onInit callback. The setTimeout() call is used to ensure
|
|
|
|
* that the plugin has been fully initialized and the object instance is
|
|
|
|
* available (so that it can be obtained in the callback).
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
setTimeout(function () { options.onInit(img, getSelection()); }, 0);
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Document keypress event handler
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event object
|
|
|
|
* @return false
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
var docKeyPress = function(event) {
|
2011-05-15 14:03:51 -04:00
|
|
|
var k = options.keys, d, t, key = event.keyCode;
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
d = !isNaN(k.alt) && (event.altKey || event.originalEvent.altKey) ? k.alt :
|
|
|
|
!isNaN(k.ctrl) && event.ctrlKey ? k.ctrl :
|
|
|
|
!isNaN(k.shift) && event.shiftKey ? k.shift :
|
|
|
|
!isNaN(k.arrows) ? k.arrows : 10;
|
|
|
|
|
|
|
|
if (k.arrows == 'resize' || (k.shift == 'resize' && event.shiftKey) ||
|
|
|
|
(k.ctrl == 'resize' && event.ctrlKey) ||
|
|
|
|
(k.alt == 'resize' && (event.altKey || event.originalEvent.altKey)))
|
|
|
|
{
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Resize selection */
|
|
|
|
|
2009-09-10 18:07:33 -04:00
|
|
|
switch (key) {
|
|
|
|
case 37:
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Left */
|
2009-09-10 18:07:33 -04:00
|
|
|
d = -d;
|
|
|
|
case 39:
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Right */
|
2009-09-10 18:07:33 -04:00
|
|
|
t = max(x1, x2);
|
|
|
|
x1 = min(x1, x2);
|
|
|
|
x2 = max(t + d, x1);
|
2011-05-15 14:03:51 -04:00
|
|
|
fixAspectRatio();
|
2009-09-10 18:07:33 -04:00
|
|
|
break;
|
|
|
|
case 38:
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Up */
|
2009-09-10 18:07:33 -04:00
|
|
|
d = -d;
|
|
|
|
case 40:
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Down */
|
2009-09-10 18:07:33 -04:00
|
|
|
t = max(y1, y2);
|
|
|
|
y1 = min(y1, y2);
|
|
|
|
y2 = max(t + d, y1);
|
2011-05-15 14:03:51 -04:00
|
|
|
fixAspectRatio(true);
|
2009-09-10 18:07:33 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
doResize();
|
|
|
|
}
|
|
|
|
else {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Move selection */
|
|
|
|
|
2009-09-10 18:07:33 -04:00
|
|
|
x1 = min(x1, x2);
|
|
|
|
y1 = min(y1, y2);
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 37:
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Left */
|
2009-09-10 18:07:33 -04:00
|
|
|
doMove(max(x1 - d, left), y1);
|
|
|
|
break;
|
|
|
|
case 38:
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Up */
|
2009-09-10 18:07:33 -04:00
|
|
|
doMove(x1, max(y1 - d, top));
|
|
|
|
break;
|
|
|
|
case 39:
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Right */
|
2009-09-10 18:07:33 -04:00
|
|
|
doMove(x1 + min(d, imgWidth - selX(x2)), y1);
|
|
|
|
break;
|
|
|
|
case 40:
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Down */
|
2009-09-10 18:07:33 -04:00
|
|
|
doMove(x1, y1 + min(d, imgHeight - selY(y2)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Apply style options to plugin element (or multiple elements)
|
|
|
|
*
|
|
|
|
* @param $elem
|
|
|
|
* A jQuery object representing the element(s) to style
|
|
|
|
* @param props
|
|
|
|
* An object that maps option names to corresponding CSS
|
|
|
|
* properties
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function styleOptions($elem, props) {
|
|
|
|
for (option in props)
|
|
|
|
if (options[option] !== undefined)
|
|
|
|
$elem.css(props[option], options[option]);
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Set plugin options
|
|
|
|
*
|
|
|
|
* @param newOptions
|
|
|
|
* The new options object
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
function setOptions(newOptions) {
|
|
|
|
if (newOptions.parent)
|
|
|
|
($parent = $(newOptions.parent)).append($box.add($outer));
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Merge the new options with the existing ones */
|
2011-05-15 14:03:51 -04:00
|
|
|
$.extend(options, newOptions);
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
adjust();
|
|
|
|
|
|
|
|
if (newOptions.handles != null) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Recreate selection area handles */
|
2009-09-10 18:07:33 -04:00
|
|
|
$handles.remove();
|
|
|
|
$handles = $([]);
|
|
|
|
|
|
|
|
i = newOptions.handles ? newOptions.handles == 'corners' ? 4 : 8 : 0;
|
|
|
|
|
|
|
|
while (i--)
|
|
|
|
$handles = $handles.add(div());
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Add a class to handles and set the CSS properties */
|
2009-09-10 18:07:33 -04:00
|
|
|
$handles.addClass(options.classPrefix + '-handle').css({
|
|
|
|
position: 'absolute',
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* The font-size property needs to be set to zero, otherwise
|
|
|
|
* Internet Explorer makes the handles too large
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
fontSize: 0,
|
|
|
|
zIndex: zIndex + 1 || 1
|
|
|
|
});
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* If handle width/height has not been set with CSS rules, set the
|
|
|
|
* default 5px
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
if (!parseInt($handles.css('width')) >= 0)
|
2009-09-10 18:07:33 -04:00
|
|
|
$handles.width(5).height(5);
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* If the borderWidth option is in use, add a solid border to
|
|
|
|
* handles
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
if (o = options.borderWidth)
|
|
|
|
$handles.css({ borderWidth: o, borderStyle: 'solid' });
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Apply other style options */
|
2009-09-10 18:07:33 -04:00
|
|
|
styleOptions($handles, { borderColor1: 'border-color',
|
|
|
|
borderColor2: 'background-color',
|
|
|
|
borderOpacity: 'opacity' });
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Calculate scale factors */
|
2009-09-10 18:07:33 -04:00
|
|
|
scaleX = options.imageWidth / imgWidth || 1;
|
|
|
|
scaleY = options.imageHeight / imgHeight || 1;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Set selection */
|
2009-09-10 18:07:33 -04:00
|
|
|
if (newOptions.x1 != null) {
|
|
|
|
setSelection(newOptions.x1, newOptions.y1, newOptions.x2,
|
2011-05-15 14:03:51 -04:00
|
|
|
newOptions.y2);
|
2009-09-10 18:07:33 -04:00
|
|
|
newOptions.show = !newOptions.hide;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newOptions.keys)
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Enable keyboard support */
|
2009-09-10 18:07:33 -04:00
|
|
|
options.keys = $.extend({ shift: 1, ctrl: 'resize' },
|
|
|
|
newOptions.keys);
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Add classes to plugin elements */
|
2009-09-10 18:07:33 -04:00
|
|
|
$outer.addClass(options.classPrefix + '-outer');
|
|
|
|
$area.addClass(options.classPrefix + '-selection');
|
|
|
|
for (i = 0; i++ < 4;)
|
|
|
|
$($border[i-1]).addClass(options.classPrefix + '-border' + i);
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Apply style options */
|
2009-09-10 18:07:33 -04:00
|
|
|
styleOptions($area, { selectionColor: 'background-color',
|
|
|
|
selectionOpacity: 'opacity' });
|
|
|
|
styleOptions($border, { borderOpacity: 'opacity',
|
|
|
|
borderWidth: 'border-width' });
|
|
|
|
styleOptions($outer, { outerColor: 'background-color',
|
|
|
|
outerOpacity: 'opacity' });
|
|
|
|
if (o = options.borderColor1)
|
|
|
|
$($border[0]).css({ borderStyle: 'solid', borderColor: o });
|
|
|
|
if (o = options.borderColor2)
|
|
|
|
$($border[1]).css({ borderStyle: 'dashed', borderColor: o });
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Append all the selection area elements to the container box */
|
2011-05-15 14:03:51 -04:00
|
|
|
$box.append($area.add($border).add($areaOpera).add($handles));
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
if ($.browser.msie) {
|
2012-04-25 17:49:57 -04:00
|
|
|
if (o = $outer.css('filter').match(/opacity=(\d+)/))
|
2009-09-10 18:07:33 -04:00
|
|
|
$outer.css('opacity', o[1]/100);
|
2012-04-25 17:49:57 -04:00
|
|
|
if (o = $border.css('filter').match(/opacity=(\d+)/))
|
2009-09-10 18:07:33 -04:00
|
|
|
$border.css('opacity', o[1]/100);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newOptions.hide)
|
|
|
|
hide($box.add($outer));
|
|
|
|
else if (newOptions.show && imgLoaded) {
|
|
|
|
shown = true;
|
|
|
|
$box.add($outer).fadeIn(options.fadeSpeed||0);
|
|
|
|
doUpdate();
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Calculate the aspect ratio factor */
|
2009-09-10 18:07:33 -04:00
|
|
|
aspectRatio = (d = (options.aspectRatio || '').split(/:/))[0] / d[1];
|
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
$img.add($outer).unbind('mousedown', imgMouseDown);
|
|
|
|
|
2009-09-10 18:07:33 -04:00
|
|
|
if (options.disable || options.enable === false) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Disable the plugin */
|
2009-09-10 18:07:33 -04:00
|
|
|
$box.unbind('mousemove', areaMouseMove).unbind('mousedown', areaMouseDown);
|
2011-05-15 14:03:51 -04:00
|
|
|
$(window).unbind('resize', windowResize);
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
2011-05-15 14:03:51 -04:00
|
|
|
else {
|
|
|
|
if (options.enable || options.disable === false) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Enable the plugin */
|
2011-05-15 14:03:51 -04:00
|
|
|
if (options.resizable || options.movable)
|
|
|
|
$box.mousemove(areaMouseMove).mousedown(areaMouseDown);
|
|
|
|
|
|
|
|
$(window).resize(windowResize);
|
|
|
|
}
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
if (!options.persistent)
|
|
|
|
$img.add($outer).mousedown(imgMouseDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.enable = options.disable = undefined;
|
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Remove plugin completely
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
this.remove = function () {
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Call setOptions with { disable: true } to unbind the event handlers
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
setOptions({ disable: true });
|
|
|
|
$box.add($outer).remove();
|
|
|
|
};
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Public API
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current options
|
|
|
|
*
|
|
|
|
* @return An object containing the set of options currently in use
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
this.getOptions = function () { return options; };
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Set plugin options
|
|
|
|
*
|
|
|
|
* @param newOptions
|
|
|
|
* The new options object
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
this.setOptions = setOptions;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Get the current selection
|
|
|
|
*
|
|
|
|
* @param noScale
|
|
|
|
* If set to <code>true</code>, scaling is not applied to the
|
|
|
|
* returned selection
|
|
|
|
* @return Selection object
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
this.getSelection = getSelection;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Set the current selection
|
|
|
|
*
|
|
|
|
* @param x1
|
|
|
|
* X coordinate of the upper left corner of the selection area
|
|
|
|
* @param y1
|
|
|
|
* Y coordinate of the upper left corner of the selection area
|
|
|
|
* @param x2
|
|
|
|
* X coordinate of the lower right corner of the selection area
|
|
|
|
* @param y2
|
|
|
|
* Y coordinate of the lower right corner of the selection area
|
|
|
|
* @param noScale
|
|
|
|
* If set to <code>true</code>, scaling is not applied to the
|
|
|
|
* new selection
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
this.setSelection = setSelection;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Cancel selection
|
|
|
|
*/
|
|
|
|
this.cancelSelection = cancelSelection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update plugin elements
|
|
|
|
*
|
|
|
|
* @param resetKeyPress
|
|
|
|
* If set to <code>false</code>, this instance's keypress
|
|
|
|
* event handler is not activated
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
this.update = doUpdate;
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Traverse the image's parent elements (up to <body>) and find the
|
|
|
|
* highest z-index
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$p = $img;
|
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
while ($p.length) {
|
|
|
|
zIndex = max(zIndex,
|
|
|
|
!isNaN($p.css('z-index')) ? $p.css('z-index') : zIndex);
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Also check if any of the ancestor elements has fixed position */
|
2009-09-10 18:07:33 -04:00
|
|
|
if ($p.css('position') == 'fixed')
|
|
|
|
position = 'fixed';
|
|
|
|
|
2011-05-15 14:03:51 -04:00
|
|
|
$p = $p.parent(':not(body)');
|
2009-09-10 18:07:33 -04:00
|
|
|
}
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* If z-index is given as an option, it overrides the one found by the
|
|
|
|
* above loop
|
|
|
|
*/
|
2011-05-15 14:03:51 -04:00
|
|
|
zIndex = options.zIndex || zIndex;
|
2009-09-10 18:07:33 -04:00
|
|
|
|
|
|
|
if ($.browser.msie)
|
|
|
|
$img.attr('unselectable', 'on');
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* In MSIE and WebKit, we need to use the keydown event instead of keypress
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$.imgAreaSelect.keyPress = $.browser.msie ||
|
|
|
|
$.browser.safari ? 'keydown' : 'keypress';
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* There is a bug affecting the CSS cursor property in Opera (observed in
|
|
|
|
* versions up to 10.00) that prevents the cursor from being updated unless
|
|
|
|
* the mouse leaves and enters the element again. To trigger the mouseover
|
|
|
|
* event, we're adding an additional div to $box and we're going to toggle
|
|
|
|
* it when mouse moves inside the selection area.
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
if ($.browser.opera)
|
|
|
|
$areaOpera = div().css({ width: '100%', height: '100%',
|
|
|
|
position: 'absolute', zIndex: zIndex + 2 || 2 });
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* We initially set visibility to "hidden" as a workaround for a weird
|
|
|
|
* behaviour observed in Google Chrome 1.0.154.53 (on Windows XP). Normally
|
|
|
|
* we would just set display to "none", but, for some reason, if we do so
|
|
|
|
* then Chrome refuses to later display the element with .show() or
|
|
|
|
* .fadeIn().
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$box.add($outer).css({ visibility: 'hidden', position: position,
|
|
|
|
overflow: 'hidden', zIndex: zIndex || '0' });
|
|
|
|
$box.css({ zIndex: zIndex + 2 || 2 });
|
2011-05-15 14:03:51 -04:00
|
|
|
$area.add($border).css({ position: 'absolute', fontSize: 0 });
|
2009-09-10 18:07:33 -04:00
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* If the image has been fully loaded, or if it is not really an image (eg.
|
|
|
|
* a div), call imgLoad() immediately; otherwise, bind it to be called once
|
|
|
|
* on image load event.
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
img.complete || img.readyState == 'complete' || !$img.is('img') ?
|
|
|
|
imgLoad() : $img.one('load', imgLoad);
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* MSIE 9.0 doesn't always fire the image load event -- resetting the src
|
|
|
|
* attribute seems to trigger it. The check is for version 7 and above to
|
|
|
|
* accommodate for MSIE 9 running in compatibility mode.
|
|
|
|
*/
|
|
|
|
if ($.browser.msie && $.browser.version >= 7)
|
2011-05-15 14:03:51 -04:00
|
|
|
img.src = img.src;
|
2009-09-10 18:07:33 -04:00
|
|
|
};
|
|
|
|
|
2012-04-25 17:49:57 -04:00
|
|
|
/**
|
|
|
|
* Invoke imgAreaSelect on a jQuery object containing the image(s)
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
* Options object
|
|
|
|
* @return The jQuery object or a reference to imgAreaSelect instance (if the
|
|
|
|
* <code>instance</code> option was specified)
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
$.fn.imgAreaSelect = function (options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
this.each(function () {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Is there already an imgAreaSelect instance bound to this element? */
|
2011-05-15 14:03:51 -04:00
|
|
|
if ($(this).data('imgAreaSelect')) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Yes there is -- is it supposed to be removed? */
|
2011-05-15 14:03:51 -04:00
|
|
|
if (options.remove) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Remove the plugin */
|
2011-05-15 14:03:51 -04:00
|
|
|
$(this).data('imgAreaSelect').remove();
|
|
|
|
$(this).removeData('imgAreaSelect');
|
|
|
|
}
|
|
|
|
else
|
2012-04-25 17:49:57 -04:00
|
|
|
/* Reset options */
|
2011-05-15 14:03:51 -04:00
|
|
|
$(this).data('imgAreaSelect').setOptions(options);
|
|
|
|
}
|
|
|
|
else if (!options.remove) {
|
2012-04-25 17:49:57 -04:00
|
|
|
/* No exising instance -- create a new one */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If neither the "enable" nor the "disable" option is present, add
|
|
|
|
* "enable" as the default
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
if (options.enable === undefined && options.disable === undefined)
|
|
|
|
options.enable = true;
|
|
|
|
|
|
|
|
$(this).data('imgAreaSelect', new $.imgAreaSelect(this, options));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.instance)
|
2012-04-25 17:49:57 -04:00
|
|
|
/*
|
|
|
|
* Return the imgAreaSelect instance bound to the first element in the
|
|
|
|
* set
|
|
|
|
*/
|
2009-09-10 18:07:33 -04:00
|
|
|
return $(this).data('imgAreaSelect');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
})(jQuery);
|