(function (factory) { if (typeof define === "function" && define.amd) { // amd. register as anonymous module. define(["jquery"], factory); } else if (typeof exports === "object") { // node / commonjs factory(require("jquery")); } else { // browser globals. factory(jquery); } })(function ($) { "use strict"; var $window = $(window), $document = $(document), location = window.location, // constants true = true, false = false, null = null, nan = nan, infinity = infinity, string_undefined = "undefined", string_directive = "directive", cropper_namespace = ".cropper", // regexps regexp_directives = /^(e|n|w|s|ne|nw|sw|se|all|crop|move|zoom)$/, regexp_options = /^(x|y|width|height)$/, regexp_properties = /^(naturalwidth|naturalheight|width|height|aspectratio|ratio|rotate)$/, // classes class_modal = "cropper-modal", class_hidden = "cropper-hidden", class_invisible = "cropper-invisible", class_move = "cropper-move", class_crop = "cropper-crop", class_disabled = "cropper-disabled", // events event_mouse_down = "mousedown touchstart", event_mouse_move = "mousemove touchmove", event_mouse_up = "mouseup mouseleave touchend touchleave touchcancel", event_wheel = "wheel mousewheel dommousescroll", event_resize = "resize" + cropper_namespace, // bind to window with namespace event_dblclick = "dblclick", event_build = "build" + cropper_namespace, event_built = "built" + cropper_namespace, event_drag_start = "dragstart" + cropper_namespace, event_drag_move = "dragmove" + cropper_namespace, event_drag_end = "dragend" + cropper_namespace, // functions isnumber = function (n) { return typeof n === "number"; }, // constructor cropper = function (element, options) { this.element = element; this.$element = $(element); this.defaults = $.extend({}, cropper.defaults, $.isplainobject(options) ? options : {}); this.$original = null; this.ready = false; this.built = false; this.cropped = false; this.rotated = false; this.disabled = false; this.replaced = false; this.init(); }, // others round = math.round, sqrt = math.sqrt, min = math.min, max = math.max, abs = math.abs, sin = math.sin, cos = math.cos, num = parsefloat; cropper.prototype = { constructor: cropper, support: { canvas: $.isfunction($("")[0].getcontext) }, init: function () { var defaults = this.defaults; $.each(defaults, function (i, n) { switch (i) { case "aspectratio": defaults[i] = abs(num(n)) || nan; // 0 -> nan break; case "autocroparea": defaults[i] = abs(num(n)) || 0.8; // 0 | nan -> 0.8 break; case "minwidth": case "minheight": defaults[i] = abs(num(n)) || 0; // nan -> 0 break; case "maxwidth": case "maxheight": defaults[i] = abs(num(n)) || infinity; // 0 | nan -> infinity break; // no default } }); // set default image data this.image = { rotate: 0 }; this.load(); }, load: function () { var _this = this, $this = this.$element, element = this.element, image = this.image, crossorigin = "", $clone, url; if ($this.is("img")) { url = $this.prop("src"); } else if ($this.is("canvas") && this.support.canvas) { url = element.todataurl(); } if (!url) { return; } // reset image rotate degree if (this.replaced) { image.rotate = 0; } if (this.defaults.checkimageorigin) { if ($this.prop("crossorigin") || this.iscrossoriginurl(url)) { crossorigin = " crossorigin"; } } if(url.indexof('data:')>=0){ this.$clone = ($clone = $("')); }else{ this.$clone = ($clone = $("')); } $clone.one("load", function () { image.naturalwidth = this.naturalwidth || $clone.width(); image.naturalheight = this.naturalheight || $clone.height(); image.aspectratio = image.naturalwidth / image.naturalheight; _this.url = url; _this.ready = true; _this.build(); }); // hide and prepend the clone iamge to the document body (don't append to). $clone.addclass(class_invisible).prependto("body"); }, iscrossoriginurl: function (url) { var parts = url.match(/^(https?:)\/\/([^\:\/\?#]+):?(\d*)/i); if ((parts && (parts[1] !== location.protocol || parts[2] !== location.hostname || parts[3] !== location.port))) { return true; } return false; }, build: function () { var $this = this.$element, defaults = this.defaults, buildevent, $cropper; if (!this.ready) { return; } if (this.built) { this.unbuild(); } $this.one(event_build, defaults.build); // only trigger once buildevent = $.event(event_build); $this.trigger(buildevent); if (buildevent.isdefaultprevented()) { return; } // create cropper elements this.$cropper = ($cropper = $(cropper.template)); // hide the original image $this.addclass(class_hidden); // show and prepend the clone iamge to the cropper this.$clone.removeclass(class_invisible).prependto($cropper); // save original image for rotation if (!this.rotated) { this.$original = this.$clone.clone(); // append the image to document to avoid "ns_error_not_available" error on firefox when call the "drawimage" method. this.$original.addclass(class_hidden).prependto(this.$cropper); this.originalimage = $.extend({}, this.image); } this.$container = $this.parent(); this.$container.append($cropper); this.$canvas = $cropper.find(".cropper-canvas"); this.$dragger = $cropper.find(".cropper-dragger"); this.$viewer = $cropper.find(".cropper-viewer"); defaults.autocrop ? (this.cropped = true) : this.$dragger.addclass(class_hidden); defaults.dragcrop && this.setdragmode("crop"); defaults.modal && this.$canvas.addclass(class_modal); !defaults.dashed && this.$dragger.find(".cropper-dashed").addclass(class_hidden); !defaults.movable && this.$dragger.find(".cropper-face").data(string_directive, "move"); !defaults.resizable && this.$dragger.find(".cropper-line, .cropper-point").addclass(class_hidden); this.$scope = defaults.multiple ? this.$cropper : $document; this.addlisteners(); this.initpreview(); this.built = true; this.update(); $this.one(event_built, defaults.built); // only trigger once $this.trigger(event_built); }, unbuild: function () { if (!this.built) { return; } this.built = false; this.removelisteners(); this.$preview.empty(); this.$preview = null; this.$dragger = null; this.$canvas = null; this.$container = null; this.$cropper.remove(); this.$cropper = null; }, update: function (data) { this.initcontainer(); this.initcropper(); this.initimage(); this.initdragger(); if (data) { this.setdata(data, true); this.setdragmode("crop"); } else { this.setdata(this.defaults.data); } }, resize: function () { cleartimeout(this.resizing); this.resizing = settimeout($.proxy(this.update, this, this.getdata()), 200); }, preview: function () { var image = this.image, dragger = this.dragger, width = image.width, height = image.height, left = dragger.left - image.left, top = dragger.top - image.top; this.$viewer.find("img").css({ width: round(width), height: round(height), marginleft: -round(left), margintop: -round(top) }); this.$preview.each(function () { var $this = $(this), ratio = $this.width() / dragger.width; $this.find("img").css({ width: round(width * ratio), height: round(height * ratio), marginleft: -round(left * ratio), margintop: -round(top * ratio) }); }); }, addlisteners: function () { var defaults = this.defaults; this.$element.on(event_drag_start, defaults.dragstart).on(event_drag_move, defaults.dragmove).on(event_drag_end, defaults.dragend); this.$cropper.on(event_mouse_down, (this._dragstart = $.proxy(this.dragstart, this))).on(event_dblclick, (this._dblclick = $.proxy(this.dblclick, this))); defaults.zoomable && this.$cropper.on(event_wheel, (this._wheel = $.proxy(this.wheel, this))); this.$scope.on(event_mouse_move, (this._dragmove = $.proxy(this.dragmove, this))).on(event_mouse_up, (this._dragend = $.proxy(this.dragend, this))); $window.on(event_resize, (this._resize = $.proxy(this.resize, this))); }, removelisteners: function () { var defaults = this.defaults; this.$element.off(event_drag_start, defaults.dragstart).off(event_drag_move, defaults.dragmove).off(event_drag_end, defaults.dragend); this.$cropper.off(event_mouse_down, this._dragstart).off(event_dblclick, this._dblclick); defaults.zoomable && this.$cropper.off(event_wheel, this._wheel); this.$scope.off(event_mouse_move, this._dragmove).off(event_mouse_up, this._dragend); $window.off(event_resize, this._resize); }, initpreview: function () { var img = ''; this.$preview = $(this.defaults.preview); this.$viewer.html(img); this.$preview.html(img).find("img").css("csstext", "min-width:0!important;min-height:0!important;max-width:none!important;max-height:none!important;"); }, initcontainer: function () { var $container = this.$container; this.container = { width: max($container.width(), 300), height: max($container.height(), 150) }; }, initcropper: function () { var container = this.container, image = this.image, cropper; if (((image.naturalwidth * container.height / image.naturalheight) - container.width) >= 0) { cropper = { width: container.width, height: container.width / image.aspectratio, left: 0 }; cropper.top = (container.height - cropper.height) / 2; } else { cropper = { width: container.height * image.aspectratio, height: container.height, top: 0 }; cropper.left = (container.width - cropper.width) / 2; } this.$cropper.css({ width: round(cropper.width), height: round(cropper.height), left: round(cropper.left), top: round(cropper.top) }); this.cropper = cropper; }, initimage: function () { var image = this.image, cropper = this.cropper, defaultimage = { _width: cropper.width, _height: cropper.height, width: cropper.width, height: cropper.height, left: 0, top: 0, ratio: cropper.width / image.naturalwidth }; this.defaultimage = $.extend({}, image, defaultimage); if (image._width !== cropper.width || image._height !== cropper.height) { $.extend(image, defaultimage); } else { image = $.extend({}, defaultimage, image); // reset image ratio if (this.replaced) { this.replaced = false; image.ratio = defaultimage.ratio; } } this.image = image; this.renderimage(); }, renderimage: function (mode) { var image = this.image; if (mode === "zoom") { image.left -= (image.width - image.oldwidth) / 2; image.top -= (image.height - image.oldheight) / 2; } image.left = min(max(image.left, image._width - image.width), 0); image.top = min(max(image.top, image._height - image.height), 0); this.$clone.css({ width: round(image.width), height: round(image.height), marginleft: round(image.left), margintop: round(image.top) }); if (mode) { this.defaults.done(this.getdata()); this.preview(); } }, initdragger: function () { var defaults = this.defaults, cropper = this.cropper, // if not set, use the original aspect ratio of the image. aspectratio = defaults.aspectratio || this.image.aspectratio, ratio = this.image.ratio, dragger; if (((cropper.height * aspectratio) - cropper.width) >= 0) { dragger = { height: cropper.width / aspectratio, width: cropper.width, left: 0, top: (cropper.height - (cropper.width / aspectratio)) / 2, maxwidth: cropper.width, maxheight: cropper.width / aspectratio }; } else { dragger = { height: cropper.height, width: cropper.height * aspectratio, left: (cropper.width - (cropper.height * aspectratio)) / 2, top: 0, maxwidth: cropper.height * aspectratio, maxheight: cropper.height }; } dragger.minwidth = 0; dragger.minheight = 0; if (defaults.aspectratio) { if (isfinite(defaults.maxwidth)) { dragger.maxwidth = min(dragger.maxwidth, defaults.maxwidth * ratio); dragger.maxheight = dragger.maxwidth / aspectratio; } else if (isfinite(defaults.maxheight)) { dragger.maxheight = min(dragger.maxheight, defaults.maxheight * ratio); dragger.maxwidth = dragger.maxheight * aspectratio; } if (defaults.minwidth > 0) { dragger.minwidth = max(0, defaults.minwidth * ratio); dragger.minheight = dragger.minwidth / aspectratio; } else if (defaults.minheight > 0) { dragger.minheight = max(0, defaults.minheight * ratio); dragger.minwidth = dragger.minheight * aspectratio; } } else { dragger.maxwidth = min(dragger.maxwidth, defaults.maxwidth * ratio); dragger.maxheight = min(dragger.maxheight, defaults.maxheight * ratio); dragger.minwidth = max(0, defaults.minwidth * ratio); dragger.minheight = max(0, defaults.minheight * ratio); } // minwidth can't be greater than maxwidth, and minheight too. dragger.minwidth = min(dragger.maxwidth, dragger.minwidth); dragger.minheight = min(dragger.maxheight, dragger.minheight); // center the dragger by default dragger.height *= defaults.autocroparea; dragger.width *= defaults.autocroparea; dragger.left = (cropper.width - dragger.width) / 2; dragger.top = (cropper.height - dragger.height) / 2; dragger.oldleft = dragger.left; dragger.oldtop = dragger.top; this.defaultdragger = dragger; this.dragger = $.extend({}, dragger); }, renderdragger: function () { var dragger = this.dragger, cropper = this.cropper; if (dragger.width > dragger.maxwidth) { dragger.width = dragger.maxwidth; dragger.left = dragger.oldleft; } else if (dragger.width < dragger.minwidth) { dragger.width = dragger.minwidth; dragger.left = dragger.oldleft; } if (dragger.height > dragger.maxheight) { dragger.height = dragger.maxheight; dragger.top = dragger.oldtop; } else if (dragger.height < dragger.minheight) { dragger.height = dragger.minheight; dragger.top = dragger.oldtop; } dragger.left = min(max(dragger.left, 0), cropper.width - dragger.width); dragger.top = min(max(dragger.top, 0), cropper.height - dragger.height); dragger.oldleft = dragger.left; dragger.oldtop = dragger.top; // re-render the dragger this.dragger = dragger; if (!this.disabled) { this.defaults.done(this.getdata()); } this.$dragger.css({ width: round(dragger.width), height: round(dragger.height), left: round(dragger.left), top: round(dragger.top) }); this.preview(); }, reset: function (deep) { if (!this.cropped) { return; } if (deep) { this.defaults.data = {}; } this.image = $.extend({}, this.defaultimage); this.renderimage(); this.dragger = $.extend({}, this.defaultdragger); this.setdata(this.defaults.data); }, clear: function () { if (!this.cropped) { return; } this.cropped = false; this.setdata({ x: 0, y: 0, width: 0, height: 0 }); this.$canvas.removeclass(class_modal); this.$dragger.addclass(class_hidden); }, destroy: function () { var $this = this.$element; if (!this.ready) { return; } this.unbuild(); $this.removeclass(class_hidden).removedata("cropper"); if (this.rotated) { $this.attr("src", this.$original.attr("src")); } }, replace: function (url, /*internal*/ rotated) { var _this = this, $this = this.$element, element = this.element, context; if (url && url !== this.url && url !== $this.attr("src")) { if (!rotated) { this.rotated = false; this.replaced = true; } if ($this.is("img")) { $this.attr("src", url); this.load(); } else if ($this.is("canvas") && this.support.canvas) { context = element.getcontext("2d"); $('').one("load", function () { element.width = this.width; element.height = this.height; context.clearrect(0, 0, element.width, element.height); context.drawimage(this, 0, 0); _this.load(); }); } } }, setdata: function (data, /*internal*/ once) { var cropper = this.cropper, dragger = this.dragger, image = this.image, aspectratio = this.defaults.aspectratio; if (!this.built || typeof data === string_undefined) { return; } if (data === null || $.isemptyobject(data)) { dragger = $.extend({}, this.defaultdragger); } if ($.isplainobject(data) && !$.isemptyobject(data)) { if (!once) { this.defaults.data = data; } data = this.transformdata(data); if (isnumber(data.x) && data.x <= cropper.width - image.left) { dragger.left = data.x + image.left; } if (isnumber(data.y) && data.y <= cropper.height - image.top) { dragger.top = data.y + image.top; } if (aspectratio) { if (isnumber(data.width) && data.width <= dragger.maxwidth && data.width >= dragger.minwidth) { dragger.width = data.width; dragger.height = dragger.width / aspectratio; } else if (isnumber(data.height) && data.height <= dragger.maxheight && data.height >= dragger.minheight) { dragger.height = data.height; dragger.width = dragger.height * aspectratio; } } else { if (isnumber(data.width) && data.width <= dragger.maxwidth && data.width >= dragger.minwidth) { dragger.width = data.width; } if (isnumber(data.height) && data.height <= dragger.maxheight && data.height >= dragger.minheight) { dragger.height = data.height; } } } this.dragger = dragger; this.renderdragger(); }, getdata: function (rounded) { var dragger = this.dragger, image = this.image, data = {}; if (this.built) { data = { x: dragger.left - image.left, y: dragger.top - image.top, width: dragger.width, height: dragger.height }; data = this.transformdata(data, true, rounded); } return data; }, transformdata: function (data, reversed, rounded) { var ratio = this.image.ratio, result = {}; $.each(data, function (i, n) { n = num(n); if (regexp_options.test(i) && !isnan(n)) { // not round when set data. result[i] = reversed ? (rounded ? round(n / ratio) : n / ratio) : n * ratio; } }); return result; }, setaspectratio: function (aspectratio) { var freeratio = aspectratio === "auto"; aspectratio = num(aspectratio); if (freeratio || (!isnan(aspectratio) && aspectratio > 0)) { this.defaults.aspectratio = freeratio ? nan : aspectratio; if (this.built) { this.initdragger(); this.renderdragger(); } } }, getimagedata: function () { var data = {}; if (this.ready) { $.each(this.image, function (name, value) { if (regexp_properties.test(name)) { data[name] = value; } }); } return data; }, getdataurl: function (options, type, quality) { var canvas = $("")[0], data = this.getdata(), dataurl = "", context; if (!$.isplainobject(options)) { quality = type; type = options; options = {}; } options = $.extend({ width: data.width, height: data.height }, options); if (this.cropped && this.support.canvas) { canvas.width = options.width; canvas.height = options.height; context = canvas.getcontext("2d"); if (type === "image/jpeg") { context.fillstyle = "#fff"; context.fillrect(0, 0, options.width, options.height); } context.drawimage(this.$clone[0], data.x, data.y, data.width, data.height, 0, 0, options.width, options.height); dataurl = canvas.todataurl("image/jpeg", 0.6);//输出压缩jpg } return dataurl; }, setdragmode: function (mode) { var $canvas = this.$canvas, defaults = this.defaults, cropable = false, movable = false; if (!this.built || this.disabled) { return; } switch (mode) { case "crop": if (defaults.dragcrop) { cropable = true; $canvas.data(string_directive, mode); } break; case "move": movable = true; $canvas.data(string_directive, mode); break; default: $canvas.removedata(string_directive); } $canvas.toggleclass(class_crop, cropable).toggleclass(class_move, movable); }, enable: function () { if (this.built) { this.disabled = false; this.$cropper.removeclass(class_disabled); } }, disable: function () { if (this.built) { this.disabled = true; this.$cropper.addclass(class_disabled); } }, rotate: function (degree) { var image = this.image; degree = num(degree) || 0; if (!this.built || degree === 0 || this.disabled || !this.defaults.rotatable || !this.support.canvas) { return; } this.rotated = true; degree = (image.rotate = (image.rotate + degree) % 360); // replace with "true" to prevent to override the original image this.replace(this.getrotateddataurl(degree), true); }, getrotateddataurl: function (degree) { var canvas = $("")[0], context = canvas.getcontext("2d"), arc = degree * math.pi / 180, deg = abs(degree) % 180, acuteangle = deg > 90 ? (180 - deg) : deg, acuteanglearc = acuteangle * math.pi / 180, originalimage = this.originalimage, naturalwidth = originalimage.naturalwidth, naturalheight = originalimage.naturalheight, width = abs(naturalwidth * cos(acuteanglearc) + naturalheight * sin(acuteanglearc)), height = abs(naturalwidth * sin(acuteanglearc) + naturalheight * cos(acuteanglearc)); canvas.width = width; canvas.height = height; context.save(); context.translate(width / 2, height / 2); context.rotate(arc); context.drawimage(this.$original[0], -naturalwidth / 2, -naturalheight / 2, naturalwidth, naturalheight); context.restore(); return canvas.todataurl(); }, zoom: function (delta) { var image = this.image, width, height, range; delta = num(delta); if (!this.built || !delta || this.disabled || !this.defaults.zoomable) { return; } width = image.width * (1 + delta); height = image.height * (1 + delta); range = width / image._width; if (range > 10) { return; } if (range < 1) { width = image._width; height = image._height; } if (range <= 1) { this.setdragmode("crop"); } else { this.setdragmode("move"); } image.oldwidth = image.width; image.oldheight = image.height; image.width = width; image.height = height; image.ratio = image.width / image.naturalwidth; this.renderimage("zoom"); }, dblclick: function () { if (this.disabled) { return; } if (this.$canvas.hasclass(class_crop)) { this.setdragmode("move"); } else { this.setdragmode("crop"); } }, wheel: function (event) { var e = event.originalevent, msdeltay = 117.25, // ie mozdelaty = 5, // firefox webkitdelaty = 166.66665649414062, // chrome, opera zoomdelta = 0.1, // 10% delta; if (this.disabled) { return; } event.preventdefault(); if (e.deltay) { delta = e.deltay; delta = delta % mozdelaty === 0 ? delta / mozdelaty : delta % msdeltay === 0 ? delta / msdeltay : delta / webkitdelaty; } else { delta = e.wheeldelta ? -e.wheeldelta / 120 : (e.detail ? e.detail / 3 : 0); } this.zoom(delta * zoomdelta); }, dragstart: function (event) { var touches = event.originalevent.touches, e = event, directive, dragstartevent, toucheslength; if (this.disabled) { return; } if (touches) { toucheslength = touches.length; if (toucheslength > 1) { if (this.defaults.zoomable && toucheslength === 2) { e = touches[1]; this.startx2 = e.pagex; this.starty2 = e.pagey; directive = "zoom"; } else { return; } } e = touches[0]; } directive = directive || $(e.target).data(string_directive); if (regexp_directives.test(directive)) { event.preventdefault(); dragstartevent = $.event(event_drag_start); this.$element.trigger(dragstartevent); if (dragstartevent.isdefaultprevented()) { return; } this.directive = directive; this.cropping = false; this.startx = e.pagex; this.starty = e.pagey; if (directive === "crop") { this.cropping = true; this.$canvas.addclass(class_modal); } } }, dragmove: function (event) { var touches = event.originalevent.touches, e = event, dragmoveevent, toucheslength; if (this.disabled) { return; } if (touches) { toucheslength = touches.length; if (toucheslength > 1) { if (this.defaults.zoomable && toucheslength === 2) { e = touches[1]; this.endx2 = e.pagex; this.endy2 = e.pagey; } else { return; } } e = touches[0]; } if (this.directive) { event.preventdefault(); dragmoveevent = $.event(event_drag_move); this.$element.trigger(dragmoveevent); if (dragmoveevent.isdefaultprevented()) { return; } this.endx = e.pagex; this.endy = e.pagey; this.dragging(); } }, dragend: function (event) { var dragendevent; if (this.disabled) { return; } if (this.directive) { event.preventdefault(); dragendevent = $.event(event_drag_end); this.$element.trigger(dragendevent); if (dragendevent.isdefaultprevented()) { return; } if (this.cropping) { this.cropping = false; this.$canvas.toggleclass(class_modal, this.cropped && this.defaults.modal); } this.directive = ""; } }, dragging: function () { var directive = this.directive, image = this.image, cropper = this.cropper, maxwidth = cropper.width, maxheight = cropper.height, dragger = this.dragger, width = dragger.width, height = dragger.height, left = dragger.left, top = dragger.top, right = left + width, bottom = top + height, renderable = true, defaults = this.defaults, aspectratio = defaults.aspectratio, range = { x: this.endx - this.startx, y: this.endy - this.starty }, offset; if (aspectratio) { range.x = range.y * aspectratio; range.y = range.x / aspectratio; } switch (directive) { // move dragger case "all": left += range.x; top += range.y; break; // resize dragger case "e": if (range.x >= 0 && (right >= maxwidth || aspectratio && (top <= 0 || bottom >= maxheight))) { renderable = false; break; } width += range.x; if (aspectratio) { height = width / aspectratio; top -= range.y / 2; } if (width < 0) { directive = "w"; width = 0; } break; case "n": if (range.y <= 0 && (top <= 0 || aspectratio && (left <= 0 || right >= maxwidth))) { renderable = false; break; } height -= range.y; top += range.y; if (aspectratio) { width = height * aspectratio; left += range.x / 2; } if (height < 0) { directive = "s"; height = 0; } break; case "w": if (range.x <= 0 && (left <= 0 || aspectratio && (top <= 0 || bottom >= maxheight))) { renderable = false; break; } width -= range.x; left += range.x; if (aspectratio) { height = width / aspectratio; top += range.y / 2; } if (width < 0) { directive = "e"; width = 0; } break; case "s": if (range.y >= 0 && (bottom >= maxheight || aspectratio && (left <= 0 || right >= maxwidth))) { renderable = false; break; } height += range.y; if (aspectratio) { width = height * aspectratio; left -= range.x / 2; } if (height < 0) { directive = "n"; height = 0; } break; case "ne": if (aspectratio) { if (range.y <= 0 && (top <= 0 || right >= maxwidth)) { renderable = false; break; } height -= range.y; top += range.y; width = height * aspectratio; } else { if (range.x >= 0) { if (right < maxwidth) { width += range.x; } else if (range.y <= 0 && top <= 0) { renderable = false; } } else { width += range.x; } if (range.y <= 0) { if (top > 0) { height -= range.y; top += range.y; } } else { height -= range.y; top += range.y; } } if (height < 0) { directive = "sw"; height = 0; width = 0; } break; case "nw": if (aspectratio) { if (range.y <= 0 && (top <= 0 || left <= 0)) { renderable = false; break; } height -= range.y; top += range.y; width = height * aspectratio; left += range.x; } else { if (range.x <= 0) { if (left > 0) { width -= range.x; left += range.x; } else if (range.y <= 0 && top <= 0) { renderable = false; } } else { width -= range.x; left += range.x; } if (range.y <= 0) { if (top > 0) { height -= range.y; top += range.y; } } else { height -= range.y; top += range.y; } } if (height < 0) { directive = "se"; height = 0; width = 0; } break; case "sw": if (aspectratio) { if (range.x <= 0 && (left <= 0 || bottom >= maxheight)) { renderable = false; break; } width -= range.x; left += range.x; height = width / aspectratio; } else { if (range.x <= 0) { if (left > 0) { width -= range.x; left += range.x; } else if (range.y >= 0 && bottom >= maxheight) { renderable = false; } } else { width -= range.x; left += range.x; } if (range.y >= 0) { if (bottom < maxheight) { height += range.y; } } else { height += range.y; } } if (width < 0) { directive = "ne"; height = 0; width = 0; } break; case "se": if (aspectratio) { if (range.x >= 0 && (right >= maxwidth || bottom >= maxheight)) { renderable = false; break; } width += range.x; height = width / aspectratio; } else { if (range.x >= 0) { if (right < maxwidth) { width += range.x; } else if (range.y >= 0 && bottom >= maxheight) { renderable = false; } } else { width += range.x; } if (range.y >= 0) { if (bottom < maxheight) { height += range.y; } } else { height += range.y; } } if (width < 0) { directive = "nw"; height = 0; width = 0; } break; // move image case "move": image.left += range.x; image.top += range.y; this.renderimage("move"); renderable = false; break; // scale image case "zoom": if (defaults.zoomable) { this.zoom(function (x, y, x1, y1, x2, y2) { return (sqrt(x2 * x2 + y2 * y2) - sqrt(x1 * x1 + y1 * y1)) / sqrt(x * x + y * y); }( image.width, image.height, abs(this.startx - this.startx2), abs(this.starty - this.starty2), abs(this.endx - this.endx2), abs(this.endy - this.endy2) )); this.endx2 = this.startx2; this.endy2 = this.starty2; } break; // crop image case "crop": if (range.x && range.y) { offset = this.$cropper.offset(); left = this.startx - offset.left; top = this.starty - offset.top; width = dragger.minwidth; height = dragger.minheight; if (range.x > 0) { if (range.y > 0) { directive = "se"; } else { directive = "ne"; top -= height; } } else { if (range.y > 0) { directive = "sw"; left -= width; } else { directive = "nw"; left -= width; top -= height; } } // show the dragger if is hidden if (!this.cropped) { this.cropped = true; this.$dragger.removeclass(class_hidden); } } break; // no default } if (renderable) { dragger.width = width; dragger.height = height; dragger.left = left; dragger.top = top; this.directive = directive; this.renderdragger(); } // override this.startx = this.endx; this.starty = this.endy; } }; // use the string compressor: strmin (https://github.com/fengyuanchen/strmin) cropper.template = (function (source, words) { words = words.split(","); return source.replace(/\d+/g, function (i) { return words[i]; }); })('<0 6="5-container"><0 6="5-canvas"><0 6="5-dragger"><1 6="5-viewer"><1 6="5-8 8-h"><1 6="5-8 8-v"><1 6="5-face" 3-2="all"><1 6="5-7 7-e" 3-2="e"><1 6="5-7 7-n" 3-2="n"><1 6="5-7 7-w" 3-2="w"><1 6="5-7 7-s" 3-2="s"><1 6="5-4 4-e" 3-2="e"><1 6="5-4 4-n" 3-2="n"><1 6="5-4 4-w" 3-2="w"><1 6="5-4 4-s" 3-2="s"><1 6="5-4 4-ne" 3-2="ne"><1 6="5-4 4-nw" 3-2="nw"><1 6="5-4 4-sw" 3-2="sw"><1 6="5-4 4-se" 3-2="se">', "div,span,directive,data,point,cropper,class,line,dashed"); /* template source:
*/ cropper.defaults = { // basic aspectratio: "auto", autocroparea: 0.8, // 80% data: { // x: 0, // y: 0, // width: 300, // height: 150 }, done: $.noop, preview: "", // toggles multiple: false, autocrop: true, dragcrop: true, dashed: true, modal: true, movable: true, resizable: true, zoomable: true, rotatable: true, checkimageorigin: true, // dimensions minwidth: 0, minheight: 0, maxwidth: infinity, maxheight: infinity, // events build: null, built: null, dragstart: null, dragmove: null, dragend: null }; cropper.setdefaults = function (options) { $.extend(cropper.defaults, options); }; // save the other cropper cropper.other = $.fn.cropper; // register as jquery plugin $.fn.cropper = function (options) { var args = [].slice.call(arguments, 1), result; this.each(function () { var $this = $(this), data = $this.data("cropper"), fn; if (!data) { $this.data("cropper", (data = new cropper(this, options))); } if (typeof options === "string" && $.isfunction((fn = data[options]))) { result = fn.apply(data, args); } }); return (typeof result !== string_undefined ? result : this); }; $.fn.cropper.constructor = cropper; $.fn.cropper.setdefaults = cropper.setdefaults; // no conflict $.fn.cropper.noconflict = function () { $.fn.cropper = cropper.other; return this; }; });