42 $.imgAreaSelect =
function (
img, options) {
103 minWidth, minHeight, maxWidth, maxHeight,
115 selection = { x1: 0, y1: 0, x2: 0, y2: 0,
width: 0,
height: 0 },
121 ua = navigator.userAgent,
124 $p, d,
i, o, w, h, adjusted;
139 return x + imgOfs.left - parOfs.left;
150 return y + imgOfs.top - parOfs.top;
165 return x - imgOfs.left + parOfs.left;
176 return y - imgOfs.top + parOfs.top;
191 function evX(event) {
192 return event.pageX - parOfs.left;
202 function evY(event) {
203 return event.pageY - parOfs.top;
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) };
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;
263 if (!imgLoaded || !$img.width())
270 imgOfs = {
left: round($img.offset().left),
top: round($img.offset().top) };
273 imgWidth = $img.innerWidth();
274 imgHeight = $img.innerHeight();
276 imgOfs.top += ($img.outerHeight() - imgHeight) >> 1;
277 imgOfs.left += ($img.outerWidth() - imgWidth) >> 1;
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));
290 !docElem[
'getBoundingClientRect'])
292 imgOfs.top += max(
document.body.scrollTop, docElem.scrollTop);
293 imgOfs.left += max(
document.body.scrollLeft, docElem.scrollLeft);
297 parOfs = /absolute|relative/.test($parent.css(
'position')) ?
298 {
left: round($parent.offset().left) - $parent.scrollLeft(),
299 top: round($parent.offset().top) - $parent.scrollTop() } :
311 if (selection.x2 > imgWidth || selection.y2 > imgHeight)
322 function update(resetKeyPress) {
330 $box.css({
left: viewX(selection.x1),
top: viewY(selection.y1) })
331 .add($area).width(w = selection.width).height(h = selection.height);
337 $area.add($border).add($handles).css({
left: 0,
top: 0 });
341 .width(max(w - $border.outerWidth() + $border.innerWidth(), 0))
342 .height(max(h - $border.outerHeight() + $border.innerHeight(), 0));
350 width: imgWidth - selection.x2,
height: imgHeight });
351 $($outer[3]).css({
left:
left + selection.x1,
top:
top + selection.y2,
354 w -= $handles.outerWidth();
355 h -= $handles.outerHeight();
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) {
376 $.imgAreaSelect.onKeyPress);
384 $.imgAreaSelect.onKeyPress = docKeyPress);
399 if (msie && $border.outerWidth() - $border.innerWidth() == 2) {
400 $border.css(
'margin', 0);
401 setTimeout(
function () { $border.css(
'margin',
'auto'); }, 0);
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);
428 function hide($elem, fn) {
429 options.fadeSpeed ? $elem.fadeOut(options.fadeSpeed, fn) : $elem.hide();
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; });
452 if (options.resizable) {
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' :
'');
479 function docMouseUp(event) {
481 $(
'body').css(
'cursor',
'');
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());
502 function areaMouseDown(event) {
503 if (event.which != 1)
return false;
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);
545 function fixAspectRatio(xFirst) {
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))));
567 function doResize() {
573 x1 = min(x1,
left + imgWidth);
574 y1 = min(y1,
top + imgHeight);
576 if (abs(x2 - x1) < 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) {
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) {
603 x2 = x1 - maxWidth * (x2 < x1 || -1);
607 if (abs(y2 - y1) > 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)),
619 options.onSelectChange(
img, getSelection());
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);
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());
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'))
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));
713 options.onSelectChange(
img, getSelection());
714 options.onSelectEnd(
img, getSelection());
725 function imgMouseDown(event) {
727 if (event.which != 1 || $outer.is(
':animated'))
return false;
730 startX = x1 = evX(event);
731 startY = y1 = evY(event);
734 $(
document).mousemove(startSelection).mouseup(cancelSelection);
742 function windowResize() {
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);
780 setTimeout(
function () { options.onInit(
img, getSelection()); }, 0);
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)))
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)));
871 function styleOptions($elem, props) {
872 for (var option in props)
873 if (options[option] !== undefined)
874 $elem.css(props[option], options[option]);
883 function setOptions(newOptions) {
884 if (newOptions.parent)
885 ($parent = $(newOptions.parent)).append($box.add($outer));
888 $.extend(options, newOptions);
892 if (newOptions.handles != null) {
897 i = newOptions.handles ? newOptions.handles ==
'corners' ? 4 : 8 : 0;
900 $handles = $handles.add(
div());
903 $handles.addClass(options.classPrefix +
'-handle').css({
910 zIndex: zIndex + 1 || 1
917 if (!parseInt($handles.css(
'width')) >= 0)
918 $handles.width(5).height(5);
924 if (o = options.borderWidth)
925 $handles.css({ borderWidth: o, borderStyle:
'solid' });
928 styleOptions($handles, { borderColor1:
'border-color',
929 borderColor2:
'background-color',
930 borderOpacity:
'opacity' });
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;
946 options.keys = $.extend({ shift: 1, ctrl:
'resize' },
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);
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 });
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);
986 aspectRatio = (d = (options.aspectRatio ||
'').split(/:/))[0] / d[1];
988 $img.add($outer).unbind(
'mousedown', imgMouseDown);
990 if (options.disable || options.enable ===
false) {
992 $box.unbind(
'mousemove', areaMouseMove).unbind(
'mousedown', areaMouseDown);
993 $(window).unbind(
'resize', windowResize);
996 if (options.enable || options.disable ===
false) {
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;
1014 this.
remove =
function () {
1018 setOptions({ disable:
true });
1019 $box.add($outer).remove();
1031 this.getOptions =
function () {
return options; };
1039 this.setOptions = setOptions;
1049 this.getSelection = getSelection;
1066 this.setSelection = setSelection;
1071 this.cancelSelection = cancelSelection;
1080 this.update = doUpdate;
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);
1094 zIndex = max(zIndex,
1095 !isNaN($p.css(
'z-index')) ? $p.css(
'z-index') : zIndex);
1097 if ($p.css(
'position') ==
'fixed')
1100 $p = $p.parent(
':not(body)');
1107 zIndex = options.zIndex || zIndex;
1110 $img.attr(
'unselectable',
'on');
1115 $.imgAreaSelect.keyPress = msie || safari ?
'keydown' :
'keypress';
1126 position:
'absolute', zIndex: zIndex + 2 || 2 });
1136 overflow:
'hidden', zIndex: zIndex ||
'0' });
1137 $box.css({ zIndex: zIndex + 2 || 2 });
1138 $area.add($border).css({
position:
'absolute', fontSize: 0 });
1145 img.complete ||
img.readyState ==
'complete' || !$img.is(
'img') ?
1146 imgLoad() : $img.one(
'load', imgLoad);
1153 if (!imgLoaded && msie && msie >= 7)
1165 $.fn.imgAreaSelect =
function (options) {
1166 options = options || {};
1168 this.each(
function () {
1170 if ($(
this).data(
'imgAreaSelect')) {
1172 if (options.remove) {
1174 $(this).data(
'imgAreaSelect').remove();
1175 $(this).removeData(
'imgAreaSelect');
1179 $(
this).data(
'imgAreaSelect').setOptions(options);
1181 else if (!options.remove) {
1188 if (options.enable === undefined && options.disable === undefined)
1189 options.enable =
true;
1191 $(
this).data(
'imgAreaSelect',
new $.
imgAreaSelect(
this, options));
1195 if (options.instance)
1200 return $(
this).data(
'imgAreaSelect');
elgg extend
Merges two or more objects together and returns the result.
imgAreaSelect
imgAreaSelect initialization
function div()
Create a new HTML div element.
GNU GENERAL PUBLIC LICENSE June Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license document
if(!empty($class)) if(!empty($vars['width'])) if(!empty($vars['height'])) $img