we-cropper.min.js 12 KB

123456
  1. /**
  2. * we-cropper v1.3.9
  3. * (c) 2020 dlhandsome
  4. * @license MIT
  5. */
  6. !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.WeCropper=e()}(this,function(){"use strict";var t=void 0,e=["touchstarted","touchmoved","touchended"];function r(n){for(var o=[],t=arguments.length-1;0<t--;)o[t]=arguments[t+1];e.forEach(function(t,e){void 0!==o[e]&&(n[t]=o[e])})}function n(){return t||(t=wx.getSystemInfoSync()),t}var o={},a={id:{default:"cropper",get:function(){return o.id},set:function(t){"string"!=typeof t&&console.error("id\uff1a"+t+" is invalid"),o.id=t}},width:{default:750,get:function(){return o.width},set:function(t){"number"!=typeof t&&console.error("width\uff1a"+t+" is invalid"),o.width=t}},height:{default:750,get:function(){return o.height},set:function(t){"number"!=typeof t&&console.error("height\uff1a"+t+" is invalid"),o.height=t}},pixelRatio:{default:n().pixelRatio,get:function(){return o.pixelRatio},set:function(t){"number"!=typeof t&&console.error("pixelRatio\uff1a"+t+" is invalid"),o.pixelRatio=t}},scale:{default:2.5,get:function(){return o.scale},set:function(t){"number"!=typeof t&&console.error("scale\uff1a"+t+" is invalid"),o.scale=t}},zoom:{default:5,get:function(){return o.zoom},set:function(t){"number"!=typeof t?console.error("zoom\uff1a"+t+" is invalid"):(t<0||10<t)&&console.error("zoom should be ranged in 0 ~ 10"),o.zoom=t}},src:{default:"",get:function(){return o.src},set:function(t){"string"!=typeof t&&console.error("src\uff1a"+t+" is invalid"),o.src=t}},cut:{default:{},get:function(){return o.cut},set:function(t){"object"!=typeof t&&console.error("cut\uff1a"+t+" is invalid"),o.cut=t}},boundStyle:{default:{},get:function(){return o.boundStyle},set:function(t){"object"!=typeof t&&console.error("boundStyle\uff1a"+t+" is invalid"),o.boundStyle=t}},onReady:{default:null,get:function(){return o.ready},set:function(t){o.ready=t}},onBeforeImageLoad:{default:null,get:function(){return o.beforeImageLoad},set:function(t){o.beforeImageLoad=t}},onImageLoad:{default:null,get:function(){return o.imageLoad},set:function(t){o.imageLoad=t}},onBeforeDraw:{default:null,get:function(){return o.beforeDraw},set:function(t){o.beforeDraw=t}}},i=n().windowWidth;var f="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function c(t,e){return t(e={exports:{}},e.exports),e.exports}var u=c(function(t,e){e.isStr=function(t){return"string"==typeof t},e.isNum=function(t){return"number"==typeof t},e.isArr=Array.isArray,e.isUndef=function(t){return void 0===t},e.isTrue=function(t){return!0===t},e.isFalse=function(t){return!1===t},e.isFunc=function(t){return"function"==typeof t},e.isObj=e.isObject=function(t){return null!==t&&"object"==typeof t};var n=Object.prototype.toString;e.isPlainObject=function(t){return"[object Object]"===n.call(t)};var o=Object.prototype.hasOwnProperty;e.hasOwn=function(t,e){return o.call(t,e)},e.noop=function(t,e,n){},e.isValidArrayIndex=function(t){var e=parseFloat(String(t));return 0<=e&&Math.floor(e)===e&&isFinite(t)}}),l=u.isFunc,g=u.isPlainObject,s=["ready","beforeImageLoad","beforeDraw","imageLoad"];function d(r){return function(t){for(var o=[],e=arguments.length-1;0<e--;)o[e]=arguments[e+1];return void 0===t&&(t={}),new Promise(function(e,n){t.success=function(t){e(t)},t.fail=function(t){n(t)},r.apply(void 0,[t].concat(o))})}}function p(e,n){return void 0===n&&(n=!1),new Promise(function(t){e.draw(n,t)})}var v=d(wx.getImageInfo),y=d(wx.canvasToTempFilePath),h=c(function(u,h){!function(t){var e=h,n=u&&u.exports==e&&u,o="object"==typeof f&&f;o.global!==o&&o.window!==o||(t=o);var r=function(t){this.message=t};(r.prototype=new Error).name="InvalidCharacterError";var s=function(t){throw new r(t)},d="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",c=/[\t\n\f\r ]/g,a={encode:function(t){t=String(t),/[^\0-\xFF]/.test(t)&&s("The string to be encoded contains characters outside of the Latin1 range.");for(var e,n,o,r,a=t.length%3,i="",c=-1,u=t.length-a;++c<u;)e=t.charCodeAt(c)<<16,n=t.charCodeAt(++c)<<8,o=t.charCodeAt(++c),i+=d.charAt((r=e+n+o)>>18&63)+d.charAt(r>>12&63)+d.charAt(r>>6&63)+d.charAt(63&r);return 2==a?(e=t.charCodeAt(c)<<8,n=t.charCodeAt(++c),i+=d.charAt((r=e+n)>>10)+d.charAt(r>>4&63)+d.charAt(r<<2&63)+"="):1==a&&(r=t.charCodeAt(c),i+=d.charAt(r>>2)+d.charAt(r<<4&63)+"=="),i},decode:function(t){var e=(t=String(t).replace(c,"")).length;e%4==0&&(e=(t=t.replace(/==?$/,"")).length),(e%4==1||/[^+a-zA-Z0-9/]/.test(t))&&s("Invalid character: the string to be decoded is not correctly encoded.");for(var n,o,r=0,a="",i=-1;++i<e;)o=d.indexOf(t.charAt(i)),n=r%4?64*n+o:o,r++%4&&(a+=String.fromCharCode(255&n>>(-2*r&6)));return a},version:"0.1.0"};if(e&&!e.nodeType)if(n)n.exports=a;else for(var i in a)a.hasOwnProperty(i)&&(e[i]=a[i]);else t.base64=a}(f)});function x(t){var e="";if("string"==typeof t)e=t;else for(var n=0;n<t.length;n++)e+=String.fromCharCode(t[n]);return h.encode(e)}function m(t,e,n,o,r,a,i){var c,u,s,d,h,f;void 0===i&&(i=function(){}),void 0===a&&(a="png"),a="image/"+a.toLowerCase().replace(/jpg/i,"jpeg").match(/png|jpeg|bmp|gif/)[0],/bmp/.test(a)?(c=t,u=e,s=n,d=o,h=r,f=function(t,e){var n=function(t){var e=t.width,n=t.height,o=e*n*3,r=o+54,a=[66,77,255&r,r>>8&255,r>>16&255,r>>24&255,0,0,0,0,54,0,0,0],i=[40,0,0,0,255&e,e>>8&255,e>>16&255,e>>24&255,255&n,n>>8&255,n>>16&255,n>>24&255,1,0,24,0,0,0,0,0,255&o,o>>8&255,o>>16&255,o>>24&255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],c=(4-3*e%4)%4,u=t.data,s="",d=e<<2,h=n,f=String.fromCharCode;do{for(var l=d*(h-1),g="",p=0;p<e;p++){var v=p<<2;g+=f(u[l+v+2])+f(u[l+v+1])+f(u[l+v])}for(var y=0;y<c;y++)g+=String.fromCharCode(0);s+=g}while(--h);return x(a.concat(i))+x(s)}(t);l(i)&&i("data:"+("image/"+a)+";base64,"+n,e)},wx.canvasGetImageData({canvasId:c,x:u,y:s,width:d,height:h,success:function(t){f(t,null)},fail:function(t){f(null,t)}})):console.error("\u6682\u4e0d\u652f\u6301\u751f\u6210'"+a+"'\u7c7b\u578b\u7684base64\u56fe\u7247")}var b=function(t,e){return void 0===t&&(t={}),void 0===e&&(e=function(){}),m(t.canvasId,t.x,t.y,t.width,t.height,"bmp",e)};var w={touchStart:function(t){var e=t.touches,n=e[0],o=e[1];this.src&&(r(this,!0,null,null),this.__oneTouchStart(n),2<=t.touches.length&&this.__twoTouchStart(n,o))},touchMove:function(t){var e=t.touches,n=e[0],o=e[1];this.src&&(r(this,null,!0),1===t.touches.length&&this.__oneTouchMove(n),2<=t.touches.length&&this.__twoTouchMove(n,o))},touchEnd:function(t){this.src&&(r(this,!1,!1,!0),this.__xtouchEnd())}};var C=function(t){var e,n,o=this,r={};return e=o,n=a,Object.defineProperties(e,n),Object.keys(a).forEach(function(t){r[t]=a[t].default}),Object.assign(o,r,t),o.prepare(),o.attachPage(),o.createCtx(),o.observer(),o.cutt(),o.methods(),o.init(),o.update(),o};return C.prototype.init=function(){var t=this,e=t.src;return t.version="1.3.9","function"==typeof t.onReady&&t.onReady(t.ctx,t),e?t.pushOrign(e):t.updateCanvas(),r(t,!1,!1,!1),t.oldScale=1,t.newScale=1,t},Object.assign(C.prototype,w),C.prototype.prepare=function(){var n=this;n.attachPage=function(){var t=getCurrentPages(),e=t[t.length-1];Object.defineProperty(e,"wecropper",{get:function(){return console.warn("Instance will not be automatically bound to the page after v1.4.0\n\nPlease use a custom instance name instead\n\nExample: \nthis.mycropper = new WeCropper(options)\n\n// ...\nthis.mycropper.getCropperImage()"),n},configurable:!0})},n.createCtx=function(){var t=n.id,e=n.targetId;t?(n.ctx=n.ctx||wx.createCanvasContext(t),n.targetCtx=n.targetCtx||wx.createCanvasContext(e)):console.error("constructor: create canvas context failed, 'id' must be valuable")},n.deviceRadio=i/750},C.prototype.observer=function(){var o=this;o.on=function(t,e){var n;return-1<s.indexOf(t)?l(e)&&("ready"===t?e(o):o["on"+(n=t,n.charAt(0).toUpperCase()+n.slice(1))]=e):console.error("event: "+t+" is invalid"),o}},C.prototype.methods=function(){var a=this,t=a.width,e=a.height,i=a.id,c=a.targetId,u=a.pixelRatio,n=a.cut,s=n.x;void 0===s&&(s=0);var d=n.y;void 0===d&&(d=0);var h=n.width;void 0===h&&(h=t);var f=n.height;void 0===f&&(f=e),a.updateCanvas=function(t){return a.croperTarget&&a.ctx.drawImage(a.croperTarget,a.imgLeft,a.imgTop,a.scaleWidth,a.scaleHeight),l(a.onBeforeDraw)&&a.onBeforeDraw(a.ctx,a),a.setBoundStyle(a.boundStyle),a.ctx.draw(!1,t),a},a.pushOrigin=a.pushOrign=function(t){return a.src=t,l(a.onBeforeImageLoad)&&a.onBeforeImageLoad(a.ctx,a),v({src:t}).then(function(t){var e=t.width/t.height,n=h/f;return a.croperTarget=t.path,e<n?(a.rectX=s,a.baseWidth=h,a.baseHeight=h/e,a.rectY=d-Math.abs((f-a.baseHeight)/2)):(a.rectY=d,a.baseWidth=f*e,a.baseHeight=f,a.rectX=s-Math.abs((h-a.baseWidth)/2)),a.imgLeft=a.rectX,a.imgTop=a.rectY,a.scaleWidth=a.baseWidth,a.scaleHeight=a.baseHeight,a.update(),new Promise(function(t){a.updateCanvas(t)})}).then(function(){l(a.onImageLoad)&&a.onImageLoad(a.ctx,a)})},a.removeImage=function(){return a.src="",a.croperTarget="",p(a.ctx)},a.getCropperBase64=function(t){void 0===t&&(t=function(){}),b({canvasId:i,x:s,y:d,width:h,height:f},t)},a.getCropperImage=function(t,n){var e=t,o={canvasId:i,x:s,y:d,width:h,height:f},r=function(){return Promise.resolve()};return g(e)&&e.original&&(r=function(){return a.targetCtx.drawImage(a.croperTarget,a.imgLeft*u,a.imgTop*u,a.scaleWidth*u,a.scaleHeight*u),o={canvasId:c,x:s*u,y:d*u,width:h*u,height:f*u},p(a.targetCtx)}),r().then(function(){g(e)&&(o=Object.assign({},o,e)),l(e)&&(n=e);var t=o.componentContext?[o,o.componentContext]:[o];return y.apply(null,t)}).then(function(t){var e=t.tempFilePath;return l(n)?n.call(a,e,null):e}).catch(function(t){if(!l(n))throw t;n.call(a,null,t)})}},C.prototype.cutt=function(){var i=this,c=i.width,u=i.height,t=i.cut,s=t.x;void 0===s&&(s=0);var d=t.y;void 0===d&&(d=0);var h=t.width;void 0===h&&(h=c);var f=t.height;void 0===f&&(f=u),i.outsideBound=function(t,e){i.imgLeft=s<=t?s:i.scaleWidth+t-s<=h?s+h-i.scaleWidth:t,i.imgTop=d<=e?d:i.scaleHeight+e-d<=f?d+f-i.scaleHeight:e},i.setBoundStyle=function(t){void 0===t&&(t={});var e=t.color;void 0===e&&(e="#04b00f");var n=t.mask;void 0===n&&(n="rgba(0, 0, 0, 0.3)");var o=t.lineWidth;void 0===o&&(o=1);var r=o/2,a=[{start:{x:s-r,y:d+10-r},step1:{x:s-r,y:d-r},step2:{x:s+10-r,y:d-r}},{start:{x:s-r,y:d+f-10+r},step1:{x:s-r,y:d+f+r},step2:{x:s+10-r,y:d+f+r}},{start:{x:s+h-10+r,y:d-r},step1:{x:s+h+r,y:d-r},step2:{x:s+h+r,y:d+10-r}},{start:{x:s+h+r,y:d+f-10+r},step1:{x:s+h+r,y:d+f+r},step2:{x:s+h-10+r,y:d+f+r}}];i.ctx.beginPath(),i.ctx.setFillStyle(n),i.ctx.fillRect(0,0,s,u),i.ctx.fillRect(s,0,h,d),i.ctx.fillRect(s,d+f,h,u-d-f),i.ctx.fillRect(s+h,0,c-s-h,u),i.ctx.fill(),a.forEach(function(t){i.ctx.beginPath(),i.ctx.setStrokeStyle(e),i.ctx.setLineWidth(o),i.ctx.moveTo(t.start.x,t.start.y),i.ctx.lineTo(t.step1.x,t.step1.y),i.ctx.lineTo(t.step2.x,t.step2.y),i.ctx.stroke()})}},C.prototype.update=function(){var p=this;p.src&&(p.__oneTouchStart=function(t){p.touchX0=Math.round(t.x),p.touchY0=Math.round(t.y)},p.__oneTouchMove=function(t){var e,n;if(p.touchended)return p.updateCanvas();e=Math.round(t.x-p.touchX0),n=Math.round(t.y-p.touchY0);var o=Math.round(p.rectX+e),r=Math.round(p.rectY+n);p.outsideBound(o,r),p.updateCanvas()},p.__twoTouchStart=function(t,e){var n,o,r;p.touchX1=Math.round(p.rectX+p.scaleWidth/2),p.touchY1=Math.round(p.rectY+p.scaleHeight/2),n=Math.round(e.x-t.x),o=Math.round(e.y-t.y),r=Math.round(Math.sqrt(n*n+o*o)),p.oldDistance=r},p.__twoTouchMove=function(t,e){var n,o,r,a,i,c,u,s=p.oldScale,d=p.oldDistance,h=p.scale,f=p.zoom;p.newScale=(n=s,o=d,r=f,a=t,i=e,c=Math.round(i.x-a.x),u=Math.round(i.y-a.y),n+.001*r*(Math.round(Math.sqrt(c*c+u*u))-o)),p.newScale<=1&&(p.newScale=1),p.newScale>=h&&(p.newScale=h),p.scaleWidth=Math.round(p.newScale*p.baseWidth),p.scaleHeight=Math.round(p.newScale*p.baseHeight);var l=Math.round(p.touchX1-p.scaleWidth/2),g=Math.round(p.touchY1-p.scaleHeight/2);p.outsideBound(l,g),p.updateCanvas()},p.__xtouchEnd=function(){p.oldScale=p.newScale,p.rectX=p.imgLeft,p.rectY=p.imgTop})},C});