UNPKG

create-puzzle

Version:

在浏览器端生成滑块验证码的拼图和背景图。

3 lines (2 loc) 15.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).createPuzzle={})}(this,function(t){"use strict";var e=Object.prototype.toString,n=Math.min,o=Math.max,r=Math.random,i=Math.floor,a=Math.ceil,c="[object ",s=c+"String]",u=c+"Symbol]",h=c+"Blob]";function p(t){return"function"==typeof t||function(t){return null!==t&&"object"==typeof t}(t)}function f(t){return e.call(t)}var l=/^0b[01]+$/i,d=/^0o[0-7]+$/i,y=/^[-+]0x[0-9a-f]+$/i;function v(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||f(t)===u}(t))return NaN;if(p(t)&&(t=Number(t)),"string"!=typeof t)return 0===t?t:+t;t=t.trim();var e=l.test(t);return e||d.test(t)?parseInt(t.slice(2),e?2:8):y.test(t)?NaN:+t}function m(t){return t?(t=v(t))===1/0||t===-1/0?17976931348623157e292*(t<0?-1:1):t==t?t:0:0===t?t:0}function g(t,e){void 0===t&&(t=0),void 0===e&&(e=1),t=m(t),e=m(e);var c=a(n(t,e)||0),s=i(o(t,e)||0);if(c>s){var u=c;c=s,s=u}return i(c+r()*(s-c+1))}var b=function(t,e){return b=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},b(t,e)};var w=function(){return w=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},w.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var P="undefined"!=typeof Blob;function O(t){return!!(P&&t instanceof Blob)||f(t)===h}var x,_="undefined"!=typeof URL,k=Object.keys,I=_?URL.createObjectURL:(x="",function(){return x}),T=_?URL.revokeObjectURL:function(){};var j=[200,304];function L(t,e){return new Promise(function(n,o){var r,i,a,c,s,u,h,p,f,l,d,y,v,m,g,b,P,x,_,I,T,L,C,S,R,E;O(t)?n(t):(r=t,i=w({responseType:"blob"},e),a=i||{},c=a.method,s=void 0===c?"get":c,u=a.data,h=void 0===u?null:u,p=a.timeout,f=a.headers,l=a.withCredentials,d=void 0!==l&&l,y=a.async,v=void 0===y||y,m=a.user,g=void 0===m?null:m,b=a.password,P=void 0===b?null:b,x=a.responseType,_=a.onReadyStateChange,I=a.onLoadStart,T=a.onProgress,L=a.onAbort,C=a.onTimeout,S=a.onError,R=a.onLoad,E=a.onLoadEnd,new Promise(function(t,e){var n=new XMLHttpRequest;n.open(s.toLowerCase(),r,v,g,P),_&&(n.onreadystatechange=_),"number"==typeof p&&p>0&&(n.timeout=p),n.withCredentials=d,x&&(n.responseType=x),"object"==typeof f&&k(f).map(function(t){n.setRequestHeader(t,f[t])});var o,i=function(t){return function(o){e(o),null==t||t.call(n,o)}},a={loadstart:I,progress:T,abort:i(L),timeout:i(C),error:i(S),load:(o=R,function(e){t(e),null==o||o.call(n,e)}),loadend:E};k(a).forEach(function(t){var e=a[t];e&&n.addEventListener(t,e)}),n.send(h)})).then(function(e){var r=e.target.status;if(-1!==j.indexOf(r))n(e.target.response);else{var i=new Error("The file does not support get requests, responseStatus ".concat(r,", '").concat(t,"'."));console.error(i),o(i)}}).catch(function(t){console.error(new Error("Failed to request file. ".concat(t))),o(t)})})}function C(t,e){return L(t,e).then(function(t){return function(t){return new Promise(function(e,n){var o=O(t),r=o?I(t):t,i=new Image;o||(i.crossOrigin="anonymous"),i.onload=function(){e(i)},i.onerror=function(e){o&&T(r),console.error("[loadImage] The image load failed, '".concat(t,"'.")),n(e)},i.src=r})}(t).then(function(e){return{blob:t,image:e}})})}var S=function(){function t(){this.handlers={}}return t.prototype.eventNames=function(){var t,e=(null===(t=Object.getOwnPropertySymbols)||void 0===t?void 0:t.call(Object,this.handlers))||[];return Object.keys(this.handlers).concat(e)},t.prototype.rawListeners=function(t){var e=this.handlers[t];return e?e.map(function(t){return t.raw}):[]},t.prototype.listeners=function(t){var e=this.handlers[t];return e?e.map(function(t){return t.wrap}):[]},t.prototype.hasListener=function(t,e){return this.rawListeners(t).some(function(t){return t===e})},t.prototype._on=function(t,e,n,o,r){void 0===o&&(o=null),void 0===r&&(r=1);var i={raw:e,wrap:n,context:o};if(this.handlers[t]){var a=1===r?"push":"unshift";this.handlers[t][a](i)}else this.handlers[t]=[i];return this},t.prototype.prependListener=function(t,e,n){return this._on(t,e,e,n,0)},t.prototype.on=function(t,e,n){return this._on(t,e,e,n)},t.prototype._wrapOnce=function(t,e,n){var o=this;void 0===n&&(n=null);var r=function(){for(var i=[],a=0;a<arguments.length;a++)i[a]=arguments[a];e.apply(n,i),o.off(t,r)};return r},t.prototype.once=function(t,e,n){var o=this._wrapOnce(t,e,n);return this._on(t,e,o,n)},t.prototype.prependOnceListener=function(t,e,n){var o=this._wrapOnce(t,e,n);return this._on(t,e,o,n,0)},t.prototype.off=function(t,e){var n=this.handlers[t];if(n)if(e){var o=n.findIndex(function(t){return t.wrap===e||t.raw===e});-1!==o&&n.splice(o,1)}else delete this.handlers[t];return this},t.prototype.offAll=function(){return this.handlers={},this},t.prototype.emit=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var o=this.handlers[t];return!!(o&&o.length>0)&&(o.forEach(function(t){t.wrap.apply(t.context,e)}),!0)},t}(),R={},E=function(){function t(t){void 0===t&&(t="default"),this.scope=t,R[this.scope]||(R[this.scope]={}),this.data=R[this.scope]}return t.prototype.getItem=function(t){return t in this.data?this.data[t]:null},t.prototype.setItem=function(t,e){this.data[t]=e},t.prototype.removeItem=function(t){delete this.data[t]},t.prototype.clear=function(){R[this.scope]={},this.data=R[this.scope]},t}();function N(t){try{var e="object"==typeof t&&null!==t&&!!t.setItem&&!!t.getItem&&!!t.removeItem;if(e){var n=Math.random().toString(16).substring(2,8)+(new Date).getTime();if(t.setItem(n,"1"),"1"!==t.getItem(n))return!1;t.removeItem(n)}return e}catch(e){return console.error("[cache2] ".concat(t," is not supported. The default memory cache will be used.")),!1}}var U,V=function(){function t(t,e){void 0===e&&(e={});var n=!!t&&N(t);this.options=w({needParsed:n,prefix:""},e),this.storage=n?t:new E(this.options.memoryScope)}return t.prototype.getKey=function(t){return this.options.prefix+t},t.prototype.get=function(t){var e=this.storage.getItem(this.getKey(t));return this.options.needParsed?function(t,e){try{return JSON.parse(t,e)}catch(e){return t}}(e,this.options.reviver):e},t.prototype.set=function(t,e){this.storage.setItem(this.getKey(t),this.options.needParsed?function(t,e){return JSON.stringify(t,e)}(e,this.options.replacer):e)},t.prototype.del=function(t){this.storage.removeItem(this.getKey(t))},t.prototype.clear=function(){"function"==typeof this.storage.clear&&this.storage.clear()},t}(),M="default",K=function(t){function e(e,n){var o,r=t.call(this)||this,i=M;return"string"==typeof e?i=e||M:"object"==typeof e&&(o=e),o||"object"!=typeof n||(o=n),r.options=w({max:-1,stdTTL:0,maxStrategy:"limited",checkperiod:0,prefix:"cache2_"},o),r.storage=new V(r.options.storage,w({memoryScope:i},r.options)),r.cacheKey=i,r.startCheckperiod(),r}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}b(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}(e,t),e.prototype._check=function(t,e){var n=!0;return 0!==e.t&&e.t<Date.now()&&(n=!1,this.del(t),this.emit("expired",t,e.v)),n},e.prototype._wrap=function(t,e){var n=Date.now(),o="number"==typeof e?e:this.options.stdTTL;return{v:t,t:o>0?n+o:0,n:n}},e.prototype._isLimited=function(t){return this.options.max>-1&&t>=this.options.max},e.prototype._getReplaceKey=function(t,e){var n=t[0];return t.forEach(function(t){(e[t].t<e[n].t||e[t].t===e[n].t&&e[t].n<e[n].n)&&(n=t)}),n},Object.defineProperty(e.prototype,"cacheValues",{get:function(){return this.storage.get(this.cacheKey)||{}},enumerable:!1,configurable:!0}),e.prototype.setCacheValues=function(t){this.storage.set(this.cacheKey,t)},e.prototype.get=function(t){var e=this.cacheValues[t];if(e&&this._check(t,e))return e.v},e.prototype.mget=function(t){var e=this,n={};if(!Array.isArray(t))return n;var o=this.cacheValues;return t.forEach(function(t){var r=o[t];r&&e._check(t,r)&&(n[t]=r.v)}),n},e.prototype.getAll=function(){var t=Object.keys(this.cacheValues);return this.mget(t)},e.prototype.set=function(t,e,n){if(0===this.options.max)return!1;var o=this.cacheValues,r=Object.keys(o);if(!o[t]&&this._isLimited(r.length)){var i=this.keys();if(this._isLimited(i.length)){if("replaced"!==this.options.maxStrategy)return!1;var a=this._getReplaceKey(i,o);this.del(a)}}return o[t]=this._wrap(e,n),this.setCacheValues(o),this.emit("set",t,o[t].v),!0},e.prototype.mset=function(t){var e=this,n=!0;return t.forEach(function(t){var o=e.set(t.key,t.value,t.ttl);n&&!o&&(n=!1)}),n},e.prototype.del=function(t){var e=this,n=this.cacheValues,o=0;return(Array.isArray(t)?t:[t]).forEach(function(t){if(n[t]){o++;var r=n[t];delete n[t],e.emit("del",t,r.v)}}),o>0&&this.setCacheValues(n),o},e.prototype.clear=function(){this.storage.del(this.cacheKey)},e.prototype.keys=function(){var t=this,e=this.cacheValues;return Object.keys(e).filter(function(n){return t._check(n,e[n])})},e.prototype.has=function(t){var e=this.cacheValues[t];return!(!e||!this._check(t,e))},e.prototype.take=function(t){var e,n=this.cacheValues[t];return n&&this._check(t,n)&&(e=n.v,this.del(t)),e},e.prototype.ttl=function(t,e){var n=this.cacheValues,o=n[t];return!(!o||!this._check(t,o))&&(n[t]=this._wrap(o.v,e),!0)},e.prototype.getTtl=function(t){var e=this.cacheValues,n=e[t];if(n&&this._check(t,n))return e[t].t},e.prototype.getLastModified=function(t){var e=this.cacheValues,n=e[t];if(n&&this._check(t,n))return e[t].n},e.prototype.startCheckperiod=function(){var t=this;this.keys(),this.options.checkperiod>0&&(clearTimeout(this._checkTimeout),this._checkTimeout=setTimeout(function(){t.startCheckperiod()},this.options.checkperiod))},e.prototype.stopCheckperiod=function(){clearTimeout(this._checkTimeout)},e}(S),z=function(){function t(t,e){void 0===e&&(e="uh_async_memo"),this.promiseCache={},this.cache=new K(e,t)}return t.prototype.run=function(t,e,n){var o=this;if(!e||!function(t){return"string"==typeof t||f(t)===s}(e))return t();var r=w({persisted:!0},n);if(r.persisted){var i=this.cache.get(e);if(i)return Promise.resolve(i)}return this.promiseCache[e]||(this.promiseCache[e]=t().then(function(t){return delete o.promiseCache[e],o.cache.set(e,t,r.ttl),t}).catch(function(t){return delete o.promiseCache[e],Promise.reject(t)})),this.promiseCache[e]},t}(),A=Math.PI;t.Point=void 0,(U=t.Point||(t.Point={}))[U.None=0]="None",U[U.Outer=1]="Outer",U[U.Inner=2]="Inner";var D=[t.Point.None,t.Point.Outer,t.Point.Inner];function W(t){return t[g(0,t.length-1)]}function q(e){var n={top:W(D),right:W(D),bottom:W(D),left:W(D)},o=Object.keys(n),r=["top","bottom"],i=["left","right"];if(n.top===t.Point.Outer&&n.bottom===t.Point.Outer?n[W(r)]=t.Point.Inner:n.top!==t.Point.Outer&&n.bottom!==t.Point.Outer&&(n[W(r)]=t.Point.Outer),n.left===t.Point.Outer&&n.right===t.Point.Outer?n[W(i)]=t.Point.Inner:n.left!==t.Point.Outer&&n.right!==t.Point.Outer&&(n[W(i)]=t.Point.Outer),e){var a=[],c=[];o.forEach(function(e){n[e]===t.Point.Inner?a.push(e):n[e]===t.Point.None&&c.push(e)}),2===e?a.forEach(function(e){return n[e]=t.Point.None}):3===e?0===a.length?n[W(c)]=t.Point.Inner:2===a.length&&(n[W(a)]=t.Point.None):4==e&&c.forEach(function(e){return n[e]=t.Point.Inner})}return n}function B(e,n){void 0===n&&(n={});var o=n.x,r=void 0===o?0:o,i=n.y,a=void 0===i?0:i,c=n.w,s=void 0===c?60:c,u=n.h,h=void 0===u?60:u,p=n.needClosePath,f=void 0===p||p,l=n.points,d=n.margin,y=void 0===d?0:d;y=y<=0?0:y,"number"!=typeof l&&l||(l=q(l));var v=.15*(Math.min(s,h)-2*y),m=Math.hypot(v,v)/2,g=v+m,b={x:r+y,y:a+y,w:s-g-2*y,h:h-g-2*y},w=b.w/2,P=b.h/2;l.left===t.Point.Outer&&(b.x+=g),l.top===t.Point.Outer&&(b.y+=g),e.beginPath(),e.lineWidth=2,e.moveTo(b.x,b.y),l.top!==t.Point.None&&(e.lineTo(b.x+w-m,b.y),l.top===t.Point.Inner?e.arc(b.x+w,b.y+m,v,1.25*A,1.75*A,!0):e.arc(b.x+w,b.y-m,v,.75*A,.25*A)),e.lineTo(b.x+b.w,b.y),l.right!==t.Point.None&&(e.lineTo(b.x+b.w,b.y+P-m),l.right===t.Point.Inner?e.arc(b.x+b.w-m,b.y+P,v,1.75*A,.25*A,!0):e.arc(b.x+b.w+m,b.y+P,v,1.25*A,.75*A)),e.lineTo(b.x+b.w,b.y+b.h),l.bottom!==t.Point.None&&(e.lineTo(b.x+w+m,b.y+b.h),l.bottom===t.Point.Inner?e.arc(b.x+w,b.y+b.h-m,v,.25*A,.75*A,!0):e.arc(b.x+w,b.y+b.h+m,v,1.75*A,1.25*A)),e.lineTo(b.x,b.y+b.h),l.left!==t.Point.None&&(e.lineTo(b.x,b.y+P+m),l.left===t.Point.Inner?e.arc(b.x+m,b.y+P,v,.75*A,1.25*A,!0):e.arc(b.x-m,b.y+P,v,.25*A,1.75*A)),e.lineTo(b.x,b.y),e.stroke(),f&&e.closePath()}function H(t,e,n,o){return new Promise(function(r){e?t.toBlob(function(e){r(e?URL.createObjectURL(e):t.toDataURL(n,o))},n,o):r(t.toDataURL(n,o))})}var $=new z({max:5,maxStrategy:"replaced"});$.cache.on("del",function(t,e){try{e.image.src&&URL.revokeObjectURL(e.image.src)}catch(t){}});var J=new WeakMap,F="image/jpeg",X="image/png",G=[];function Q(t,e){void 0===e&&(e={});var n=e.borderWidth,o=void 0===n?2:n,r=e.borderColor,i=void 0===r?"rgba(255,255,255,0.7)":r,a=e.fillColor,c=void 0===a?"rgba(255,255,255,0.7)":a,s=e.points,u=e.width,h=void 0===u?60:u,f=e.height,l=void 0===f?60:f,d=e.x,y=e.y,v=e.margin,m=void 0===v?2:v,b=e.equalHeight,w=void 0===b||b,P=e.imageWidth,O=e.imageHeight,x=e.bgWidth,_=e.bgHeight,k=e.bgOffset,I=void 0===k?[0,0]:k,T=e.bgImageType,j=void 0===T?F:T,L=e.quality,S=void 0===L?.8:L,R=e.format,E=void 0===R?"dataURL":R,N=e.cacheImage,U=void 0===N||N,V=e.autoRevokePreviousBlobUrl,M=void 0===V||V,K=e.ajaxOptions;return new Promise(function(e,n){var r=document.createElement("canvas"),a=document.createElement("canvas"),u=r.getContext("2d"),f=a.getContext("2d"),v=U?function(t){if(!p(t))return String(t);if(!J.get(t)){var e="_"+Date.now();J.set(t,e)}return J.get(t)}(t):void 0;$.run(function(){return C(t,K)},v).then(function(t){var p=t.image;P&&(p.width=P),O&&(p.height=O);var v="number"==typeof x&&x>0?x>h?x:h:p.width,b="number"==typeof _&&_>0?_>l?_:l:p.height;r.width=v,r.height=b;var k=v-h,T=b-l,L=void 0===d?g(h,k):d||0,C=void 0===y?g(0,T):y||0;L<0?L=0:L>k&&(L=k),C<0?C=0:C>T&&(C=T);var R="number"!=typeof s&&s?s:q(s),N="function"==typeof I?I(p.width,p.height):I;u.strokeStyle=i,u.lineWidth=o,u.fillStyle=c,B(u,{x:L,y:C,w:h,h:l,points:R,margin:m}),u.fillStyle=c,u.fill(),u.globalCompositeOperation="destination-over",u.drawImage(p,N[0],N[1],p.width,p.height),a.width=v,a.height=b,f.strokeStyle=i,f.lineWidth=o,B(f,{x:L,y:C,w:h,h:l,points:R,margin:m}),f.globalCompositeOperation="destination-over",f.clip(),f.drawImage(p,N[0],N[1],p.width,p.height);var U=f.getImageData(L,C,h,l);f.clearRect(0,0,v,b),a.width=h,a.height=w?b:l,f.putImageData(U,0,w?C:0);var V="blob"===E,K=H(a,V,X,S),z=H(r,V,j,S);Promise.all([K,z]).then(function(t){var n=t[0],o=t[1];M&&(G.length&&(G.forEach(function(t){URL.revokeObjectURL(t)}),G.length=0),V&&G.push(o,n)),e({puzzleUrl:n,bgUrl:o,x:L,y:w?0:C})}).catch(n)}).catch(n)})}t.clearCache=function(t){t?$.cache.del(t):$.cache.clear()},t.createPuzzle=Q,t.default=Q,t.drawPuzzle=B,t.getRandomPoints=q,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=createPuzzle.min.js.map