2 * imgAreaSelect jQuery plugin
5 * Copyright (c) 2008-2013 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,
121 ua = navigator.userAgent,
123 /* Various helper variables used throughout the code */
124 $p, d, i, o, w, h, adjusted;
127 * Translate selection coordinates (relative to scaled image) to viewport
128 * coordinates (relative to parent element)
132 * Translate selection X to viewport X
139 return x + imgOfs.left - parOfs.left;
143 * Translate selection Y to viewport Y
150 return y + imgOfs.top - parOfs.top;
154 * Translate viewport coordinates to selection coordinates
158 * Translate viewport X to selection X
162 * @return Selection X
165 return x - imgOfs.left + parOfs.left;
169 * Translate viewport Y to selection Y
173 * @return Selection Y
176 return y - imgOfs.top + parOfs.top;
180 * Translate event coordinates (relative to document) to viewport
185 * Get event X and translate it to viewport X
191 function evX(event) {
192 return event.pageX - parOfs.left;
196 * Get event Y and translate it to viewport Y
202 function evY(event) {
203 return event.pageY - parOfs.top;
207 * Get the current selection
210 * If set to <code>true</code>, scaling is not applied to the
212 * @return Selection object
214 function getSelection(noScale) {
215 var sx = noScale || scaleX, sy = noScale || scaleY;
217 return { x1: round(selection.x1 * sx),
218 y1: round(selection.y1 * sy),
219 x2: round(selection.x2 * sx),
220 y2: round(selection.y2 * sy),
221 width: round(selection.x2 * sx) - round(selection.x1 * sx),
222 height: round(selection.y2 * sy) - round(selection.y1 * sy) };
226 * Set the current selection
229 * X coordinate of the upper left corner of the selection area
231 * Y coordinate of the upper left corner of the selection area
233 * X coordinate of the lower right corner of the selection area
235 * Y coordinate of the lower right corner of the selection area
237 * If set to <code>true</code>, scaling is not applied to the
240 function setSelection(x1, y1, x2, y2, noScale) {
241 var sx = noScale || scaleX, sy = noScale || scaleY;
244 x1: round(x1 / sx || 0),
245 y1: round(y1 / sy || 0),
246 x2: round(x2 / sx || 0),
247 y2: round(y2 / sy || 0)
250 selection.width = selection.x2 - selection.x1;
251 selection.height = selection.y2 - selection.y1;
255 * Recalculate image and parent offsets
259 * Do not adjust if image has not yet loaded or if width is not a
260 * positive number. The latter might happen when imgAreaSelect is put
261 * on a parent element which is then hidden.
263 if (!imgLoaded || !$img.width())
267 * Get image offset. The .offset() method returns float values, so they
268 * need to be rounded.
270 imgOfs = { left: round($img.offset().left), top: round($img.offset().top) };
272 /* Get image dimensions */
273 imgWidth = $img.innerWidth();
274 imgHeight = $img.innerHeight();
276 imgOfs.top += ($img.outerHeight() - imgHeight) >> 1;
277 imgOfs.left += ($img.outerWidth() - imgWidth) >> 1;
279 /* Set minimum and maximum selection area dimensions */
280 minWidth = round(options.minWidth / scaleX) || 0;
281 minHeight = round(options.minHeight / scaleY) || 0;
282 maxWidth = round(min(options.maxWidth / scaleX || 1<<24, imgWidth));
283 maxHeight = round(min(options.maxHeight / scaleY || 1<<24, imgHeight));
286 * Workaround for jQuery 1.3.2 incorrect offset calculation, originally
287 * observed in Safari 3. Firefox 2 is also affected.
289 if ($().jquery == '1.3.2' && position == 'fixed' &&
290 !docElem['getBoundingClientRect'])
292 imgOfs.top += max(document.body.scrollTop, docElem.scrollTop);
293 imgOfs.left += max(document.body.scrollLeft, docElem.scrollLeft);
296 /* Determine parent element offset */
297 parOfs = /absolute|relative/.test($parent.css('position')) ?
298 { left: round($parent.offset().left) - $parent.scrollLeft(),
299 top: round($parent.offset().top) - $parent.scrollTop() } :
300 position == 'fixed' ?
301 { left: $(document).scrollLeft(), top: $(document).scrollTop() } :
308 * Check if selection area is within image boundaries, adjust if
311 if (selection.x2 > imgWidth || selection.y2 > imgHeight)
316 * Update plugin elements
318 * @param resetKeyPress
319 * If set to <code>false</code>, this instance's keypress
320 * event handler is not activated
322 function update(resetKeyPress) {
323 /* If plugin elements are hidden, do nothing */
327 * Set the position and size of the container box and the selection area
330 $box.css({ left: viewX(selection.x1), top: viewY(selection.y1) })
331 .add($area).width(w = selection.width).height(h = selection.height);
334 * Reset the position of selection area, borders, and handles (IE6/IE7
335 * position them incorrectly if we don't do this)
337 $area.add($border).add($handles).css({ left: 0, top: 0 });
339 /* Set border dimensions */
341 .width(max(w - $border.outerWidth() + $border.innerWidth(), 0))
342 .height(max(h - $border.outerHeight() + $border.innerHeight(), 0));
344 /* Arrange the outer area elements */
345 $($outer[0]).css({ left: left, top: top,
346 width: selection.x1, height: imgHeight });
347 $($outer[1]).css({ left: left + selection.x1, top: top,
348 width: w, height: selection.y1 });
349 $($outer[2]).css({ left: left + selection.x2, top: top,
350 width: imgWidth - selection.x2, height: imgHeight });
351 $($outer[3]).css({ left: left + selection.x1, top: top + selection.y2,
352 width: w, height: imgHeight - selection.y2 });
354 w -= $handles.outerWidth();
355 h -= $handles.outerHeight();
357 /* Arrange handles */
358 switch ($handles.length) {
360 $($handles[4]).css({ left: w >> 1 });
361 $($handles[5]).css({ left: w, top: h >> 1 });
362 $($handles[6]).css({ left: w >> 1, top: h });
363 $($handles[7]).css({ top: h >> 1 });
365 $handles.slice(1,3).css({ left: w });
366 $handles.slice(2,4).css({ top: h });
369 if (resetKeyPress !== false) {
371 * Need to reset the document keypress event handler -- unbind the
374 if ($.imgAreaSelect.onKeyPress != docKeyPress)
375 $(document).unbind($.imgAreaSelect.keyPress,
376 $.imgAreaSelect.onKeyPress);
380 * Set the document keypress event handler to this instance's
381 * docKeyPress() function
383 $(document)[$.imgAreaSelect.keyPress](
384 $.imgAreaSelect.onKeyPress = docKeyPress);
388 * Internet Explorer displays 1px-wide dashed borders incorrectly by
389 * filling the spaces between dashes with white. Toggling the margin
390 * property between 0 and "auto" fixes this in IE6 and IE7 (IE8 is still
391 * broken). This workaround is not perfect, as it requires setTimeout()
392 * and thus causes the border to flicker a bit, but I haven't found a
395 * Note: This only happens with CSS borders, set with the borderWidth,
396 * borderOpacity, borderColor1, and borderColor2 options (which are now
397 * deprecated). Borders created with GIF background images are fine.
399 if (msie && $border.outerWidth() - $border.innerWidth() == 2) {
400 $border.css('margin', 0);
401 setTimeout(function () { $border.css('margin', 'auto'); }, 0);
406 * Do the complete update sequence: recalculate offsets, update the
407 * elements, and set the correct values of x1, y1, x2, and y2.
409 * @param resetKeyPress
410 * If set to <code>false</code>, this instance's keypress
411 * event handler is not activated
413 function doUpdate(resetKeyPress) {
415 update(resetKeyPress);
416 x1 = viewX(selection.x1); y1 = viewY(selection.y1);
417 x2 = viewX(selection.x2); y2 = viewY(selection.y2);
421 * Hide or fade out an element (or multiple elements)
424 * A jQuery object containing the element(s) to hide/fade out
426 * Callback function to be called when fadeOut() completes
428 function hide($elem, fn) {
429 options.fadeSpeed ? $elem.fadeOut(options.fadeSpeed, fn) : $elem.hide();
433 * Selection area mousemove event handler
438 function areaMouseMove(event) {
439 var x = selX(evX(event)) - selection.x1,
440 y = selY(evY(event)) - selection.y1;
446 $box.one('mouseout', function () { adjusted = false; });
449 /* Clear the resize mode */
452 if (options.resizable) {
454 * Check if the mouse pointer is over the resize margin area and set
455 * the resize mode accordingly
457 if (y <= options.resizeMargin)
459 else if (y >= selection.height - options.resizeMargin)
461 if (x <= options.resizeMargin)
463 else if (x >= selection.width - options.resizeMargin)
467 $box.css('cursor', resize ? resize + '-resize' :
468 options.movable ? 'move' : '');
474 * Document mouseup event handler
479 function docMouseUp(event) {
480 /* Set back the default cursor */
481 $('body').css('cursor', '');
483 * If autoHide is enabled, or if the selection has zero width/height,
484 * hide the selection and the outer area
486 if (options.autoHide || selection.width * selection.height == 0)
487 hide($box.add($outer), function () { $(this).hide(); });
489 $(document).unbind('mousemove', selectingMouseMove);
490 $box.mousemove(areaMouseMove);
492 options.onSelectEnd(img, getSelection());
496 * Selection area mousedown event handler
502 function areaMouseDown(event) {
503 if (event.which != 1) return false;
508 /* Resize mode is in effect */
509 $('body').css('cursor', resize + '-resize');
511 x1 = viewX(selection[/w/.test(resize) ? 'x2' : 'x1']);
512 y1 = viewY(selection[/n/.test(resize) ? 'y2' : 'y1']);
514 $(document).mousemove(selectingMouseMove)
515 .one('mouseup', docMouseUp);
516 $box.unbind('mousemove', areaMouseMove);
518 else if (options.movable) {
519 startX = left + selection.x1 - evX(event);
520 startY = top + selection.y1 - evY(event);
522 $box.unbind('mousemove', areaMouseMove);
524 $(document).mousemove(movingMouseMove)
525 .one('mouseup', function () {
526 options.onSelectEnd(img, getSelection());
528 $(document).unbind('mousemove', movingMouseMove);
529 $box.mousemove(areaMouseMove);
533 $img.mousedown(event);
539 * Adjust the x2/y2 coordinates to maintain aspect ratio (if defined)
542 * If set to <code>true</code>, calculate x2 first. Otherwise,
543 * calculate y2 first.
545 function fixAspectRatio(xFirst) {
548 x2 = max(left, min(left + imgWidth,
549 x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1)));
550 y2 = round(max(top, min(top + imgHeight,
551 y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1))));
555 y2 = max(top, min(top + imgHeight,
556 y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1)));
557 x2 = round(max(left, min(left + imgWidth,
558 x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1))));
564 * Resize the selection area respecting the minimum/maximum dimensions and
567 function doResize() {
569 * Make sure the top left corner of the selection area stays within
570 * image boundaries (it might not if the image source was dynamically
573 x1 = min(x1, left + imgWidth);
574 y1 = min(y1, top + imgHeight);
576 if (abs(x2 - x1) < minWidth) {
577 /* Selection width is smaller than minWidth */
578 x2 = x1 - minWidth * (x2 < x1 || -1);
581 x1 = left + minWidth;
582 else if (x2 > left + imgWidth)
583 x1 = left + imgWidth - minWidth;
586 if (abs(y2 - y1) < minHeight) {
587 /* Selection height is smaller than minHeight */
588 y2 = y1 - minHeight * (y2 < y1 || -1);
591 y1 = top + minHeight;
592 else if (y2 > top + imgHeight)
593 y1 = top + imgHeight - minHeight;
596 x2 = max(left, min(x2, left + imgWidth));
597 y2 = max(top, min(y2, top + imgHeight));
599 fixAspectRatio(abs(x2 - x1) < abs(y2 - y1) * aspectRatio);
601 if (abs(x2 - x1) > maxWidth) {
602 /* Selection width is greater than maxWidth */
603 x2 = x1 - maxWidth * (x2 < x1 || -1);
607 if (abs(y2 - y1) > maxHeight) {
608 /* Selection height is greater than maxHeight */
609 y2 = y1 - maxHeight * (y2 < y1 || -1);
610 fixAspectRatio(true);
613 selection = { x1: selX(min(x1, x2)), x2: selX(max(x1, x2)),
614 y1: selY(min(y1, y2)), y2: selY(max(y1, y2)),
615 width: abs(x2 - x1), height: abs(y2 - y1) };
619 options.onSelectChange(img, getSelection());
623 * Mousemove event handler triggered when the user is selecting an area
629 function selectingMouseMove(event) {
630 x2 = /w|e|^$/.test(resize) || aspectRatio ? evX(event) : viewX(selection.x2);
631 y2 = /n|s|^$/.test(resize) || aspectRatio ? evY(event) : viewY(selection.y2);
639 * Move the selection area
646 function doMove(newX1, newY1) {
647 x2 = (x1 = newX1) + selection.width;
648 y2 = (y1 = newY1) + selection.height;
650 $.extend(selection, { x1: selX(x1), y1: selY(y1), x2: selX(x2),
655 options.onSelectChange(img, getSelection());
659 * Mousemove event handler triggered when the selection area is being moved
665 function movingMouseMove(event) {
666 x1 = max(left, min(startX + evX(event), left + imgWidth - selection.width));
667 y1 = max(top, min(startY + evY(event), top + imgHeight - selection.height));
671 event.preventDefault();
678 function startSelection() {
679 $(document).unbind('mousemove', startSelection);
688 if (!$outer.is(':visible'))
689 /* Show the plugin elements */
690 $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
694 $(document).unbind('mouseup', cancelSelection)
695 .mousemove(selectingMouseMove).one('mouseup', docMouseUp);
696 $box.unbind('mousemove', areaMouseMove);
698 options.onSelectStart(img, getSelection());
704 function cancelSelection() {
705 $(document).unbind('mousemove', startSelection)
706 .unbind('mouseup', cancelSelection);
707 hide($box.add($outer));
709 setSelection(selX(x1), selY(y1), selX(x1), selY(y1));
711 /* If this is an API call, callback functions should not be triggered */
712 if (!(this instanceof $.imgAreaSelect)) {
713 options.onSelectChange(img, getSelection());
714 options.onSelectEnd(img, getSelection());
719 * Image mousedown event handler
725 function imgMouseDown(event) {
726 /* Ignore the event if animation is in progress */
727 if (event.which != 1 || $outer.is(':animated')) return false;
730 startX = x1 = evX(event);
731 startY = y1 = evY(event);
733 /* Selection will start when the mouse is moved */
734 $(document).mousemove(startSelection).mouseup(cancelSelection);
740 * Window resize event handler
742 function windowResize() {
747 * Image load event handler. This is the final part of the initialization
754 setOptions(options = $.extend({
755 classPrefix: 'imgareaselect',
760 onInit: function () {},
761 onSelectStart: function () {},
762 onSelectChange: function () {},
763 onSelectEnd: function () {}
766 $box.add($outer).css({ visibility: '' });
772 $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
776 * Call the onInit callback. The setTimeout() call is used to ensure
777 * that the plugin has been fully initialized and the object instance is
778 * available (so that it can be obtained in the callback).
780 setTimeout(function () { options.onInit(img, getSelection()); }, 0);
784 * Document keypress event handler
790 var docKeyPress = function(event) {
791 var k = options.keys, d, t, key = event.keyCode;
793 d = !isNaN(k.alt) && (event.altKey || event.originalEvent.altKey) ? k.alt :
794 !isNaN(k.ctrl) && event.ctrlKey ? k.ctrl :
795 !isNaN(k.shift) && event.shiftKey ? k.shift :
796 !isNaN(k.arrows) ? k.arrows : 10;
798 if (k.arrows == 'resize' || (k.shift == 'resize' && event.shiftKey) ||
799 (k.ctrl == 'resize' && event.ctrlKey) ||
800 (k.alt == 'resize' && (event.altKey || event.originalEvent.altKey)))
802 /* Resize selection */
823 fixAspectRatio(true);
840 doMove(max(x1 - d, left), y1);
844 doMove(x1, max(y1 - d, top));
848 doMove(x1 + min(d, imgWidth - selX(x2)), y1);
852 doMove(x1, y1 + min(d, imgHeight - selY(y2)));
863 * Apply style options to plugin element (or multiple elements)
866 * A jQuery object representing the element(s) to style
868 * An object that maps option names to corresponding CSS
871 function styleOptions($elem, props) {
872 for (var option in props)
873 if (options[option] !== undefined)
874 $elem.css(props[option], options[option]);
881 * The new options object
883 function setOptions(newOptions) {
884 if (newOptions.parent)
885 ($parent = $(newOptions.parent)).append($box.add($outer));
887 /* Merge the new options with the existing ones */
888 $.extend(options, newOptions);
892 if (newOptions.handles != null) {
893 /* Recreate selection area handles */
897 i = newOptions.handles ? newOptions.handles == 'corners' ? 4 : 8 : 0;
900 $handles = $handles.add(div());
902 /* Add a class to handles and set the CSS properties */
903 $handles.addClass(options.classPrefix + '-handle').css({
904 position: 'absolute',
906 * The font-size property needs to be set to zero, otherwise
907 * Internet Explorer makes the handles too large
910 zIndex: zIndex + 1 || 1
914 * If handle width/height has not been set with CSS rules, set the
917 if (!parseInt($handles.css('width')) >= 0)
918 $handles.width(5).height(5);
921 * If the borderWidth option is in use, add a solid border to
924 if (o = options.borderWidth)
925 $handles.css({ borderWidth: o, borderStyle: 'solid' });
927 /* Apply other style options */
928 styleOptions($handles, { borderColor1: 'border-color',
929 borderColor2: 'background-color',
930 borderOpacity: 'opacity' });
933 /* Calculate scale factors */
934 scaleX = options.imageWidth / imgWidth || 1;
935 scaleY = options.imageHeight / imgHeight || 1;
938 if (newOptions.x1 != null) {
939 setSelection(newOptions.x1, newOptions.y1, newOptions.x2,
941 newOptions.show = !newOptions.hide;
945 /* Enable keyboard support */
946 options.keys = $.extend({ shift: 1, ctrl: 'resize' },
949 /* Add classes to plugin elements */
950 $outer.addClass(options.classPrefix + '-outer');
951 $area.addClass(options.classPrefix + '-selection');
952 for (i = 0; i++ < 4;)
953 $($border[i-1]).addClass(options.classPrefix + '-border' + i);
955 /* Apply style options */
956 styleOptions($area, { selectionColor: 'background-color',
957 selectionOpacity: 'opacity' });
958 styleOptions($border, { borderOpacity: 'opacity',
959 borderWidth: 'border-width' });
960 styleOptions($outer, { outerColor: 'background-color',
961 outerOpacity: 'opacity' });
962 if (o = options.borderColor1)
963 $($border[0]).css({ borderStyle: 'solid', borderColor: o });
964 if (o = options.borderColor2)
965 $($border[1]).css({ borderStyle: 'dashed', borderColor: o });
967 /* Append all the selection area elements to the container box */
968 $box.append($area.add($border).add($areaOpera)).append($handles);
971 if (o = ($outer.css('filter')||'').match(/opacity=(\d+)/))
972 $outer.css('opacity', o[1]/100);
973 if (o = ($border.css('filter')||'').match(/opacity=(\d+)/))
974 $border.css('opacity', o[1]/100);
978 hide($box.add($outer));
979 else if (newOptions.show && imgLoaded) {
981 $box.add($outer).fadeIn(options.fadeSpeed||0);
985 /* Calculate the aspect ratio factor */
986 aspectRatio = (d = (options.aspectRatio || '').split(/:/))[0] / d[1];
988 $img.add($outer).unbind('mousedown', imgMouseDown);
990 if (options.disable || options.enable === false) {
991 /* Disable the plugin */
992 $box.unbind('mousemove', areaMouseMove).unbind('mousedown', areaMouseDown);
993 $(window).unbind('resize', windowResize);
996 if (options.enable || options.disable === false) {
997 /* Enable the plugin */
998 if (options.resizable || options.movable)
999 $box.mousemove(areaMouseMove).mousedown(areaMouseDown);
1001 $(window).resize(windowResize);
1004 if (!options.persistent)
1005 $img.add($outer).mousedown(imgMouseDown);
1008 options.enable = options.disable = undefined;
1012 * Remove plugin completely
1014 this.remove = function () {
1016 * Call setOptions with { disable: true } to unbind the event handlers
1018 setOptions({ disable: true });
1019 $box.add($outer).remove();
1027 * Get current options
1029 * @return An object containing the set of options currently in use
1031 this.getOptions = function () { return options; };
1034 * Set plugin options
1037 * The new options object
1039 this.setOptions = setOptions;
1042 * Get the current selection
1045 * If set to <code>true</code>, scaling is not applied to the
1046 * returned selection
1047 * @return Selection object
1049 this.getSelection = getSelection;
1052 * Set the current selection
1055 * X coordinate of the upper left corner of the selection area
1057 * Y coordinate of the upper left corner of the selection area
1059 * X coordinate of the lower right corner of the selection area
1061 * Y coordinate of the lower right corner of the selection area
1063 * If set to <code>true</code>, scaling is not applied to the
1066 this.setSelection = setSelection;
1071 this.cancelSelection = cancelSelection;
1074 * Update plugin elements
1076 * @param resetKeyPress
1077 * If set to <code>false</code>, this instance's keypress
1078 * event handler is not activated
1080 this.update = doUpdate;
1082 /* Do the dreaded browser detection */
1083 var msie = (/msie ([\w.]+)/i.exec(ua)||[])[1],
1084 opera = /opera/i.test(ua),
1085 safari = /webkit/i.test(ua) && !/chrome/i.test(ua);
1088 * Traverse the image's parent elements (up to <body>) and find the
1094 zIndex = max(zIndex,
1095 !isNaN($p.css('z-index')) ? $p.css('z-index') : zIndex);
1096 /* Also check if any of the ancestor elements has fixed position */
1097 if ($p.css('position') == 'fixed')
1100 $p = $p.parent(':not(body)');
1104 * If z-index is given as an option, it overrides the one found by the
1107 zIndex = options.zIndex || zIndex;
1110 $img.attr('unselectable', 'on');
1113 * In MSIE and WebKit, we need to use the keydown event instead of keypress
1115 $.imgAreaSelect.keyPress = msie || safari ? 'keydown' : 'keypress';
1118 * There is a bug affecting the CSS cursor property in Opera (observed in
1119 * versions up to 10.00) that prevents the cursor from being updated unless
1120 * the mouse leaves and enters the element again. To trigger the mouseover
1121 * event, we're adding an additional div to $box and we're going to toggle
1122 * it when mouse moves inside the selection area.
1125 $areaOpera = div().css({ width: '100%', height: '100%',
1126 position: 'absolute', zIndex: zIndex + 2 || 2 });
1129 * We initially set visibility to "hidden" as a workaround for a weird
1130 * behaviour observed in Google Chrome 1.0.154.53 (on Windows XP). Normally
1131 * we would just set display to "none", but, for some reason, if we do so
1132 * then Chrome refuses to later display the element with .show() or
1135 $box.add($outer).css({ visibility: 'hidden', position: position,
1136 overflow: 'hidden', zIndex: zIndex || '0' });
1137 $box.css({ zIndex: zIndex + 2 || 2 });
1138 $area.add($border).css({ position: 'absolute', fontSize: 0 });
1141 * If the image has been fully loaded, or if it is not really an image (eg.
1142 * a div), call imgLoad() immediately; otherwise, bind it to be called once
1143 * on image load event.
1145 img.complete || img.readyState == 'complete' || !$img.is('img') ?
1146 imgLoad() : $img.one('load', imgLoad);
1149 * MSIE 9.0 doesn't always fire the image load event -- resetting the src
1150 * attribute seems to trigger it. The check is for version 7 and above to
1151 * accommodate for MSIE 9 running in compatibility mode.
1153 if (!imgLoaded && msie && msie >= 7)
1158 * Invoke imgAreaSelect on a jQuery object containing the image(s)
1162 * @return The jQuery object or a reference to imgAreaSelect instance (if the
1163 * <code>instance</code> option was specified)
1165 $.fn.imgAreaSelect = function (options) {
1166 options = options || {};
1168 this.each(function () {
1169 /* Is there already an imgAreaSelect instance bound to this element? */
1170 if ($(this).data('imgAreaSelect')) {
1171 /* Yes there is -- is it supposed to be removed? */
1172 if (options.remove) {
1173 /* Remove the plugin */
1174 $(this).data('imgAreaSelect').remove();
1175 $(this).removeData('imgAreaSelect');
1179 $(this).data('imgAreaSelect').setOptions(options);
1181 else if (!options.remove) {
1182 /* No exising instance -- create a new one */
1185 * If neither the "enable" nor the "disable" option is present, add
1186 * "enable" as the default
1188 if (options.enable === undefined && options.disable === undefined)
1189 options.enable = true;
1191 $(this).data('imgAreaSelect', new $.imgAreaSelect(this, options));
1195 if (options.instance)
1197 * Return the imgAreaSelect instance bound to the first element in the
1200 return $(this).data('imgAreaSelect');