2024-07-22 18:17:27 +08:00
|
|
|
!function(){var t=document.createElement("canvas");function i(t,i,e){return t+i<e&&(t=e-i),t>0?0:t}t=!(!t.getContext||!t.getContext("2d"));var e="cropbox";function o(o){function s(t,i,e){this.width=null,this.height=null,this.img_width=null,this.img_height=null,this.img_left=0,this.img_top=0,this.minPercent=null,this.options=i,this.$image=t,this.$image.hide().prop("draggable",!1).addClass("cropImage").wrap('<div class="cropFrame" />'),this.$frame=this.$image.parent(),this.on_load=e||function(){},this.init()}s.prototype={init:function(){var t,i=this,s=o("<div/>",{class:"cropControls"}).append(o("<span>"+this.options.label+"</span>")).append(o("<button/>",{class:"cropZoomIn",type:"button"}).on("click",o.proxy(this.zoomIn,this))).append(o("<button/>",{class:"cropZoomOut",type:"button"}).on("click",o.proxy(this.zoomOut,this)));(this.$frame.append(this.options.controls||s),this.updateOptions(),!(BROWSER&&BROWSER.ie>8)&&BROWSER.ie||"function"!=typeof o.fn.hammer&&"undefined"==typeof Hammer)?(this.$image.on("dragstart",function(){return!1}),this.$image.on("mousedown."+e,function(t){var s={startX:i.img_left,startY:i.img_top};t.preventDefault(),o(document).on("mousemove."+e,function(e){s.dx=e.pageX-t.pageX,s.dy=e.pageY-t.pageY,i.drag.call(i,s,!0)}).on("mouseup."+e,function(){i.update.call(i),o(document).off("mouseup."+e),o(document).off("mousemove."+e)})})):("function"==typeof o.fn.hammer?this.$image.hammer():Hammer(this.$image.get(0))).on("touch",function(t){t.gesture.preventDefault()}).on("dragleft dragright dragup dragdown",function(e){t||(t={startX:i.img_left,startY:i.img_top}),t.dx=e.gesture.deltaX,t.dy=e.gesture.deltaY,e.gesture.preventDefault(),e.gesture.stopPropagation(),i.drag.call(i,t,!0)}).on("release",function(e){e.gesture.preventDefault(),t=null,i.update.call(i)}).on("doubletap",function(t){t.gesture.preventDefault(),i.zoomIn.call(i)}).on("pinchin",function(t){t.gesture.preventDefault(),i.zoomOut.call(i)}).on("pinchout",function(t){t.gesture.preventDefault(),i.zoomIn.call(i)});o.fn.mousewheel&&this.$image.on("mousewheel."+e,function(t){t.preventDefault(),t.deltaY<0?i.zoomIn.call(i):i.zoomOut.call(i)})},updateOptions:function(){var t=this;t.img_top=0,t.img_left=0,t.$image.css({width:"",left:t.img_left,top:t.img_top}),t.$frame.width(t.options.width).height(t.options.height),t.$frame.off("."+e),t.$frame.removeClass("hover"),"always"===t.options.showControls||"auto"===t.options.showControls&&("ontouchstart"in window||"onmsgesturechange"in window)?t.$frame.addClass("hover"):"never"!==t.options.showControls&&(t.$frame.on("mouseenter."+e,function(){t.$frame.addClass("hover")}),t.$frame.on("mouseleave."+e,function(){t.$frame.removeClass("hover")}));var i=new Image;i.onload=function(){t.width=i.width,t.height=i.height,i.src="",i.onload=null,t.percent=void 0,t.fit.call(t),t.options.result?t.setCrop.call(t,t.options.result):t.zoom.call(t,t.minPercent),t.$image.fadeIn("fast"),t.on_load.call(t)},i.src=t.$image.attr("src")},remove:function(){var t;"function"==typeof o.fn.hammer?t=this.$image.hammer():"undefined"!=typeof Hammer&&(t=Hammer(this.$image.get(0))),t&&t.off("mousedown dragleft dragright dragup dragdown release doubletap pinchin pinchout"),this.$frame.off("."+e),this.$image.off("."+e),this.$image.css({width:"",left:"",top:""}),this.$image.removeClass("cropImage"),this.$image.removeData(e),this.$image.insertAfter(this.$frame),this.$frame.removeClass("cropFrame"),this.$frame.removeAttr("style"),this.$frame.empty(),this.$frame.hide()},fit:function(){var t=this.options.width/this.width,i=this.options.height/this.height;this.minPercent=t>=i?t:i},setCrop:function(t){this.percent=Math.max(this.options.width/t.cropW,this.options.height/t.cropH),this.img_width=Math.ceil(this.width*this.percent),this.img_height=Math.ceil(this.height*this.percent),this.img_left=-Math.floor(t.cropX*this.percent),this.img_top=-Math.floor(t.cropY*this.percent),this.$image.css({width:this.img_width,left:this.img_left,top:this.img_top}),this.update()},zoom:function(t){var e=this.percent;if(this.percent=Math.max(this.minPercent,Math.min(this.options.maxZo
|