2 * imgAreaSelect jQuery plugin
5 * Copyright (c) 2008-2011 Michal Wojciechowski (odyniec.net)
7 * Dual licensed under the MIT (MIT-LICENSE.txt)
8 * and GPL (GPL-LICENSE.txt) licenses.
10 * http://odyniec.net/projects/imgareaselect/
17 * Math functions will be used extensively, so it's convenient to make a few
26 * Create a new HTML div element
28 * @return A jQuery object representing the new element
35 * imgAreaSelect initialization
38 * A HTML image element to attach the plugin to
42 $.imgAreaSelect = function (img, options) {
44 /* jQuery object representing the image */
47 /* Has the image finished loading? */
56 /* Border (four divs) */
57 $border = div().add(div()).add(div()).add(div()),
58 /* Outer area (four divs) */
59 $outer = div().add(div()).add(div()).add(div()),
60 /* Handles (empty by default, initialized in setOptions()) */
64 * Additional element to work around a cursor problem in Opera
69 /* Image position (relative to viewport) */
72 /* Image offset (as returned by .offset()) */
73 imgOfs = { left: 0, top: 0 },
75 /* Image dimensions (as returned by .width() and .height()) */
79 * jQuery object representing the parent element that the plugin
80 * elements are appended to
84 /* Parent element offset (as returned by .offset()) */
85 parOfs = { left: 0, top: 0 },
87 /* Base z-index for plugin elements */
90 /* Plugin elements position */
91 position = 'absolute',
93 /* X/Y coordinates of the starting point for move/resize operations */
96 /* Horizontal and vertical scaling factors */
99 /* Current resize mode ("nw", "se", etc.) */
102 /* Selection area constraints */
103 minWidth, minHeight, maxWidth, maxHeight,
105 /* Aspect ratio to maintain (floating point number) */
108 /* Are the plugin elements currently displayed? */
111 /* Current selection (relative to parent element) */
114 /* Current selection (relative to scaled image) */
115 selection = { x1: 0, y1: 0, x2: 0, y2: 0, width: 0, height: 0 },
117 /* Document element */
118 docElem = document.documentElement,
120 /* Various helper variables used throughout the code */
121 $p, d, i, o, w, h, adjusted;
124 * Translate selection coordinates (relative to scaled image) to viewport
125 * coordinates (relative to parent element)
129 * Translate selection X to viewport X
136 return x + imgOfs.left - parOfs.left;
140 * Translate selection Y to viewport Y
147 return y + imgOfs.top - parOfs.top;
151 * Translate viewport coordinates to selection coordinates
155 * Translate viewport X to selection X
159 * @return Selection X
162 return x - imgOfs.left + parOfs.left;
166 * Translate viewport Y to selection Y
170 * @return Selection Y
173 return y - imgOfs.top + parOfs.top;
177 * Translate event coordinates (relative to document) to viewport
182 * Get event X and translate it to viewport X
188 function evX(event) {
189 return event.pageX - parOfs.left;
193 * Get event Y and translate it to viewport Y
199 function evY(event) {
200 return event.pageY - parOfs.top;
204 * Get the current selection
207 * If set to <code>true</code>, scaling is not applied to the
209 * @return Selection object
211 function getSelection(noScale) {
212 var sx = noScale || scaleX, sy = noScale || scaleY;
214 return { x1: round(selection.x1 * sx),
215 y1: round(selection.y1 * sy),
216 x2: round(selection.x2 * sx),
217 y2: round(selection.y2 * sy),
218 width: round(selection.x2 * sx) - round(selection.x1 * sx),
219 height: round(selection.y2 * sy) - round(selection.y1 * sy) };
223 * Set the current selection
226 * X coordinate of the upper left corner of the selection area
228 * Y coordinate of the upper left corner of the selection area
230 * X coordinate of the lower right corner of the selection area
232 * Y coordinate of the lower right corner of the selection area
234 * If set to <code>true</code>, scaling is not applied to the
237 function setSelection(x1, y1, x2, y2, noScale) {
238 var sx = noScale || scaleX, sy = noScale || scaleY;
241 x1: round(x1 / sx || 0),
242 y1: round(y1 / sy || 0),
243 x2: round(x2 / sx || 0),
244 y2: round(y2 / sy || 0)
247 selection.width = selection.x2 - selection.x1;
248 selection.height = selection.y2 - selection.y1;
252 * Recalculate image and parent offsets
256 * Do not adjust if image width is not a positive number. This might
257 * happen when imgAreaSelect is put on a parent element which is then
264 * Get image offset. The .offset() method returns float values, so they
265 * need to be rounded.
267 imgOfs = { left: round($img.offset().left), top: round($img.offset().top) };
269 /* Get image dimensions */
270 imgWidth = $img.innerWidth();
271 imgHeight = $img.innerHeight();
273 imgOfs.top += ($img.outerHeight() - imgHeight) >> 1;
274 imgOfs.left += ($img.outerWidth() - imgWidth) >> 1;
276 /* Set minimum and maximum selection area dimensions */
277 minWidth = round(options.minWidth / scaleX) || 0;
278 minHeight = round(options.minHeight / scaleY) || 0;
279 maxWidth = round(min(options.maxWidth / scaleX || 1<<24, imgWidth));
280 maxHeight = round(min(options.maxHeight / scaleY || 1<<24, imgHeight));
283 * Workaround for jQuery 1.3.2 incorrect offset calculation, originally
284 * observed in Safari 3. Firefox 2 is also affected.
286 if ($().jquery == '1.3.2' && position == 'fixed' &&
287 !docElem['getBoundingClientRect'])
289 imgOfs.top += max(document.body.scrollTop, docElem.scrollTop);
290 imgOfs.left += max(document.body.scrollLeft, docElem.scrollLeft);
293 /* Determine parent element offset */
294 parOfs = /absolute|relative/.test($parent.css('position')) ?
295 { left: round($parent.offset().left) - $parent.scrollLeft(),
296 top: round($parent.offset().top) - $parent.scrollTop() } :
297 position == 'fixed' ?
298 { left: $(document).scrollLeft(), top: $(document).scrollTop() } :
305 * Check if selection area is within image boundaries, adjust if
308 if (selection.x2 > imgWidth || selection.y2 > imgHeight)
313 * Update plugin elements
315 * @param resetKeyPress
316 * If set to <code>false</code>, this instance's keypress
317 * event handler is not activated
319 function update(resetKeyPress) {
320 /* If plugin elements are hidden, do nothing */
324 * Set the position and size of the container box and the selection area
327 $box.css({ left: viewX(selection.x1), top: viewY(selection.y1) })
328 .add($area).width(w = selection.width).height(h = selection.height);
331 * Reset the position of selection area, borders, and handles (IE6/IE7
332 * position them incorrectly if we don't do this)
334 $area.add($border).add($handles).css({ left: 0, top: 0 });
336 /* Set border dimensions */
338 .width(max(w - $border.outerWidth() + $border.innerWidth(), 0))
339 .height(max(h - $border.outerHeight() + $border.innerHeight(), 0));
341 /* Arrange the outer area elements */
342 $($outer[0]).css({ left: left, top: top,
343 width: selection.x1, height: imgHeight });
344 $($outer[1]).css({ left: left + selection.x1, top: top,
345 width: w, height: selection.y1 });
346 $($outer[2]).css({ left: left + selection.x2, top: top,
347 width: imgWidth - selection.x2, height: imgHeight });
348 $($outer[3]).css({ left: left + selection.x1, top: top + selection.y2,
349 width: w, height: imgHeight - selection.y2 });
351 w -= $handles.outerWidth();
352 h -= $handles.outerHeight();
354 /* Arrange handles */
355 switch ($handles.length) {
357 $($handles[4]).css({ left: w >> 1 });
358 $($handles[5]).css({ left: w, top: h >> 1 });
359 $($handles[6]).css({ left: w >> 1, top: h });
360 $($handles[7]).css({ top: h >> 1 });
362 $handles.slice(1,3).css({ left: w });
363 $handles.slice(2,4).css({ top: h });
366 if (resetKeyPress !== false) {
368 * Need to reset the document keypress event handler -- unbind the
371 if ($.imgAreaSelect.keyPress != docKeyPress)
372 $(document).unbind($.imgAreaSelect.keyPress,
373 $.imgAreaSelect.onKeyPress);
377 * Set the document keypress event handler to this instance's
378 * docKeyPress() function
380 $(document)[$.imgAreaSelect.keyPress](
381 $.imgAreaSelect.onKeyPress = docKeyPress);
385 * Internet Explorer displays 1px-wide dashed borders incorrectly by
386 * filling the spaces between dashes with white. Toggling the margin
387 * property between 0 and "auto" fixes this in IE6 and IE7 (IE8 is still
388 * broken). This workaround is not perfect, as it requires setTimeout()
389 * and thus causes the border to flicker a bit, but I haven't found a
392 * Note: This only happens with CSS borders, set with the borderWidth,
393 * borderOpacity, borderColor1, and borderColor2 options (which are now
394 * deprecated). Borders created with GIF background images are fine.
396 if ($.browser.msie && $border.outerWidth() - $border.innerWidth() == 2) {
397 $border.css('margin', 0);
398 setTimeout(function () { $border.css('margin', 'auto'); }, 0);
403 * Do the complete update sequence: recalculate offsets, update the
404 * elements, and set the correct values of x1, y1, x2, and y2.
406 * @param resetKeyPress
407 * If set to <code>false</code>, this instance's keypress
408 * event handler is not activated
410 function doUpdate(resetKeyPress) {
412 update(resetKeyPress);
413 x1 = viewX(selection.x1); y1 = viewY(selection.y1);
414 x2 = viewX(selection.x2); y2 = viewY(selection.y2);
418 * Hide or fade out an element (or multiple elements)
421 * A jQuery object containing the element(s) to hide/fade out
423 * Callback function to be called when fadeOut() completes
425 function hide($elem, fn) {
426 options.fadeSpeed ? $elem.fadeOut(options.fadeSpeed, fn) : $elem.hide();
430 * Selection area mousemove event handler
435 function areaMouseMove(event) {
436 var x = selX(evX(event)) - selection.x1,
437 y = selY(evY(event)) - selection.y1;
443 $box.one('mouseout', function () { adjusted = false; });
446 /* Clear the resize mode */
449 if (options.resizable) {
451 * Check if the mouse pointer is over the resize margin area and set
452 * the resize mode accordingly
454 if (y <= options.resizeMargin)
456 else if (y >= selection.height - options.resizeMargin)
458 if (x <= options.resizeMargin)
460 else if (x >= selection.width - options.resizeMargin)
464 $box.css('cursor', resize ? resize + '-resize' :
465 options.movable ? 'move' : '');
471 * Document mouseup event handler
476 function docMouseUp(event) {
477 /* Set back the default cursor */
478 $('body').css('cursor', '');
480 * If autoHide is enabled, or if the selection has zero width/height,
481 * hide the selection and the outer area
483 if (options.autoHide || selection.width * selection.height == 0)
484 hide($box.add($outer), function () { $(this).hide(); });
486 $(document).unbind('mousemove', selectingMouseMove);
487 $box.mousemove(areaMouseMove);
489 options.onSelectEnd(img, getSelection());
493 * Selection area mousedown event handler
499 function areaMouseDown(event) {
500 if (event.which != 1) return false;
505 /* Resize mode is in effect */
506 $('body').css('cursor', resize + '-resize');
508 x1 = viewX(selection[/w/.test(resize) ? 'x2' : 'x1']);
509 y1 = viewY(selection[/n/.test(resize) ? 'y2' : 'y1']);
511 $(document).mousemove(selectingMouseMove)
512 .one('mouseup', docMouseUp);
513 $box.unbind('mousemove', areaMouseMove);
515 else if (options.movable) {
516 startX = left + selection.x1 - evX(event);
517 startY = top + selection.y1 - evY(event);
519 $box.unbind('mousemove', areaMouseMove);
521 $(document).mousemove(movingMouseMove)
522 .one('mouseup', function () {
523 options.onSelectEnd(img, getSelection());
525 $(document).unbind('mousemove', movingMouseMove);
526 $box.mousemove(areaMouseMove);
530 $img.mousedown(event);
536 * Adjust the x2/y2 coordinates to maintain aspect ratio (if defined)
539 * If set to <code>true</code>, calculate x2 first. Otherwise,
540 * calculate y2 first.
542 function fixAspectRatio(xFirst) {
545 x2 = max(left, min(left + imgWidth,
546 x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1)));
547 y2 = round(max(top, min(top + imgHeight,
548 y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1))));
552 y2 = max(top, min(top + imgHeight,
553 y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1)));
554 x2 = round(max(left, min(left + imgWidth,
555 x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1))));
561 * Resize the selection area respecting the minimum/maximum dimensions and
564 function doResize() {
566 * Make sure the top left corner of the selection area stays within
567 * image boundaries (it might not if the image source was dynamically
570 x1 = min(x1, left + imgWidth);
571 y1 = min(y1, top + imgHeight);
573 if (abs(x2 - x1) < minWidth) {
574 /* Selection width is smaller than minWidth */
575 x2 = x1 - minWidth * (x2 < x1 || -1);
578 x1 = left + minWidth;
579 else if (x2 > left + imgWidth)
580 x1 = left + imgWidth - minWidth;
583 if (abs(y2 - y1) < minHeight) {
584 /* Selection height is smaller than minHeight */
585 y2 = y1 - minHeight * (y2 < y1 || -1);
588 y1 = top + minHeight;
589 else if (y2 > top + imgHeight)
590 y1 = top + imgHeight - minHeight;
593 x2 = max(left, min(x2, left + imgWidth));
594 y2 = max(top, min(y2, top + imgHeight));
596 fixAspectRatio(abs(x2 - x1) < abs(y2 - y1) * aspectRatio);
598 if (abs(x2 - x1) > maxWidth) {
599 /* Selection width is greater than maxWidth */
600 x2 = x1 - maxWidth * (x2 < x1 || -1);
604 if (abs(y2 - y1) > maxHeight) {
605 /* Selection height is greater than maxHeight */
606 y2 = y1 - maxHeight * (y2 < y1 || -1);
607 fixAspectRatio(true);
610 selection = { x1: selX(min(x1, x2)), x2: selX(max(x1, x2)),
611 y1: selY(min(y1, y2)), y2: selY(max(y1, y2)),
612 width: abs(x2 - x1), height: abs(y2 - y1) };
616 options.onSelectChange(img, getSelection());
620 * Mousemove event handler triggered when the user is selecting an area
626 function selectingMouseMove(event) {
627 x2 = /w|e|^$/.test(resize) || aspectRatio ? evX(event) : viewX(selection.x2);
628 y2 = /n|s|^$/.test(resize) || aspectRatio ? evY(event) : viewY(selection.y2);
636 * Move the selection area
643 function doMove(newX1, newY1) {
644 x2 = (x1 = newX1) + selection.width;
645 y2 = (y1 = newY1) + selection.height;
647 $.extend(selection, { x1: selX(x1), y1: selY(y1), x2: selX(x2),
652 options.onSelectChange(img, getSelection());
656 * Mousemove event handler triggered when the selection area is being moved
662 function movingMouseMove(event) {
663 x1 = max(left, min(startX + evX(event), left + imgWidth - selection.width));
664 y1 = max(top, min(startY + evY(event), top + imgHeight - selection.height));
668 event.preventDefault();
675 function startSelection() {
676 $(document).unbind('mousemove', startSelection);
685 if (!$outer.is(':visible'))
686 /* Show the plugin elements */
687 $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
691 $(document).unbind('mouseup', cancelSelection)
692 .mousemove(selectingMouseMove).one('mouseup', docMouseUp);
693 $box.unbind('mousemove', areaMouseMove);
695 options.onSelectStart(img, getSelection());
701 function cancelSelection() {
702 $(document).unbind('mousemove', startSelection)
703 .unbind('mouseup', cancelSelection);
704 hide($box.add($outer));
706 setSelection(selX(x1), selY(y1), selX(x1), selY(y1));
708 /* If this is an API call, callback functions should not be triggered */
709 if (!(this instanceof $.imgAreaSelect)) {
710 options.onSelectChange(img, getSelection());
711 options.onSelectEnd(img, getSelection());
716 * Image mousedown event handler
722 function imgMouseDown(event) {
723 /* Ignore the event if animation is in progress */
724 if (event.which != 1 || $outer.is(':animated')) return false;
727 startX = x1 = evX(event);
728 startY = y1 = evY(event);
730 /* Selection will start when the mouse is moved */
731 $(document).mousemove(startSelection).mouseup(cancelSelection);
737 * Window resize event handler
739 function windowResize() {
744 * Image load event handler. This is the final part of the initialization
751 setOptions(options = $.extend({
752 classPrefix: 'imgareaselect',
757 onInit: function () {},
758 onSelectStart: function () {},
759 onSelectChange: function () {},
760 onSelectEnd: function () {}
763 $box.add($outer).css({ visibility: '' });
769 $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
773 * Call the onInit callback. The setTimeout() call is used to ensure
774 * that the plugin has been fully initialized and the object instance is
775 * available (so that it can be obtained in the callback).
777 setTimeout(function () { options.onInit(img, getSelection()); }, 0);
781 * Document keypress event handler
787 var docKeyPress = function(event) {
788 var k = options.keys, d, t, key = event.keyCode;
790 d = !isNaN(k.alt) && (event.altKey || event.originalEvent.altKey) ? k.alt :
791 !isNaN(k.ctrl) && event.ctrlKey ? k.ctrl :
792 !isNaN(k.shift) && event.shiftKey ? k.shift :
793 !isNaN(k.arrows) ? k.arrows : 10;
795 if (k.arrows == 'resize' || (k.shift == 'resize' && event.shiftKey) ||
796 (k.ctrl == 'resize' && event.ctrlKey) ||
797 (k.alt == 'resize' && (event.altKey || event.originalEvent.altKey)))
799 /* Resize selection */
820 fixAspectRatio(true);
837 doMove(max(x1 - d, left), y1);
841 doMove(x1, max(y1 - d, top));
845 doMove(x1 + min(d, imgWidth - selX(x2)), y1);
849 doMove(x1, y1 + min(d, imgHeight - selY(y2)));
860 * Apply style options to plugin element (or multiple elements)
863 * A jQuery object representing the element(s) to style
865 * An object that maps option names to corresponding CSS
868 function styleOptions($elem, props) {
869 for (var option in props)
870 if (options[option] !== undefined)
871 $elem.css(props[option], options[option]);
878 * The new options object
880 function setOptions(newOptions) {
881 if (newOptions.parent)
882 ($parent = $(newOptions.parent)).append($box.add($outer));
884 /* Merge the new options with the existing ones */
885 $.extend(options, newOptions);
889 if (newOptions.handles != null) {
890 /* Recreate selection area handles */
894 i = newOptions.handles ? newOptions.handles == 'corners' ? 4 : 8 : 0;
897 $handles = $handles.add(div());
899 /* Add a class to handles and set the CSS properties */
900 $handles.addClass(options.classPrefix + '-handle').css({
901 position: 'absolute',
903 * The font-size property needs to be set to zero, otherwise
904 * Internet Explorer makes the handles too large
907 zIndex: zIndex + 1 || 1
911 * If handle width/height has not been set with CSS rules, set the
914 if (!parseInt($handles.css('width')) >= 0)
915 $handles.width(5).height(5);
918 * If the borderWidth option is in use, add a solid border to
921 if (o = options.borderWidth)
922 $handles.css({ borderWidth: o, borderStyle: 'solid' });
924 /* Apply other style options */
925 styleOptions($handles, { borderColor1: 'border-color',
926 borderColor2: 'background-color',
927 borderOpacity: 'opacity' });
930 /* Calculate scale factors */
931 scaleX = options.imageWidth / imgWidth || 1;
932 scaleY = options.imageHeight / imgHeight || 1;
935 if (newOptions.x1 != null) {
936 setSelection(newOptions.x1, newOptions.y1, newOptions.x2,
938 newOptions.show = !newOptions.hide;
942 /* Enable keyboard support */
943 options.keys = $.extend({ shift: 1, ctrl: 'resize' },
946 /* Add classes to plugin elements */
947 $outer.addClass(options.classPrefix + '-outer');
948 $area.addClass(options.classPrefix + '-selection');
949 for (i = 0; i++ < 4;)
950 $($border[i-1]).addClass(options.classPrefix + '-border' + i);
952 /* Apply style options */
953 styleOptions($area, { selectionColor: 'background-color',
954 selectionOpacity: 'opacity' });
955 styleOptions($border, { borderOpacity: 'opacity',
956 borderWidth: 'border-width' });
957 styleOptions($outer, { outerColor: 'background-color',
958 outerOpacity: 'opacity' });
959 if (o = options.borderColor1)
960 $($border[0]).css({ borderStyle: 'solid', borderColor: o });
961 if (o = options.borderColor2)
962 $($border[1]).css({ borderStyle: 'dashed', borderColor: o });
964 /* Append all the selection area elements to the container box */
965 $box.append($area.add($border).add($areaOpera).add($handles));
967 if ($.browser.msie) {
968 if (o = $outer.css('filter').match(/opacity=(\d+)/))
969 $outer.css('opacity', o[1]/100);
970 if (o = $border.css('filter').match(/opacity=(\d+)/))
971 $border.css('opacity', o[1]/100);
975 hide($box.add($outer));
976 else if (newOptions.show && imgLoaded) {
978 $box.add($outer).fadeIn(options.fadeSpeed||0);
982 /* Calculate the aspect ratio factor */
983 aspectRatio = (d = (options.aspectRatio || '').split(/:/))[0] / d[1];
985 $img.add($outer).unbind('mousedown', imgMouseDown);
987 if (options.disable || options.enable === false) {
988 /* Disable the plugin */
989 $box.unbind('mousemove', areaMouseMove).unbind('mousedown', areaMouseDown);
990 $(window).unbind('resize', windowResize);
993 if (options.enable || options.disable === false) {
994 /* Enable the plugin */
995 if (options.resizable || options.movable)
996 $box.mousemove(areaMouseMove).mousedown(areaMouseDown);
998 $(window).resize(windowResize);
1001 if (!options.persistent)
1002 $img.add($outer).mousedown(imgMouseDown);
1005 options.enable = options.disable = undefined;
1009 * Remove plugin completely
1011 this.remove = function () {
1013 * Call setOptions with { disable: true } to unbind the event handlers
1015 setOptions({ disable: true });
1016 $box.add($outer).remove();
1024 * Get current options
1026 * @return An object containing the set of options currently in use
1028 this.getOptions = function () { return options; };
1031 * Set plugin options
1034 * The new options object
1036 this.setOptions = setOptions;
1039 * Get the current selection
1042 * If set to <code>true</code>, scaling is not applied to the
1043 * returned selection
1044 * @return Selection object
1046 this.getSelection = getSelection;
1049 * Set the current selection
1052 * X coordinate of the upper left corner of the selection area
1054 * Y coordinate of the upper left corner of the selection area
1056 * X coordinate of the lower right corner of the selection area
1058 * Y coordinate of the lower right corner of the selection area
1060 * If set to <code>true</code>, scaling is not applied to the
1063 this.setSelection = setSelection;
1068 this.cancelSelection = cancelSelection;
1071 * Update plugin elements
1073 * @param resetKeyPress
1074 * If set to <code>false</code>, this instance's keypress
1075 * event handler is not activated
1077 this.update = doUpdate;
1080 * Traverse the image's parent elements (up to <body>) and find the
1086 zIndex = max(zIndex,
1087 !isNaN($p.css('z-index')) ? $p.css('z-index') : zIndex);
1088 /* Also check if any of the ancestor elements has fixed position */
1089 if ($p.css('position') == 'fixed')
1092 $p = $p.parent(':not(body)');
1096 * If z-index is given as an option, it overrides the one found by the
1099 zIndex = options.zIndex || zIndex;
1102 $img.attr('unselectable', 'on');
1105 * In MSIE and WebKit, we need to use the keydown event instead of keypress
1107 $.imgAreaSelect.keyPress = $.browser.msie ||
1108 $.browser.safari ? 'keydown' : 'keypress';
1111 * There is a bug affecting the CSS cursor property in Opera (observed in
1112 * versions up to 10.00) that prevents the cursor from being updated unless
1113 * the mouse leaves and enters the element again. To trigger the mouseover
1114 * event, we're adding an additional div to $box and we're going to toggle
1115 * it when mouse moves inside the selection area.
1117 if ($.browser.opera)
1118 $areaOpera = div().css({ width: '100%', height: '100%',
1119 position: 'absolute', zIndex: zIndex + 2 || 2 });
1122 * We initially set visibility to "hidden" as a workaround for a weird
1123 * behaviour observed in Google Chrome 1.0.154.53 (on Windows XP). Normally
1124 * we would just set display to "none", but, for some reason, if we do so
1125 * then Chrome refuses to later display the element with .show() or
1128 $box.add($outer).css({ visibility: 'hidden', position: position,
1129 overflow: 'hidden', zIndex: zIndex || '0' });
1130 $box.css({ zIndex: zIndex + 2 || 2 });
1131 $area.add($border).css({ position: 'absolute', fontSize: 0 });
1134 * If the image has been fully loaded, or if it is not really an image (eg.
1135 * a div), call imgLoad() immediately; otherwise, bind it to be called once
1136 * on image load event.
1138 img.complete || img.readyState == 'complete' || !$img.is('img') ?
1139 imgLoad() : $img.one('load', imgLoad);
1142 * MSIE 9.0 doesn't always fire the image load event -- resetting the src
1143 * attribute seems to trigger it. The check is for version 7 and above to
1144 * accommodate for MSIE 9 running in compatibility mode.
1146 if (!imgLoaded && $.browser.msie && $.browser.version >= 7)
1151 * Invoke imgAreaSelect on a jQuery object containing the image(s)
1155 * @return The jQuery object or a reference to imgAreaSelect instance (if the
1156 * <code>instance</code> option was specified)
1158 $.fn.imgAreaSelect = function (options) {
1159 options = options || {};
1161 this.each(function () {
1162 /* Is there already an imgAreaSelect instance bound to this element? */
1163 if ($(this).data('imgAreaSelect')) {
1164 /* Yes there is -- is it supposed to be removed? */
1165 if (options.remove) {
1166 /* Remove the plugin */
1167 $(this).data('imgAreaSelect').remove();
1168 $(this).removeData('imgAreaSelect');
1172 $(this).data('imgAreaSelect').setOptions(options);
1174 else if (!options.remove) {
1175 /* No exising instance -- create a new one */
1178 * If neither the "enable" nor the "disable" option is present, add
1179 * "enable" as the default
1181 if (options.enable === undefined && options.disable === undefined)
1182 options.enable = true;
1184 $(this).data('imgAreaSelect', new $.imgAreaSelect(this, options));
1188 if (options.instance)
1190 * Return the imgAreaSelect instance bound to the first element in the
1193 return $(this).data('imgAreaSelect');