You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
6 lines
12 KiB
6 lines
12 KiB
/**
|
|
* we-cropper v1.3.7
|
|
* (c) 2019 dlhandsome
|
|
* @license MIT
|
|
*/
|
|
!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,p=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))})}}var g=d(wx.getImageInfo),v=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 y(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),p="",g=0;g<e;g++){var v=g<<2;p+=f(u[l+v+2])+f(u[l+v+1])+f(u[l+v])}for(var y=0;y<c;y++)p+=String.fromCharCode(0);s+=p}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 m=function(t,e){return void 0===t&&(t={}),void 0===e&&(e=function(){}),y(t.canvasId,t.x,t.y,t.width,t.height,"bmp",e)};var b={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 w=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 w.prototype.init=function(){var t=this,e=t.src;return t.version="1.3.7","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(w.prototype,b),w.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}})},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},w.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}},w.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.pushOrign=function(t){return a.src=t,l(a.onBeforeImageLoad)&&a.onBeforeImageLoad(a.ctx,a),g({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.getCropperBase64=function(t){void 0===t&&(t=function(){}),m({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 p(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},e=a.targetCtx,void 0===n&&(n=!1),new Promise(function(t){e.draw(n,t)});var e,n}),r().then(function(){p(e)&&(o=Object.assign({},o,e)),l(e)&&(n=e);var t=o.componentContext?[o,o.componentContext]:[o];return v.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)})}},w.prototype.cutt=function(){var a=this,i=a.width,c=a.height,t=a.cut,u=t.x;void 0===u&&(u=0);var s=t.y;void 0===s&&(s=0);var d=t.width;void 0===d&&(d=i);var h=t.height;void 0===h&&(h=c),a.outsideBound=function(t,e){a.imgLeft=u<=t?u:a.scaleWidth+t-u<=d?u+d-a.scaleWidth:t,a.imgTop=s<=e?s:a.scaleHeight+e-s<=h?s+h-a.scaleHeight:e},a.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=[{start:{x:u-o,y:s+10-o},step1:{x:u-o,y:s-o},step2:{x:u+10-o,y:s-o}},{start:{x:u-o,y:s+h-10+o},step1:{x:u-o,y:s+h+o},step2:{x:u+10-o,y:s+h+o}},{start:{x:u+d-10+o,y:s-o},step1:{x:u+d+o,y:s-o},step2:{x:u+d+o,y:s+10-o}},{start:{x:u+d+o,y:s+h-10+o},step1:{x:u+d+o,y:s+h+o},step2:{x:u+d-10+o,y:s+h+o}}];a.ctx.beginPath(),a.ctx.setFillStyle(n),a.ctx.fillRect(0,0,u,c),a.ctx.fillRect(u,0,d,s),a.ctx.fillRect(u,s+h,d,c-s-h),a.ctx.fillRect(u+d,0,i-u-d,c),a.ctx.fill(),r.forEach(function(t){a.ctx.beginPath(),a.ctx.setStrokeStyle(e),a.ctx.setLineWidth(o),a.ctx.moveTo(t.start.x,t.start.y),a.ctx.lineTo(t.step1.x,t.step1.y),a.ctx.lineTo(t.step2.x,t.step2.y),a.ctx.stroke()})}},w.prototype.update=function(){var g=this;g.src&&(g.__oneTouchStart=function(t){g.touchX0=Math.round(t.x),g.touchY0=Math.round(t.y)},g.__oneTouchMove=function(t){var e,n;if(g.touchended)return g.updateCanvas();e=Math.round(t.x-g.touchX0),n=Math.round(t.y-g.touchY0);var o=Math.round(g.rectX+e),r=Math.round(g.rectY+n);g.outsideBound(o,r),g.updateCanvas()},g.__twoTouchStart=function(t,e){var n,o,r;g.touchX1=Math.round(g.rectX+g.scaleWidth/2),g.touchY1=Math.round(g.rectY+g.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)),g.oldDistance=r},g.__twoTouchMove=function(t,e){var n,o,r,a,i,c,u,s=g.oldScale,d=g.oldDistance,h=g.scale,f=g.zoom;g.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)),g.newScale<=1&&(g.newScale=1),g.newScale>=h&&(g.newScale=h),g.scaleWidth=Math.round(g.newScale*g.baseWidth),g.scaleHeight=Math.round(g.newScale*g.baseHeight);var l=Math.round(g.touchX1-g.scaleWidth/2),p=Math.round(g.touchY1-g.scaleHeight/2);g.outsideBound(l,p),g.updateCanvas()},g.__xtouchEnd=function(){g.oldScale=g.newScale,g.rectX=g.imgLeft,g.rectY=g.imgTop})},w}); |