2 * imgAreaSelect jQuery plugin
5 * Copyright (c) 2008-2009 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/
25 $.imgAreaSelect = function (img, options) {
34 $border = div().add(div()).add(div()).add(div()),
35 $outer = div().add(div()).add(div()).add(div()),
52 position = 'absolute',
68 selection = { x1: 0, y1: 0, x2: 0, y2: 0, width: 0, height: 0 },
70 $p, d, i, o, w, h, adjusted;
73 return x + imgOfs.left - parOfs.left;
77 return y + imgOfs.top - parOfs.top;
81 return x - imgOfs.left + parOfs.left;
85 return y - imgOfs.top + parOfs.top;
89 return event.pageX - parOfs.left;
93 return event.pageY - parOfs.top;
96 function getSelection(noScale) {
97 var sx = noScale || scaleX, sy = noScale || scaleY;
99 return { x1: round(selection.x1 * sx),
100 y1: round(selection.y1 * sy),
101 x2: round(selection.x2 * sx),
102 y2: round(selection.y2 * sy),
103 width: round(selection.x2 * sx) - round(selection.x1 * sx),
104 height: round(selection.y2 * sy) - round(selection.y1 * sy) };
107 function setSelection(x1, y1, x2, y2, noScale) {
108 var sx = noScale || scaleX, sy = noScale || scaleY;
117 selection.width = (x2 = viewX(selection.x2)) - (x1 = viewX(selection.x1));
118 selection.height = (y2 = viewX(selection.y2)) - (y1 = viewX(selection.y1));
125 imgOfs = { left: round($img.offset().left), top: round($img.offset().top) };
127 imgWidth = $img.width();
128 imgHeight = $img.height();
130 if ($().jquery == '1.3.2' && $.browser.safari && position == 'fixed') {
131 imgOfs.top += max(document.documentElement.scrollTop, $('body').scrollTop());
133 imgOfs.left += max(document.documentElement.scrollLeft, $('body').scrollLeft());
136 parOfs = $.inArray($parent.css('position'), ['absolute', 'relative']) + 1 ?
137 { left: round($parent.offset().left) - $parent.scrollLeft(),
138 top: round($parent.offset().top) - $parent.scrollTop() } :
139 position == 'fixed' ?
140 { left: $(document).scrollLeft(), top: $(document).scrollTop() } :
147 function update(resetKeyPress) {
150 $box.css({ left: viewX(selection.x1), top: viewY(selection.y1) })
151 .add($area).width(w = selection.width).height(h = selection.height);
153 $area.add($border).add($handles).css({ left: 0, top: 0 });
156 .width(max(w - $border.outerWidth() + $border.innerWidth(), 0))
157 .height(max(h - $border.outerHeight() + $border.innerHeight(), 0));
159 $($outer[0]).css({ left: left, top: top,
160 width: selection.x1, height: imgHeight });
161 $($outer[1]).css({ left: left + selection.x1, top: top,
162 width: w, height: selection.y1 });
163 $($outer[2]).css({ left: left + selection.x2, top: top,
164 width: imgWidth - selection.x2, height: imgHeight });
165 $($outer[3]).css({ left: left + selection.x1, top: top + selection.y2,
166 width: w, height: imgHeight - selection.y2 });
168 w -= $handles.outerWidth();
169 h -= $handles.outerHeight();
171 switch ($handles.length) {
173 $($handles[4]).css({ left: w / 2 });
174 $($handles[5]).css({ left: w, top: h / 2 });
175 $($handles[6]).css({ left: w / 2, top: h });
176 $($handles[7]).css({ top: h / 2 });
178 $handles.slice(1,3).css({ left: w });
179 $handles.slice(2,4).css({ top: h });
182 if (resetKeyPress !== false) {
183 if ($.imgAreaSelect.keyPress != docKeyPress)
184 $(document).unbind($.imgAreaSelect.keyPress,
185 $.imgAreaSelect.onKeyPress);
188 $(document)[$.imgAreaSelect.keyPress](
189 $.imgAreaSelect.onKeyPress = docKeyPress);
192 if ($.browser.msie && $border.outerWidth() - $border.innerWidth() == 2) {
193 $border.css('margin', 0);
194 setTimeout(function () { $border.css('margin', 'auto'); }, 0);
198 function doUpdate(resetKeyPress) {
200 update(resetKeyPress);
201 x1 = viewX(selection.x1); y1 = viewY(selection.y1);
202 x2 = viewX(selection.x2); y2 = viewY(selection.y2);
205 function hide($elem, fn) {
206 options.fadeSpeed ? $elem.fadeOut(options.fadeSpeed, fn) : $elem.hide();
210 function areaMouseMove(event) {
211 var x = selX(evX(event)) - selection.x1,
212 y = selY(evY(event)) - selection.y1;
218 $box.one('mouseout', function () { adjusted = false; });
223 if (options.resizable) {
224 if (y <= resizeMargin)
226 else if (y >= selection.height - resizeMargin)
228 if (x <= resizeMargin)
230 else if (x >= selection.width - resizeMargin)
234 $box.css('cursor', resize ? resize + '-resize' :
235 options.movable ? 'move' : '');
240 function docMouseUp(event) {
241 $('body').css('cursor', '');
243 if (options.autoHide || selection.width * selection.height == 0)
244 hide($box.add($outer), function () { $(this).hide(); });
246 options.onSelectEnd(img, getSelection());
248 $(document).unbind('mousemove', selectingMouseMove);
249 $box.mousemove(areaMouseMove);
252 function areaMouseDown(event) {
253 if (event.which != 1) return false;
258 $('body').css('cursor', resize + '-resize');
260 x1 = viewX(selection[/w/.test(resize) ? 'x2' : 'x1']);
261 y1 = viewY(selection[/n/.test(resize) ? 'y2' : 'y1']);
263 $(document).mousemove(selectingMouseMove)
264 .one('mouseup', docMouseUp);
265 $box.unbind('mousemove', areaMouseMove);
267 else if (options.movable) {
268 startX = left + selection.x1 - evX(event);
269 startY = top + selection.y1 - evY(event);
271 $box.unbind('mousemove', areaMouseMove);
273 $(document).mousemove(movingMouseMove)
274 .one('mouseup', function () {
275 options.onSelectEnd(img, getSelection());
277 $(document).unbind('mousemove', movingMouseMove);
278 $box.mousemove(areaMouseMove);
282 $img.mousedown(event);
287 function aspectRatioXY() {
288 x2 = max(left, min(left + imgWidth,
289 x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1)));
291 y2 = round(max(top, min(top + imgHeight,
292 y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1))));
296 function aspectRatioYX() {
297 y2 = max(top, min(top + imgHeight,
298 y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1)));
299 x2 = round(max(left, min(left + imgWidth,
300 x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1))));
304 function doResize() {
305 if (abs(x2 - x1) < options.minWidth) {
306 x2 = x1 - options.minWidth * (x2 < x1 || -1);
309 x1 = left + options.minWidth;
310 else if (x2 > left + imgWidth)
311 x1 = left + imgWidth - options.minWidth;
314 if (abs(y2 - y1) < options.minHeight) {
315 y2 = y1 - options.minHeight * (y2 < y1 || -1);
318 y1 = top + options.minHeight;
319 else if (y2 > top + imgHeight)
320 y1 = top + imgHeight - options.minHeight;
323 x2 = max(left, min(x2, left + imgWidth));
324 y2 = max(top, min(y2, top + imgHeight));
327 if (abs(x2 - x1) / aspectRatio > abs(y2 - y1))
332 if (abs(x2 - x1) > options.maxWidth) {
333 x2 = x1 - options.maxWidth * (x2 < x1 || -1);
334 if (aspectRatio) aspectRatioYX();
337 if (abs(y2 - y1) > options.maxHeight) {
338 y2 = y1 - options.maxHeight * (y2 < y1 || -1);
339 if (aspectRatio) aspectRatioXY();
342 selection = { x1: selX(min(x1, x2)), x2: selX(max(x1, x2)),
343 y1: selY(min(y1, y2)), y2: selY(max(y1, y2)),
344 width: abs(x2 - x1), height: abs(y2 - y1) };
348 options.onSelectChange(img, getSelection());
351 function selectingMouseMove(event) {
352 x2 = resize == '' || /w|e/.test(resize) || aspectRatio ? evX(event) : viewX(selection.x2);
353 y2 = resize == '' || /n|s/.test(resize) || aspectRatio ? evY(event) : viewY(selection.y2);
361 function doMove(newX1, newY1) {
362 x2 = (x1 = newX1) + selection.width;
363 y2 = (y1 = newY1) + selection.height;
365 selection = $.extend(selection, { x1: selX(x1), y1: selY(y1),
366 x2: selX(x2), y2: selY(y2) });
370 options.onSelectChange(img, getSelection());
373 function movingMouseMove(event) {
374 x1 = max(left, min(startX + evX(event), left + imgWidth - selection.width));
375 y1 = max(top, min(startY + evY(event), top + imgHeight - selection.height));
379 event.preventDefault();
384 function startSelection() {
394 if ($outer.is(':not(:visible)'))
395 $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
399 $(document).unbind('mouseup', cancelSelection)
400 .mousemove(selectingMouseMove).one('mouseup', docMouseUp);
401 $box.unbind('mousemove', areaMouseMove);
403 options.onSelectStart(img, getSelection());
406 function cancelSelection() {
407 $(document).unbind('mousemove', startSelection);
408 hide($box.add($outer));
410 selection = { x1: selX(x1), y1: selY(y1), x2: selX(x1), y2: selY(y1),
411 width: 0, height: 0 };
413 options.onSelectChange(img, getSelection());
414 options.onSelectEnd(img, getSelection());
417 function imgMouseDown(event) {
418 if (event.which != 1 || $outer.is(':animated')) return false;
421 startX = x1 = evX(event);
422 startY = y1 = evY(event);
424 $(document).one('mousemove', startSelection)
425 .one('mouseup', cancelSelection);
430 function parentScroll() {
437 setOptions(options = $.extend({
438 classPrefix: 'imgareaselect',
442 onInit: function () {},
443 onSelectStart: function () {},
444 onSelectChange: function () {},
445 onSelectEnd: function () {}
448 $box.add($outer).css({ visibility: '' });
454 $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
457 setTimeout(function () { options.onInit(img, getSelection()); }, 0);
460 var docKeyPress = function(event) {
461 var k = options.keys, d, t, key = event.keyCode || event.which;
463 d = !isNaN(k.alt) && (event.altKey || event.originalEvent.altKey) ? k.alt :
464 !isNaN(k.ctrl) && event.ctrlKey ? k.ctrl :
465 !isNaN(k.shift) && event.shiftKey ? k.shift :
466 !isNaN(k.arrows) ? k.arrows : 10;
468 if (k.arrows == 'resize' || (k.shift == 'resize' && event.shiftKey) ||
469 (k.ctrl == 'resize' && event.ctrlKey) ||
470 (k.alt == 'resize' && (event.altKey || event.originalEvent.altKey)))
479 if (aspectRatio) aspectRatioYX();
487 if (aspectRatio) aspectRatioXY();
501 doMove(max(x1 - d, left), y1);
504 doMove(x1, max(y1 - d, top));
507 doMove(x1 + min(d, imgWidth - selX(x2)), y1);
510 doMove(x1, y1 + min(d, imgHeight - selY(y2)));
520 function styleOptions($elem, props) {
521 for (option in props)
522 if (options[option] !== undefined)
523 $elem.css(props[option], options[option]);
526 function setOptions(newOptions) {
527 if (newOptions.parent)
528 ($parent = $(newOptions.parent)).append($box.add($outer));
530 options = $.extend(options, newOptions);
534 if (newOptions.handles != null) {
538 i = newOptions.handles ? newOptions.handles == 'corners' ? 4 : 8 : 0;
541 $handles = $handles.add(div());
543 $handles.addClass(options.classPrefix + '-handle').css({
544 position: 'absolute',
546 zIndex: zIndex + 1 || 1
549 if (!parseInt($handles.css('width')))
550 $handles.width(5).height(5);
552 if (o = options.borderWidth)
553 $handles.css({ borderWidth: o, borderStyle: 'solid' });
555 styleOptions($handles, { borderColor1: 'border-color',
556 borderColor2: 'background-color',
557 borderOpacity: 'opacity' });
560 scaleX = options.imageWidth / imgWidth || 1;
561 scaleY = options.imageHeight / imgHeight || 1;
563 if (newOptions.x1 != null) {
564 setSelection(newOptions.x1, newOptions.y1, newOptions.x2,
566 newOptions.show = !newOptions.hide;
570 options.keys = $.extend({ shift: 1, ctrl: 'resize' },
573 $outer.addClass(options.classPrefix + '-outer');
574 $area.addClass(options.classPrefix + '-selection');
575 for (i = 0; i++ < 4;)
576 $($border[i-1]).addClass(options.classPrefix + '-border' + i);
578 styleOptions($area, { selectionColor: 'background-color',
579 selectionOpacity: 'opacity' });
580 styleOptions($border, { borderOpacity: 'opacity',
581 borderWidth: 'border-width' });
582 styleOptions($outer, { outerColor: 'background-color',
583 outerOpacity: 'opacity' });
584 if (o = options.borderColor1)
585 $($border[0]).css({ borderStyle: 'solid', borderColor: o });
586 if (o = options.borderColor2)
587 $($border[1]).css({ borderStyle: 'dashed', borderColor: o });
589 $box.append($area.add($border).add($handles).add($areaOpera));
591 if ($.browser.msie) {
592 if (o = $outer.css('filter').match(/opacity=([0-9]+)/))
593 $outer.css('opacity', o[1]/100);
594 if (o = $border.css('filter').match(/opacity=([0-9]+)/))
595 $border.css('opacity', o[1]/100);
599 hide($box.add($outer));
600 else if (newOptions.show && imgLoaded) {
602 $box.add($outer).fadeIn(options.fadeSpeed||0);
606 aspectRatio = (d = (options.aspectRatio || '').split(/:/))[0] / d[1];
608 if (options.disable || options.enable === false) {
609 $box.unbind('mousemove', areaMouseMove).unbind('mousedown', areaMouseDown);
610 $img.add($outer).unbind('mousedown', imgMouseDown);
611 $(window).unbind('resize', parentScroll);
612 $img.add($img.parents()).unbind('scroll', parentScroll);
614 else if (options.enable || options.disable === false) {
615 if (options.resizable || options.movable)
616 $box.mousemove(areaMouseMove).mousedown(areaMouseDown);
618 if (!options.persistent)
619 $img.add($outer).mousedown(imgMouseDown);
620 $(window).resize(parentScroll);
621 $img.add($img.parents()).scroll(parentScroll);
624 options.enable = options.disable = undefined;
627 this.getOptions = function () { return options; };
629 this.setOptions = setOptions;
631 this.getSelection = getSelection;
633 this.setSelection = setSelection;
635 this.update = doUpdate;
639 while ($p.length && !$p.is('body')) {
640 if (!isNaN($p.css('z-index')) && $p.css('z-index') > zIndex)
641 zIndex = $p.css('z-index');
642 if ($p.css('position') == 'fixed')
648 if (!isNaN(options.zIndex))
649 zIndex = options.zIndex;
652 $img.attr('unselectable', 'on');
654 $.imgAreaSelect.keyPress = $.browser.msie ||
655 $.browser.safari ? 'keydown' : 'keypress';
658 $areaOpera = div().css({ width: '100%', height: '100%',
659 position: 'absolute', zIndex: zIndex + 2 || 2 });
661 $box.add($outer).css({ visibility: 'hidden', position: position,
662 overflow: 'hidden', zIndex: zIndex || '0' });
663 $box.css({ zIndex: zIndex + 2 || 2 });
664 $area.add($border).css({ position: 'absolute' });
666 img.complete || img.readyState == 'complete' || !$img.is('img') ?
667 imgLoad() : $img.one('load', imgLoad);
671 $.fn.imgAreaSelect = function (options) {
672 options = options || {};
674 this.each(function () {
675 if ($(this).data('imgAreaSelect'))
676 $(this).data('imgAreaSelect').setOptions(options);
678 if (options.enable === undefined && options.disable === undefined)
679 options.enable = true;
681 $(this).data('imgAreaSelect', new $.imgAreaSelect(this, options));
685 if (options.instance)
686 return $(this).data('imgAreaSelect');