lucky-scratch 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1,16 +1,1505 @@
1
- /*! *****************************************************************************
2
- Copyright (c) Microsoft Corporation.
3
-
4
- Permission to use, copy, modify, and/or distribute this software for any
5
- purpose with or without fee is hereby granted.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
- PERFORMANCE OF THIS SOFTWARE.
1
+
2
+ (function(l, r) { if (!l || l.getElementById('livereloadscript')) return; r = l.createElement('script'); r.async = 1; r.src = '//' + (self.location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1'; r.id = 'livereloadscript'; l.getElementsByTagName('head')[0].appendChild(r) })(self.document);
3
+ /******************************************************************************
4
+ Copyright (c) Microsoft Corporation.
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
14
16
  ***************************************************************************** */
15
- var t=function(e,n){return t=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])},t(e,n)};var e=function(){return e=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},e.apply(this,arguments)};function n(t,e,n,i){return new(n||(n=Promise))(function(r,o){function s(t){try{c(i.next(t))}catch(t){o(t)}}function a(t){try{c(i.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(s,a)}c((i=i.apply(t,e||[])).next())})}function i(t,e){var n,i,r,o,s={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,i&&(r=2&o[0]?i.return:o[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,o[1])).done)return r;switch(i=0,r&&(o=[2&o[0],r.value]),o[0]){case 0:case 1:r=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,i=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!(r=s.trys,(r=r.length>0&&r[r.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!r||o[1]>r[0]&&o[1]<r[3])){s.label=o[1];break}if(6===o[0]&&s.label<r[1]){s.label=r[1],r=o;break}if(r&&s.label<r[2]){s.label=r[2],s.ops.push(o);break}r[2]&&s.ops.pop(),s.trys.pop();continue}o=e.call(t,s)}catch(t){o=[6,t],i=0}finally{n=r=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}}function r(t,e){for(var n=0,i=e.length,r=t.length;n<i;n++,r++)t[r]=e[n];return t}Array.prototype.includes||Object.defineProperty(Array.prototype,"includes",{value:function(t,e){if(null==this)throw new TypeError('"this" is null or not defined');var n=Object(this),i=n.length>>>0;if(0===i)return!1;var r=0|e,o=Math.max(r>=0?r:i-Math.abs(r),0);function s(t,e){return t===e||"number"==typeof t&&"number"==typeof e&&isNaN(t)&&isNaN(e)}for(;o<i;){if(s(n[o],t))return!0;o++}return!1}}),String.prototype.includes||(String.prototype.includes=function(t,e){return"number"!=typeof e&&(e=0),!(e+t.length>this.length)&&-1!==this.indexOf(t,e)}),Array.prototype.find||Object.defineProperty(Array.prototype,"find",{value:function(t){if(null==this)throw new TypeError('"this" is null or not defined');var e=Object(this),n=e.length>>>0;if("function"!=typeof t)throw new TypeError("predicate must be a function");for(var i=arguments[1],r=0;r<n;){var o=e[r];if(t.call(i,o,r,e))return o;r++}}});var o=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];return e.some(function(e){return Object.prototype.toString.call(t).slice(8,-1).toLowerCase()===e})},s="1.1.0",a=function(){function t(){this.subs=[]}return t.prototype.addSub=function(t){this.subs.includes(t)||this.subs.push(t)},t.prototype.notify=function(){this.subs.forEach(function(t){t.update()})},t}(),c="__proto__"in{};function h(t,e,n,i){Object.defineProperty(t,e,{value:n,enumerable:!!i,writable:!0,configurable:!0})}var l=Array.prototype,u=Object.create(l);["push","pop","shift","unshift","sort","splice","reverse"].forEach(function(t){u[t]=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var i=l[t].apply(this,e),r=this.__luckyOb__;return["push","unshift","splice"].includes(t)&&r.walk(this),r.dep.notify(),i}});var f=function(){function t(t){this.dep=new a,h(t,"__luckyOb__",this),Array.isArray(t)&&(c?t.__proto__=u:Object.getOwnPropertyNames(u).forEach(function(e){h(t,e,u[e])})),this.walk(t)}return t.prototype.walk=function(t){Object.keys(t).forEach(function(e){p(t,e,t[e])})},t}();function d(t){if(t&&"object"==typeof t)return"__luckyOb__"in t?t.__luckyOb__:new f(t)}function p(t,e,n){var i=new a,r=Object.getOwnPropertyDescriptor(t,e);if(!r||!1!==r.configurable){var o=r&&r.get,s=r&&r.set;o&&!s||2!==arguments.length||(n=t[e]);var c=d(n);Object.defineProperty(t,e,{get:function(){var e=o?o.call(t):n;return a.target&&(i.addSub(a.target),c&&c.dep.addSub(a.target)),e},set:function(e){e!==n&&(n=e,o&&!s||(s?s.call(t,e):n=e,c=d(e),i.notify()))}})}}var v=0,g=function(){function t(t,e,n,i){void 0===i&&(i={}),this.id=v++,this.$lucky=t,this.expr=e,this.deep=!!i.deep,this.getter="function"==typeof e?e:function(t){t+=".";for(var e=[],n="",i=0;i<t.length;i++){var r=t[i];if(/\[|\./.test(r))e.push(n),n="";else{if(/\W/.test(r))continue;n+=r}}return function(t){return e.reduce(function(t,e){return t[e]},t)}}(e),this.cb=n,this.value=this.get()}return t.prototype.get=function(){a.target=this;var t=this.getter.call(this.$lucky,this.$lucky);return this.deep&&function(t){var e=function(t){o(t,"array","object")&&Object.keys(t).forEach(function(n){var i=t[n];e(i)})};e(t)}(t),a.target=null,t},t.prototype.update=function(){var t=this.get(),e=this.value;this.value=t,this.cb.call(this.$lucky,t,e)},t}(),y=function(r){function o(t,e){var n=r.call(this,t,{width:e.width||"300px",height:e.height||"150px"})||this;return n.mask={},n.scratch={radius:20,percent:.5},n.progress=0,n.isScratching=!1,n.isCompleted=!1,n.disabled=!1,n.isFirstScratch=!0,n.eventsInitialized=!1,n.initData(e),n.init(),n}return function(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function i(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}(o,r),o.prototype.initData=function(t){this.$set(this,"mask",e({type:"color",color:"#ccc"},t.mask)),this.$set(this,"scratch",e({radius:20,percent:.5},t.scratch)),this.$set(this,"onceBeforeStartCallback",t.onceBeforeStart),this.$set(this,"beforeStartCallback",t.beforeStart),this.$set(this,"startCallback",t.start),this.$set(this,"endCallback",t.end),this.$set(this,"successCallback",t.success),this.$set(this,"afterInitCallback",t.afterInit)},o.prototype.resize=function(){var t,e;r.prototype.resize.call(this),this.draw(),null===(e=(t=this.config).afterResize)||void 0===e||e.call(t)},o.prototype.init=function(){var t;return n(this,void 0,void 0,function(){return i(this,function(e){switch(e.label){case 0:return this.initLucky(),this.progress=0,this.isScratching=!1,this.isCompleted=!1,this.isFirstScratch=!0,[4,this.draw()];case 1:return e.sent(),this.handleBindEvents(),null===(t=this.afterInitCallback)||void 0===t||t.call(this),[2]}})})},o.prototype.draw=function(){var t,e,r,o;return n(this,void 0,void 0,function(){var n,s,a;return i(this,function(i){switch(i.label){case 0:return null===(e=(t=this.config).beforeInit)||void 0===e||e.call(t),this.clearCanvas(),s=(n=this).mask,n.scratch,"image"===s.type&&s.src?[4,this.loadImg(s.src,{src:s.src})]:[3,2];case 1:return a=i.sent(),this.ctx.drawImage(a,0,0,this.boxWidth,this.boxHeight),[3,3];case 2:this.ctx.fillStyle=s.color||"#ccc",this.ctx.fillRect(0,0,this.boxWidth,this.boxHeight),i.label=3;case 3:return null===(o=(r=this.config).afterInit)||void 0===o||o.call(r),[2]}})})},o.prototype.handleBindEvents=function(){var t=this;if(!this.eventsInitialized){this.eventsInitialized=!0;var e=this.config.canvasElement;e&&(e.addEventListener("touchstart",function(e){t.handleStart(e.touches[0])}),e.addEventListener("touchmove",function(e){e.preventDefault(),t.handleMove(e.touches[0])}),e.addEventListener("touchend",function(){t.handleEnd()}),e.addEventListener("mousedown",function(e){t.handleStart(e)}),e.addEventListener("mousemove",function(e){e.preventDefault(),t.handleMove(e)}),document.addEventListener("mouseup",function(){t.handleEnd()}))}},o.prototype.handleStart=function(t){var e;return n(this,void 0,void 0,function(){var n,r;return i(this,function(i){switch(i.label){case 0:if(this.isCompleted||this.disabled)return[2];if(!this.isFirstScratch||!this.onceBeforeStartCallback)return[3,5];i.label=1;case 1:return i.trys.push([1,3,,4]),[4,this.onceBeforeStartCallback()];case 2:return!1===i.sent()?[2]:[3,4];case 3:return n=i.sent(),console.error("onceBeforeStart 回调执行出错:",n),[2];case 4:this.isFirstScratch=!1,i.label=5;case 5:if(!this.beforeStartCallback)return[3,9];i.label=6;case 6:return i.trys.push([6,8,,9]),[4,this.beforeStartCallback()];case 7:return!1===i.sent()?[2]:[3,9];case 8:return r=i.sent(),console.error("beforeStart 回调执行出错:",r),[2];case 9:return this.isScratching=!0,null===(e=this.startCallback)||void 0===e||e.call(this),this.drawArc(t),[2]}})})},o.prototype.handleMove=function(t){!this.isScratching||this.isCompleted||this.disabled||this.drawArc(t)},o.prototype.handleEnd=function(){var t;!this.isScratching||this.isCompleted||this.disabled||(this.isScratching=!1,this.checkProgress(),null===(t=this.endCallback)||void 0===t||t.call(this))},o.prototype.drawArc=function(t){var e=this.config.canvasElement;if(e){var n=e.getBoundingClientRect(),i=(t.clientX-n.left)*this.config.dpr,r=(t.clientY-n.top)*this.config.dpr;this.drawArcAt(i,r)}},o.prototype.drawArcAt=function(t,e){this.ctx.globalCompositeOperation="destination-out",this.ctx.beginPath(),this.ctx.arc(t,e,this.scratch.radius*this.config.dpr,0,2*Math.PI),this.ctx.fill(),this.ctx.globalCompositeOperation="source-over"},o.prototype.checkProgress=function(){var t;try{for(var e=this.ctx.getImageData(0,0,this.boxWidth*this.config.dpr,this.boxHeight*this.config.dpr).data,n=0,i=0;i<e.length;i+=4)e[i+3]<128&&n++;this.progress=n/(e.length/4),this.progress>=this.scratch.percent&&(this.isCompleted=!0,this.clearCanvas(),null===(t=this.successCallback)||void 0===t||t.call(this,this.progress))}catch(t){console.error("无法计算刮开进度,可能是因为图片跨域:",t)}},o.prototype.setDisabled=function(t){this.disabled=t},o.prototype.handleTouchStart=function(t,e){var r;return n(this,void 0,void 0,function(){var n,o;return i(this,function(i){switch(i.label){case 0:if(this.isCompleted||this.disabled)return[2];if(!this.isFirstScratch||!this.onceBeforeStartCallback)return[3,5];i.label=1;case 1:return i.trys.push([1,3,,4]),[4,this.onceBeforeStartCallback()];case 2:return!1===i.sent()?[2]:[3,4];case 3:return n=i.sent(),console.error("onceBeforeStart 回调执行出错:",n),[2];case 4:this.isFirstScratch=!1,i.label=5;case 5:if(!this.beforeStartCallback)return[3,9];i.label=6;case 6:return i.trys.push([6,8,,9]),[4,this.beforeStartCallback()];case 7:return!1===i.sent()?[2]:[3,9];case 8:return o=i.sent(),console.error("beforeStart 回调执行出错:",o),[2];case 9:return this.isScratching=!0,null===(r=this.startCallback)||void 0===r||r.call(this),this.drawArcAt(t*this.config.dpr,e*this.config.dpr),[2]}})})},o.prototype.handleTouchMove=function(t,e){!this.isScratching||this.isCompleted||this.disabled||this.drawArcAt(t*this.config.dpr,e*this.config.dpr)},o.prototype.handleTouchEnd=function(){var t;!this.isScratching||this.isCompleted||this.disabled||(this.isScratching=!1,this.checkProgress(),null===(t=this.endCallback)||void 0===t||t.call(this))},o}(function(){function t(t,e){var n=this;this.version=s,this.htmlFontSize=16,this.rAF=function(){},this.boxWidth=0,this.boxHeight=0,"string"==typeof t?t={el:t}:1===t.nodeType&&(t={el:"",divElement:t}),this.config=t,this.data=e,t.flag||(t.flag="WEB"),t.el&&(t.divElement=document.querySelector(t.el)),t.divElement&&(t.canvasElement=document.createElement("canvas"),t.divElement.appendChild(t.canvasElement)),t.canvasElement&&(t.ctx=t.canvasElement.getContext("2d"),t.canvasElement.setAttribute("package","".concat("lucky-scratch","@").concat(s)),t.canvasElement.addEventListener("click",function(t){return n.handleClick(t)})),this.ctx=t.ctx,this.initWindowFunction(),this.config.ctx||console.error("无法获取到 CanvasContext2D"),window&&"function"==typeof window.addEventListener&&window.addEventListener("resize",function(t,e){void 0===e&&(e=300);var n=null;return function(){for(var i=this,r=[],o=0;o<arguments.length;o++)r[o]=arguments[o];n||(n=setTimeout(function(){t.apply(i,r),clearTimeout(n),n=null},e))}}(function(){return n.resize()},300)),window&&window.document&&"function"==typeof window.MutationObserver&&"WEB"===this.config.flag&&new window.MutationObserver(function(){n.resize()}).observe(document.documentElement,{attributes:!0})}return t.prototype.resize=function(){var t,e;null===(e=(t=this.config).beforeResize)||void 0===e||e.call(t),this.setHTMLFontSize(),this.setDpr(),this.resetWidthAndHeight(),this.zoomCanvas()},t.prototype.initLucky=function(){if(this.resize(),!this.boxWidth||!this.boxHeight)return console.error("无法获取到宽度或高度")},t.prototype.handleClick=function(t){},t.prototype.setHTMLFontSize=function(){window&&window.getComputedStyle&&(this.htmlFontSize=+window.getComputedStyle(document.documentElement).fontSize.slice(0,-2))},t.prototype.clearCanvas=function(){var t=[this.boxWidth,this.boxHeight],e=t[0],n=t[1];this.ctx.clearRect(-e,-n,2*e,2*n)},t.prototype.setDpr=function(){var t=this.config;t.dpr||(window?window.dpr=t.dpr=window.devicePixelRatio||1:t.dpr||console.error(t,"未传入 dpr 可能会导致绘制异常"))},t.prototype.resetWidthAndHeight=function(){var t=this.config,e=this.data,n=0,i=0;t.divElement&&(n=t.divElement.offsetWidth,i=t.divElement.offsetHeight),this.boxWidth=this.getLength(e.width||t.width)||n,this.boxHeight=this.getLength(e.height||t.height)||i,t.divElement&&(t.divElement.style.overflow="hidden",t.divElement.style.width=this.boxWidth+"px",t.divElement.style.height=this.boxHeight+"px")},t.prototype.zoomCanvas=function(){var t=this.config,e=this.ctx,n=t.canvasElement,i=t.dpr,r=[this.boxWidth*i,this.boxHeight*i],o=r[0],s=r[1];n&&(n.width=o,n.height=s,n.style.width="".concat(o,"px"),n.style.height="".concat(s,"px"),n.style["transform-origin"]="left top",n.style.transform="scale(".concat(1/i,")"),e.scale(i,i))},t.prototype.initWindowFunction=function(){var t=this.config;if(window)return this.rAF=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(t){window.setTimeout(t,1e3/60)},t.setTimeout=window.setTimeout,t.setInterval=window.setInterval,t.clearTimeout=window.clearTimeout,void(t.clearInterval=window.clearInterval);if(t.rAF)this.rAF=t.rAF;else if(t.setTimeout){var e=t.setTimeout;this.rAF=function(t){return e(t,16.7)}}else this.rAF=function(t){return setTimeout(t,16.7)}},t.prototype.isWeb=function(){return["WEB","UNI-H5","TARO-H5"].includes(this.config.flag)},t.prototype.loadImg=function(t,e,n){var i=this;return void 0===n&&(n="$resolve"),new Promise(function(r,o){if(t||o("=> '".concat(e.src,"' 不能为空或不合法")),"WEB"!==i.config.flag)return e[n]=r,void(e.$reject=o);var s=new Image;s.crossorigin="anonymous",s.onload=function(){return r(s)},s.onerror=function(){return o("=> '".concat(e.src,"' 图片加载失败"))},s.src=t})},t.prototype.drawImage=function(t,e){for(var n,i,o,s=[],a=2;a<arguments.length;a++)s[a-2]=arguments[a];var c=this.config,h=c.flag,l=c.dpr;if(["WEB","MP-WX"].includes(h))o=e;else{if(!["UNI-H5","UNI-MP","TARO-H5","TARO-MP"].includes(h))return console.error("意料之外的 flag, 该平台尚未兼容!");o=e.path}var u=null===(i=(n=o.canvas||o).getContext)||void 0===i?void 0:i.call(n,"2d");if(u&&!this.isWeb()){s=s.map(function(t){return t*l});var f=u.getImageData.apply(u,s.slice(0,4));t.putImageData.apply(t,r([f],s.slice(4,6)))}else{8===s.length&&(s=s.map(function(t,e){return e<4?t*l:t}));try{t.drawImage.apply(t,r([o],s))}catch(t){}}},t.prototype.computedWidthAndHeight=function(t,e,n,i){if(!e.width&&!e.height)return[t.width,t.height];if(e.width&&!e.height){var r=this.getLength(e.width,n);return[r,t.height*(r/t.width)]}if(!e.width&&e.height){var o=this.getLength(e.height,i);return[t.width*(o/t.height),o]}return[this.getLength(e.width,n),this.getLength(e.height,i)]},t.prototype.changeUnits=function(t,e){var n=this;void 0===e&&(e=1);var i=this.config;return Number(t.replace(/^([-]*[0-9.]*)([a-z%]*)$/,function(t,r,o){var s={"%":function(t){return t*(e/100)},px:function(t){return 1*t},rem:function(t){return t*n.htmlFontSize},vw:function(t){return t/100*window.innerWidth}}[o];if(s)return s(r);var a=i.handleCssUnit||i.unitFunc;return a?a(r,o):r}))},t.prototype.getLength=function(t,e){return o(t,"number")?t:o(t,"string")?this.changeUnits(t,e):0},t.prototype.getOffsetX=function(t,e){return void 0===e&&(e=0),(e-t)/2},t.prototype.getOffscreenCanvas=function(t,e){if(!(n=this,i="_offscreenCanvas",Object.prototype.hasOwnProperty.call(n,i)||(window&&window.document&&"WEB"===this.config.flag?this._offscreenCanvas=document.createElement("canvas"):this._offscreenCanvas=this.config.offscreenCanvas,this._offscreenCanvas)))return console.error("离屏 Canvas 无法渲染!");var n,i,r=this.config.dpr,o=this._offscreenCanvas;o.width=(t||300)*r,o.height=(e||150)*r;var s=o.getContext("2d");return s.clearRect(0,0,t,e),s.scale(r,r),s.dpr=r,{_offscreenCanvas:o,_ctx:s}},t.prototype.$set=function(t,e,n){t&&"object"==typeof t&&p(t,e,n)},t.prototype.$computed=function(t,e,n){var i=this;Object.defineProperty(t,e,{get:function(){return n.call(i)}})},t.prototype.$watch=function(t,e,n){void 0===n&&(n={}),"object"==typeof e&&(e=(n=e).handler);var i=new g(this,t,e,n);return n.immediate&&e.call(this,i.value),function(){}},t.version=s,t}()),b=function(t,e){var n=document.createElement("canvas"),i=n.getContext("2d"),r=t.width,o=t.height;return n.width=r,n.height=o,function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var i=e[0],r=e[1],o=e[2],s=e[3],a=e[4],c=Math.min(o,s),h=Math.PI;a>c/2&&(a=c/2),t.beginPath(),t.moveTo(i+a,r),t.lineTo(i+a,r),t.lineTo(i+o-a,r),t.arc(i+o-a,r+a,a,-h/2,0),t.lineTo(i+o,r+s-a),t.arc(i+o-a,r+s-a,a,0,h/2),t.lineTo(i+a,r+s),t.arc(i+a,r+s-a,a,h/2,h),t.lineTo(i,r+a),t.arc(i+a,r+a,a,h,-h/2),t.closePath()}(i,0,0,r,o,e),i.clip(),i.drawImage(t,0,0,r,o),n},w=function(t,e){var n=document.createElement("canvas"),i=n.getContext("2d"),r=t.width,o=t.height;if(n.width=r,n.height=o,"string"==typeof i.filter)i.filter="opacity(".concat(100*e,"%)"),i.drawImage(t,0,0,r,o);else{i.drawImage(t,0,0,r,o);for(var s=i.getImageData(0,0,r,o),a=s.data,c=a.length,h=0;h<c;h+=4){var l=a[h+3];0!==l&&(a[h+3]=l*e)}i.putImageData(s,0,0)}return n};export{y as LuckyScratch,b as cutRound,w as opacity};
17
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
18
+
19
+ var extendStatics = function (d, b) {
20
+ extendStatics = Object.setPrototypeOf || {
21
+ __proto__: []
22
+ } instanceof Array && function (d, b) {
23
+ d.__proto__ = b;
24
+ } || function (d, b) {
25
+ for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
26
+ };
27
+ return extendStatics(d, b);
28
+ };
29
+ function __extends(d, b) {
30
+ if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
31
+ extendStatics(d, b);
32
+ function __() {
33
+ this.constructor = d;
34
+ }
35
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
36
+ }
37
+ var __assign = function () {
38
+ __assign = Object.assign || function __assign(t) {
39
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
40
+ s = arguments[i];
41
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
42
+ }
43
+ return t;
44
+ };
45
+ return __assign.apply(this, arguments);
46
+ };
47
+ function __awaiter(thisArg, _arguments, P, generator) {
48
+ function adopt(value) {
49
+ return value instanceof P ? value : new P(function (resolve) {
50
+ resolve(value);
51
+ });
52
+ }
53
+ return new (P || (P = Promise))(function (resolve, reject) {
54
+ function fulfilled(value) {
55
+ try {
56
+ step(generator.next(value));
57
+ } catch (e) {
58
+ reject(e);
59
+ }
60
+ }
61
+ function rejected(value) {
62
+ try {
63
+ step(generator["throw"](value));
64
+ } catch (e) {
65
+ reject(e);
66
+ }
67
+ }
68
+ function step(result) {
69
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
70
+ }
71
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
72
+ });
73
+ }
74
+ function __generator(thisArg, body) {
75
+ var _ = {
76
+ label: 0,
77
+ sent: function () {
78
+ if (t[0] & 1) throw t[1];
79
+ return t[1];
80
+ },
81
+ trys: [],
82
+ ops: []
83
+ },
84
+ f,
85
+ y,
86
+ t,
87
+ g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
88
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function () {
89
+ return this;
90
+ }), g;
91
+ function verb(n) {
92
+ return function (v) {
93
+ return step([n, v]);
94
+ };
95
+ }
96
+ function step(op) {
97
+ if (f) throw new TypeError("Generator is already executing.");
98
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
99
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
100
+ if (y = 0, t) op = [op[0] & 2, t.value];
101
+ switch (op[0]) {
102
+ case 0:
103
+ case 1:
104
+ t = op;
105
+ break;
106
+ case 4:
107
+ _.label++;
108
+ return {
109
+ value: op[1],
110
+ done: false
111
+ };
112
+ case 5:
113
+ _.label++;
114
+ y = op[1];
115
+ op = [0];
116
+ continue;
117
+ case 7:
118
+ op = _.ops.pop();
119
+ _.trys.pop();
120
+ continue;
121
+ default:
122
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
123
+ _ = 0;
124
+ continue;
125
+ }
126
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
127
+ _.label = op[1];
128
+ break;
129
+ }
130
+ if (op[0] === 6 && _.label < t[1]) {
131
+ _.label = t[1];
132
+ t = op;
133
+ break;
134
+ }
135
+ if (t && _.label < t[2]) {
136
+ _.label = t[2];
137
+ _.ops.push(op);
138
+ break;
139
+ }
140
+ if (t[2]) _.ops.pop();
141
+ _.trys.pop();
142
+ continue;
143
+ }
144
+ op = body.call(thisArg, _);
145
+ } catch (e) {
146
+ op = [6, e];
147
+ y = 0;
148
+ } finally {
149
+ f = t = 0;
150
+ }
151
+ if (op[0] & 5) throw op[1];
152
+ return {
153
+ value: op[0] ? op[1] : void 0,
154
+ done: true
155
+ };
156
+ }
157
+ }
158
+ function __spreadArray(to, from, pack) {
159
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
160
+ if (ar || !(i in from)) {
161
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
162
+ ar[i] = from[i];
163
+ }
164
+ }
165
+ return to.concat(ar || Array.prototype.slice.call(from));
166
+ }
167
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
168
+ var e = new Error(message);
169
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
170
+ };
171
+
172
+ /**
173
+ * 由于部分低版本下的某些 app 可能会缺少某些原型方法, 这里增加兼容
174
+ */
175
+
176
+ // ie11 不兼容 includes 方法
177
+ if (!Array.prototype.includes) {
178
+ Object.defineProperty(Array.prototype, 'includes', {
179
+ value: function value(valueToFind, fromIndex) {
180
+ if (this == null) {
181
+ throw new TypeError('"this" is null or not defined');
182
+ }
183
+
184
+ // 1. Let O be ? ToObject(this value).
185
+ var o = Object(this);
186
+
187
+ // 2. Let len be ? ToLength(? Get(O, "length")).
188
+ var len = o.length >>> 0;
189
+
190
+ // 3. If len is 0, return false.
191
+ if (len === 0) {
192
+ return false;
193
+ }
194
+
195
+ // 4. Let n be ? ToInteger(fromIndex).
196
+ // (If fromIndex is undefined, this step produces the value 0.)
197
+ var n = fromIndex | 0;
198
+
199
+ // 5. If n ≥ 0, then
200
+ // a. Let k be n.
201
+ // 6. Else n < 0,
202
+ // a. Let k be len + n.
203
+ // b. If k < 0, let k be 0.
204
+ var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
205
+ function sameValueZero(x, y) {
206
+ return x === y || typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y);
207
+ }
208
+
209
+ // 7. Repeat, while k < len
210
+ while (k < len) {
211
+ // a. Let elementK be the result of ? Get(O, ! ToString(k)).
212
+ // b. If SameValueZero(valueToFind, elementK) is true, return true.
213
+ if (sameValueZero(o[k], valueToFind)) {
214
+ return true;
215
+ }
216
+ // c. Increase k by 1.
217
+ k++;
218
+ }
219
+
220
+ // 8. Return false
221
+ return false;
222
+ }
223
+ });
224
+ }
225
+
226
+ // vivo x7 下网易云游戏 app 缺少 includes 方法
227
+ if (!String.prototype.includes) {
228
+ String.prototype.includes = function (search, start) {
229
+
230
+ if (typeof start !== 'number') {
231
+ start = 0;
232
+ }
233
+ if (start + search.length > this.length) {
234
+ return false;
235
+ } else {
236
+ return this.indexOf(search, start) !== -1;
237
+ }
238
+ };
239
+ }
240
+
241
+ // vivo x7 下网易云游戏 app 缺少 find 方法
242
+ if (!Array.prototype.find) {
243
+ Object.defineProperty(Array.prototype, 'find', {
244
+ value: function value(predicate) {
245
+ // 1. Let O be ? ToObject(this value).
246
+ if (this == null) {
247
+ throw new TypeError('"this" is null or not defined');
248
+ }
249
+ var o = Object(this);
250
+ // 2. Let len be ? ToLength(? Get(O, "length")).
251
+ var len = o.length >>> 0;
252
+ // 3. If IsCallable(predicate) is false, throw a TypeError exception.
253
+ if (typeof predicate !== 'function') {
254
+ throw new TypeError('predicate must be a function');
255
+ }
256
+ // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
257
+ var thisArg = arguments[1];
258
+ // 5. Let k be 0.
259
+ var k = 0;
260
+ // 6. Repeat, while k < len
261
+ while (k < len) {
262
+ // a. Let Pk be ! ToString(k).
263
+ // b. Let kValue be ? Get(O, Pk).
264
+ // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
265
+ // d. If testResult is true, return kValue.
266
+ var kValue = o[k];
267
+ if (predicate.call(thisArg, kValue, k, o)) {
268
+ return kValue;
269
+ }
270
+ // e. Increase k by 1.
271
+ k++;
272
+ }
273
+ // 7. Return undefined.
274
+ return void 0;
275
+ }
276
+ });
277
+ }
278
+
279
+ /**
280
+ * 判断是否是期望的类型
281
+ * @param { unknown } param 将要判断的变量
282
+ * @param { ...string } types 期望的类型
283
+ * @return { boolean } 返回期望是否正确
284
+ */
285
+ var isExpectType = function (param) {
286
+ var types = [];
287
+ for (var _i = 1; _i < arguments.length; _i++) {
288
+ types[_i - 1] = arguments[_i];
289
+ }
290
+ return types.some(function (type) { return Object.prototype.toString.call(param).slice(8, -1).toLowerCase() === type; });
291
+ };
292
+ var has = function (data, key) {
293
+ return Object.prototype.hasOwnProperty.call(data, key);
294
+ };
295
+ /**
296
+ * 节流函数
297
+ * @param fn 将要处理的函数
298
+ * @param wait 时间, 单位为毫秒
299
+ * @returns 包装好的节流函数
300
+ */
301
+ var throttle = function (fn, wait) {
302
+ if (wait === void 0) { wait = 300; }
303
+ var timeId = null;
304
+ return function () {
305
+ var _this = this;
306
+ var args = [];
307
+ for (var _i = 0; _i < arguments.length; _i++) {
308
+ args[_i] = arguments[_i];
309
+ }
310
+ if (timeId)
311
+ return;
312
+ timeId = setTimeout(function () {
313
+ fn.apply(_this, args);
314
+ clearTimeout(timeId);
315
+ timeId = null;
316
+ }, wait);
317
+ };
318
+ };
319
+
320
+ var name = "lucky-scratch";
321
+ var version = "1.1.1";
322
+
323
+ var Dep = /** @class */ (function () {
324
+ /**
325
+ * 订阅中心构造器
326
+ */
327
+ function Dep() {
328
+ this.subs = [];
329
+ }
330
+ /**
331
+ * 收集依赖
332
+ * @param {*} sub
333
+ */
334
+ Dep.prototype.addSub = function (sub) {
335
+ // 此处临时使用includes防重复添加
336
+ if (!this.subs.includes(sub)) {
337
+ this.subs.push(sub);
338
+ }
339
+ };
340
+ /**
341
+ * 派发更新
342
+ */
343
+ Dep.prototype.notify = function () {
344
+ this.subs.forEach(function (sub) {
345
+ sub.update();
346
+ });
347
+ };
348
+ return Dep;
349
+ }());
350
+
351
+ var hasProto = '__proto__' in {};
352
+ function def(obj, key, val, enumerable) {
353
+ Object.defineProperty(obj, key, {
354
+ value: val,
355
+ enumerable: !!enumerable,
356
+ writable: true,
357
+ configurable: true
358
+ });
359
+ }
360
+ function parsePath(path) {
361
+ path += '.';
362
+ var segments = [], segment = '';
363
+ for (var i = 0; i < path.length; i++) {
364
+ var curr = path[i];
365
+ if (/\[|\./.test(curr)) {
366
+ segments.push(segment);
367
+ segment = '';
368
+ }
369
+ else if (/\W/.test(curr)) {
370
+ continue;
371
+ }
372
+ else {
373
+ segment += curr;
374
+ }
375
+ }
376
+ return function (data) {
377
+ return segments.reduce(function (data, key) {
378
+ return data[key];
379
+ }, data);
380
+ };
381
+ }
382
+ function traverse(value) {
383
+ // const seenObjects = new Set()
384
+ var dfs = function (data) {
385
+ if (!isExpectType(data, 'array', 'object'))
386
+ return;
387
+ Object.keys(data).forEach(function (key) {
388
+ var value = data[key];
389
+ dfs(value);
390
+ });
391
+ };
392
+ dfs(value);
393
+ // seenObjects.clear()
394
+ }
395
+
396
+ /**
397
+ * 重写数组的原型方法
398
+ */
399
+ var oldArrayProto = Array.prototype;
400
+ var newArrayProto = Object.create(oldArrayProto);
401
+ var methods = ['push', 'pop', 'shift', 'unshift', 'sort', 'splice', 'reverse'];
402
+ methods.forEach(function (method) {
403
+ newArrayProto[method] = function () {
404
+ var args = [];
405
+ for (var _i = 0; _i < arguments.length; _i++) {
406
+ args[_i] = arguments[_i];
407
+ }
408
+ var res = oldArrayProto[method].apply(this, args);
409
+ var luckyOb = this['__luckyOb__'];
410
+ if (['push', 'unshift', 'splice'].includes(method))
411
+ luckyOb.walk(this);
412
+ luckyOb.dep.notify();
413
+ return res;
414
+ };
415
+ });
416
+
417
+ var Observer = /** @class */ (function () {
418
+ /**
419
+ * 观察者构造器
420
+ * @param value
421
+ */
422
+ function Observer(value) {
423
+ // this.value = value
424
+ this.dep = new Dep();
425
+ // 将响应式对象代理到当前value上面, 并且将当前的enumerable设置为false
426
+ def(value, '__luckyOb__', this);
427
+ if (Array.isArray(value)) { // 如果是数组, 则重写原型方法
428
+ if (hasProto) {
429
+ value['__proto__'] = newArrayProto;
430
+ }
431
+ else {
432
+ Object.getOwnPropertyNames(newArrayProto).forEach(function (key) {
433
+ def(value, key, newArrayProto[key]);
434
+ });
435
+ }
436
+ }
437
+ this.walk(value);
438
+ }
439
+ Observer.prototype.walk = function (data) {
440
+ Object.keys(data).forEach(function (key) {
441
+ defineReactive(data, key, data[key]);
442
+ });
443
+ };
444
+ return Observer;
445
+ }());
446
+ /**
447
+ * 处理响应式
448
+ * @param { Object | Array } data
449
+ */
450
+ function observe(data) {
451
+ if (!data || typeof data !== 'object')
452
+ return;
453
+ var luckyOb;
454
+ if ('__luckyOb__' in data) {
455
+ luckyOb = data['__luckyOb__'];
456
+ }
457
+ else {
458
+ luckyOb = new Observer(data);
459
+ }
460
+ return luckyOb;
461
+ }
462
+ /**
463
+ * 重写 setter / getter
464
+ * @param {*} data
465
+ * @param {*} key
466
+ * @param {*} val
467
+ */
468
+ function defineReactive(data, key, val) {
469
+ var dep = new Dep();
470
+ var property = Object.getOwnPropertyDescriptor(data, key);
471
+ if (property && property.configurable === false) {
472
+ return;
473
+ }
474
+ var getter = property && property.get;
475
+ var setter = property && property.set;
476
+ if ((!getter || setter) && arguments.length === 2) {
477
+ val = data[key];
478
+ }
479
+ var childOb = observe(val);
480
+ Object.defineProperty(data, key, {
481
+ get: function () {
482
+ var value = getter ? getter.call(data) : val;
483
+ if (Dep.target) {
484
+ dep.addSub(Dep.target);
485
+ if (childOb) {
486
+ childOb.dep.addSub(Dep.target);
487
+ }
488
+ }
489
+ return value;
490
+ },
491
+ set: function (newVal) {
492
+ if (newVal === val)
493
+ return;
494
+ val = newVal;
495
+ if (getter && !setter)
496
+ return;
497
+ if (setter) {
498
+ setter.call(data, newVal);
499
+ }
500
+ else {
501
+ val = newVal;
502
+ }
503
+ childOb = observe(newVal);
504
+ dep.notify();
505
+ }
506
+ });
507
+ }
508
+
509
+ var uid = 0;
510
+ var Watcher = /** @class */ (function () {
511
+ /**
512
+ * 观察者构造器
513
+ * @param {*} $lucky
514
+ * @param {*} expr
515
+ * @param {*} cb
516
+ */
517
+ function Watcher($lucky, expr, cb, options) {
518
+ if (options === void 0) { options = {}; }
519
+ this.id = uid++;
520
+ this.$lucky = $lucky;
521
+ this.expr = expr;
522
+ this.deep = !!options.deep;
523
+ if (typeof expr === 'function') {
524
+ this.getter = expr;
525
+ }
526
+ else {
527
+ this.getter = parsePath(expr);
528
+ }
529
+ this.cb = cb;
530
+ this.value = this.get();
531
+ }
532
+ /**
533
+ * 根据表达式获取新值
534
+ */
535
+ Watcher.prototype.get = function () {
536
+ Dep.target = this;
537
+ var value = this.getter.call(this.$lucky, this.$lucky);
538
+ // 处理深度监听
539
+ if (this.deep) {
540
+ traverse(value);
541
+ }
542
+ Dep.target = null;
543
+ return value;
544
+ };
545
+ /**
546
+ * 触发 watcher 更新
547
+ */
548
+ Watcher.prototype.update = function () {
549
+ // get获取新值
550
+ var newVal = this.get();
551
+ // 读取之前存储的旧值
552
+ var oldVal = this.value;
553
+ this.value = newVal;
554
+ // 触发 watch 回调
555
+ this.cb.call(this.$lucky, newVal, oldVal);
556
+ };
557
+ return Watcher;
558
+ }());
559
+
560
+ var Lucky = /** @class */ (function () {
561
+ /**
562
+ * 公共构造器
563
+ * @param config
564
+ */
565
+ function Lucky(config, data) {
566
+ var _this = this;
567
+ this.version = version;
568
+ this.htmlFontSize = 16;
569
+ this.rAF = function () { };
570
+ this.boxWidth = 0;
571
+ this.boxHeight = 0;
572
+ // 兼容代码开始: 为了处理 v1.0.6 版本在这里传入了一个 dom
573
+ if (typeof config === 'string')
574
+ config = { el: config };
575
+ else if (config.nodeType === 1)
576
+ config = { el: '', divElement: config };
577
+ // 这里先野蛮的处理, 等待后续优化, 对外暴露的类型是UserConfigType, 但内部期望是ConfigType
578
+ config = config;
579
+ this.config = config;
580
+ this.data = data;
581
+ // 开始初始化
582
+ if (!config.flag)
583
+ config.flag = 'WEB';
584
+ if (config.el)
585
+ config.divElement = document.querySelector(config.el);
586
+ // 如果存在父盒子, 就创建canvas标签
587
+ if (config.divElement) {
588
+ // 无论盒子内有没有canvas都执行覆盖逻辑
589
+ config.canvasElement = document.createElement('canvas');
590
+ config.divElement.appendChild(config.canvasElement);
591
+ }
592
+ // 获取 canvas 上下文
593
+ if (config.canvasElement) {
594
+ config.ctx = config.canvasElement.getContext('2d');
595
+ // 添加版本信息到标签上, 方便定位版本问题
596
+ config.canvasElement.setAttribute('package', "".concat(name, "@").concat(version));
597
+ config.canvasElement.addEventListener('click', function (e) { return _this.handleClick(e); });
598
+ }
599
+ this.ctx = config.ctx;
600
+ // 初始化 window 方法
601
+ this.initWindowFunction();
602
+ // 如果最后得不到 canvas 上下文那就无法进行绘制
603
+ if (!this.config.ctx) {
604
+ console.error('无法获取到 CanvasContext2D');
605
+ }
606
+ // 监听 window 触发 resize 时重置
607
+ if (window && typeof window.addEventListener === 'function') {
608
+ window.addEventListener('resize', throttle(function () { return _this.resize(); }, 300));
609
+ }
610
+ // 监听异步设置 html 的 fontSize 并重新绘制
611
+ if (window && window.document && typeof window.MutationObserver === 'function' && this.config.flag === 'WEB') {
612
+ new window.MutationObserver(function () {
613
+ _this.resize();
614
+ }).observe(document.documentElement, { attributes: true });
615
+ }
616
+ }
617
+ /**
618
+ * 初始化组件大小/单位
619
+ */
620
+ Lucky.prototype.resize = function () {
621
+ var _a, _b;
622
+ (_b = (_a = this.config).beforeResize) === null || _b === void 0 ? void 0 : _b.call(_a);
623
+ // 先初始化 fontSize 以防后面有 rem 单位
624
+ this.setHTMLFontSize();
625
+ // 拿到 config 即可设置 dpr
626
+ this.setDpr();
627
+ // 初始化宽高
628
+ this.resetWidthAndHeight();
629
+ // 根据 dpr 来缩放 canvas
630
+ this.zoomCanvas();
631
+ };
632
+ /**
633
+ * 初始化方法
634
+ */
635
+ Lucky.prototype.initLucky = function () {
636
+ this.resize();
637
+ if (!this.boxWidth || !this.boxHeight) {
638
+ return console.error('无法获取到宽度或高度');
639
+ }
640
+ };
641
+ /**
642
+ * 鼠标点击事件
643
+ * @param e 事件参数
644
+ */
645
+ Lucky.prototype.handleClick = function (e) { };
646
+ /**
647
+ * 根标签的字体大小
648
+ */
649
+ Lucky.prototype.setHTMLFontSize = function () {
650
+ if (!window || !window.getComputedStyle)
651
+ return;
652
+ this.htmlFontSize = +window.getComputedStyle(document.documentElement).fontSize.slice(0, -2);
653
+ };
654
+ // 清空画布
655
+ Lucky.prototype.clearCanvas = function () {
656
+ var _a = [this.boxWidth, this.boxHeight], width = _a[0], height = _a[1];
657
+ this.ctx.clearRect(-width, -height, width * 2, height * 2);
658
+ };
659
+ /**
660
+ * 设备像素比
661
+ * window 环境下自动获取, 其余环境手动传入
662
+ */
663
+ Lucky.prototype.setDpr = function () {
664
+ var config = this.config;
665
+ if (config.dpr) ;
666
+ else if (window) {
667
+ window['dpr'] = config.dpr = window.devicePixelRatio || 1;
668
+ }
669
+ else if (!config.dpr) {
670
+ console.error(config, '未传入 dpr 可能会导致绘制异常');
671
+ }
672
+ };
673
+ /**
674
+ * 重置盒子和canvas的宽高
675
+ */
676
+ Lucky.prototype.resetWidthAndHeight = function () {
677
+ var _a = this, config = _a.config, data = _a.data;
678
+ // 如果是浏览器环境并且存在盒子
679
+ var boxWidth = 0, boxHeight = 0;
680
+ if (config.divElement) {
681
+ boxWidth = config.divElement.offsetWidth;
682
+ boxHeight = config.divElement.offsetHeight;
683
+ }
684
+ // 先从 data 里取宽高, 如果 config 上面没有, 就从 style 上面取
685
+ this.boxWidth = this.getLength(data.width || config['width']) || boxWidth;
686
+ this.boxHeight = this.getLength(data.height || config['height']) || boxHeight;
687
+ // 重新把宽高赋给盒子
688
+ if (config.divElement) {
689
+ config.divElement.style.overflow = 'hidden';
690
+ config.divElement.style.width = this.boxWidth + 'px';
691
+ config.divElement.style.height = this.boxHeight + 'px';
692
+ }
693
+ };
694
+ /**
695
+ * 根据 dpr 缩放 canvas 并处理位移
696
+ */
697
+ Lucky.prototype.zoomCanvas = function () {
698
+ var _a = this, config = _a.config, ctx = _a.ctx;
699
+ var canvasElement = config.canvasElement, dpr = config.dpr;
700
+ var _b = [this.boxWidth * dpr, this.boxHeight * dpr], width = _b[0], height = _b[1];
701
+ if (!canvasElement)
702
+ return;
703
+ canvasElement.width = width;
704
+ canvasElement.height = height;
705
+ canvasElement.style.width = "".concat(width, "px");
706
+ canvasElement.style.height = "".concat(height, "px");
707
+ canvasElement.style['transform-origin'] = 'left top';
708
+ canvasElement.style.transform = "scale(".concat(1 / dpr, ")");
709
+ ctx.scale(dpr, dpr);
710
+ };
711
+ /**
712
+ * 从 window 对象上获取一些方法
713
+ */
714
+ Lucky.prototype.initWindowFunction = function () {
715
+ var config = this.config;
716
+ if (window) {
717
+ this.rAF = window.requestAnimationFrame ||
718
+ window['webkitRequestAnimationFrame'] ||
719
+ window['mozRequestAnimationFrame'] ||
720
+ function (callback) {
721
+ window.setTimeout(callback, 1000 / 60);
722
+ };
723
+ config.setTimeout = window.setTimeout;
724
+ config.setInterval = window.setInterval;
725
+ config.clearTimeout = window.clearTimeout;
726
+ config.clearInterval = window.clearInterval;
727
+ return;
728
+ }
729
+ if (config.rAF) {
730
+ // 优先使用帧动画
731
+ this.rAF = config.rAF;
732
+ }
733
+ else if (config.setTimeout) {
734
+ // 其次使用定时器
735
+ var timeout_1 = config.setTimeout;
736
+ this.rAF = function (callback) { return timeout_1(callback, 16.7); };
737
+ }
738
+ else {
739
+ // 如果config里面没有提供, 那就假设全局方法存在setTimeout
740
+ this.rAF = function (callback) { return setTimeout(callback, 16.7); };
741
+ }
742
+ };
743
+ Lucky.prototype.isWeb = function () {
744
+ return ['WEB', 'UNI-H5', 'TARO-H5'].includes(this.config.flag);
745
+ };
746
+ /**
747
+ * 异步加载图片并返回图片的几何信息
748
+ * @param src 图片路径
749
+ * @param info 图片信息
750
+ */
751
+ Lucky.prototype.loadImg = function (src, info, resolveName) {
752
+ var _this = this;
753
+ if (resolveName === void 0) { resolveName = '$resolve'; }
754
+ return new Promise(function (resolve, reject) {
755
+ if (!src)
756
+ reject("=> '".concat(info.src, "' \u4E0D\u80FD\u4E3A\u7A7A\u6216\u4E0D\u5408\u6CD5"));
757
+ if (_this.config.flag === 'WEB') {
758
+ var imgObj_1 = new Image();
759
+ imgObj_1['crossorigin'] = 'anonymous';
760
+ imgObj_1.onload = function () { return resolve(imgObj_1); };
761
+ imgObj_1.onerror = function () { return reject("=> '".concat(info.src, "' \u56FE\u7247\u52A0\u8F7D\u5931\u8D25")); };
762
+ imgObj_1.src = src;
763
+ }
764
+ else {
765
+ // 其余平台向外暴露, 交给外部自行处理
766
+ info[resolveName] = resolve;
767
+ info['$reject'] = reject;
768
+ return;
769
+ }
770
+ });
771
+ };
772
+ /**
773
+ * 公共绘制图片的方法
774
+ * @param imgObj 图片对象
775
+ * @param rectInfo: [x轴位置, y轴位置, 渲染宽度, 渲染高度]
776
+ */
777
+ Lucky.prototype.drawImage = function (ctx, imgObj) {
778
+ var _a, _b;
779
+ var rectInfo = [];
780
+ for (var _i = 2; _i < arguments.length; _i++) {
781
+ rectInfo[_i - 2] = arguments[_i];
782
+ }
783
+ var drawImg;
784
+ var _c = this.config, flag = _c.flag, dpr = _c.dpr;
785
+ if (['WEB', 'MP-WX'].includes(flag)) {
786
+ // 浏览器和新版小程序中直接绘制即可
787
+ drawImg = imgObj;
788
+ }
789
+ else if (['UNI-H5', 'UNI-MP', 'TARO-H5', 'TARO-MP'].includes(flag)) {
790
+ drawImg = imgObj.path;
791
+ }
792
+ else {
793
+ // 如果传入了未知的标识
794
+ return console.error('意料之外的 flag, 该平台尚未兼容!');
795
+ }
796
+ var miniProgramOffCtx = (_b = (_a = (drawImg['canvas'] || drawImg)).getContext) === null || _b === void 0 ? void 0 : _b.call(_a, '2d');
797
+ if (miniProgramOffCtx && !this.isWeb()) {
798
+ rectInfo = rectInfo.map(function (val) { return val * dpr; });
799
+ var temp = miniProgramOffCtx.getImageData.apply(miniProgramOffCtx, rectInfo.slice(0, 4));
800
+ ctx.putImageData.apply(ctx, __spreadArray([temp], rectInfo.slice(4, 6), false));
801
+ }
802
+ else {
803
+ if (rectInfo.length === 8) {
804
+ rectInfo = rectInfo.map(function (val, index) { return index < 4 ? val * dpr : val; });
805
+ }
806
+ // 尝试捕获错误
807
+ try {
808
+ ctx.drawImage.apply(ctx, __spreadArray([drawImg], rectInfo, false));
809
+ }
810
+ catch (err) {
811
+ /**
812
+ * TODO: safari浏览器下, init() 会出现奇怪的报错
813
+ * IndexSizeError: The index is not in the allowed range
814
+ * 但是这个报错并不影响实际的绘制, 目前先放一放, 等待有缘人
815
+ */
816
+ // console.log(err)
817
+ }
818
+ }
819
+ };
820
+ /**
821
+ * 计算图片的渲染宽高
822
+ * @param imgObj 图片标签元素
823
+ * @param imgInfo 图片信息
824
+ * @param maxWidth 最大宽度
825
+ * @param maxHeight 最大高度
826
+ * @return [渲染宽度, 渲染高度]
827
+ */
828
+ Lucky.prototype.computedWidthAndHeight = function (imgObj, imgInfo, maxWidth, maxHeight) {
829
+ // 根据配置的样式计算图片的真实宽高
830
+ if (!imgInfo.width && !imgInfo.height) {
831
+ // 如果没有配置宽高, 则使用图片本身的宽高
832
+ return [imgObj.width, imgObj.height];
833
+ }
834
+ else if (imgInfo.width && !imgInfo.height) {
835
+ // 如果只填写了宽度, 没填写高度
836
+ var trueWidth = this.getLength(imgInfo.width, maxWidth);
837
+ // 那高度就随着宽度进行等比缩放
838
+ return [trueWidth, imgObj.height * (trueWidth / imgObj.width)];
839
+ }
840
+ else if (!imgInfo.width && imgInfo.height) {
841
+ // 如果只填写了宽度, 没填写高度
842
+ var trueHeight = this.getLength(imgInfo.height, maxHeight);
843
+ // 那宽度就随着高度进行等比缩放
844
+ return [imgObj.width * (trueHeight / imgObj.height), trueHeight];
845
+ }
846
+ // 如果宽度和高度都填写了, 就如实计算
847
+ return [
848
+ this.getLength(imgInfo.width, maxWidth),
849
+ this.getLength(imgInfo.height, maxHeight)
850
+ ];
851
+ };
852
+ /**
853
+ * 转换单位
854
+ * @param { string } value 将要转换的值
855
+ * @param { number } denominator 分子
856
+ * @return { number } 返回新的字符串
857
+ */
858
+ Lucky.prototype.changeUnits = function (value, denominator) {
859
+ var _this = this;
860
+ if (denominator === void 0) { denominator = 1; }
861
+ var config = this.config;
862
+ return Number(value.replace(/^([-]*[0-9.]*)([a-z%]*)$/, function (val, num, unit) {
863
+ var handleCssUnit = {
864
+ '%': function (n) { return n * (denominator / 100); },
865
+ 'px': function (n) { return n * 1; },
866
+ 'rem': function (n) { return n * _this.htmlFontSize; },
867
+ 'vw': function (n) { return n / 100 * window.innerWidth; },
868
+ }[unit];
869
+ if (handleCssUnit)
870
+ return handleCssUnit(num);
871
+ // 如果找不到默认单位, 就交给外面处理
872
+ var otherHandleCssUnit = config.handleCssUnit || config['unitFunc'];
873
+ return otherHandleCssUnit ? otherHandleCssUnit(num, unit) : num;
874
+ }));
875
+ };
876
+ /**
877
+ * 获取长度
878
+ * @param length 将要转换的长度
879
+ * @param maxLength 最大长度
880
+ * @return 返回长度
881
+ */
882
+ Lucky.prototype.getLength = function (length, maxLength) {
883
+ if (isExpectType(length, 'number'))
884
+ return length;
885
+ if (isExpectType(length, 'string'))
886
+ return this.changeUnits(length, maxLength);
887
+ return 0;
888
+ };
889
+ /**
890
+ * 获取相对(居中)X坐标
891
+ * @param width
892
+ * @param col
893
+ */
894
+ Lucky.prototype.getOffsetX = function (width, maxWidth) {
895
+ if (maxWidth === void 0) { maxWidth = 0; }
896
+ return (maxWidth - width) / 2;
897
+ };
898
+ Lucky.prototype.getOffscreenCanvas = function (width, height) {
899
+ if (!has(this, '_offscreenCanvas')) {
900
+ if (window && window.document && this.config.flag === 'WEB') {
901
+ this['_offscreenCanvas'] = document.createElement('canvas');
902
+ }
903
+ else {
904
+ this['_offscreenCanvas'] = this.config['offscreenCanvas'];
905
+ }
906
+ if (!this['_offscreenCanvas'])
907
+ return console.error('离屏 Canvas 无法渲染!');
908
+ }
909
+ var dpr = this.config.dpr;
910
+ var _offscreenCanvas = this['_offscreenCanvas'];
911
+ _offscreenCanvas.width = (width || 300) * dpr;
912
+ _offscreenCanvas.height = (height || 150) * dpr;
913
+ var _ctx = _offscreenCanvas.getContext('2d');
914
+ _ctx.clearRect(0, 0, width, height);
915
+ _ctx.scale(dpr, dpr);
916
+ _ctx['dpr'] = dpr;
917
+ return { _offscreenCanvas: _offscreenCanvas, _ctx: _ctx };
918
+ };
919
+ /**
920
+ * 添加一个新的响应式数据 (临时)
921
+ * @param data 数据
922
+ * @param key 属性
923
+ * @param value 新值
924
+ */
925
+ Lucky.prototype.$set = function (data, key, value) {
926
+ if (!data || typeof data !== 'object')
927
+ return;
928
+ defineReactive(data, key, value);
929
+ };
930
+ /**
931
+ * 添加一个属性计算 (临时)
932
+ * @param data 源数据
933
+ * @param key 属性名
934
+ * @param callback 回调函数
935
+ */
936
+ Lucky.prototype.$computed = function (data, key, callback) {
937
+ var _this = this;
938
+ Object.defineProperty(data, key, {
939
+ get: function () {
940
+ return callback.call(_this);
941
+ }
942
+ });
943
+ };
944
+ /**
945
+ * 添加一个观察者 create user watcher
946
+ * @param expr 表达式
947
+ * @param handler 回调函数
948
+ * @param watchOpt 配置参数
949
+ * @return 卸载当前观察者的函数 (暂未返回)
950
+ */
951
+ Lucky.prototype.$watch = function (expr, handler, watchOpt) {
952
+ if (watchOpt === void 0) { watchOpt = {}; }
953
+ if (typeof handler === 'object') {
954
+ watchOpt = handler;
955
+ handler = watchOpt.handler;
956
+ }
957
+ // 创建 user watcher
958
+ var watcher = new Watcher(this, expr, handler, watchOpt);
959
+ // 判断是否需要初始化时触发回调
960
+ if (watchOpt.immediate) {
961
+ handler.call(this, watcher.value);
962
+ }
963
+ // 返回一个卸载当前观察者的函数
964
+ return function unWatchFn() { };
965
+ };
966
+ Lucky.version = version;
967
+ return Lucky;
968
+ }());
969
+
970
+ var LuckyScratch = /** @class */ (function (_super) {
971
+ __extends(LuckyScratch, _super);
972
+ /**
973
+ * 刮刮卡构造器
974
+ * @param config 配置项
975
+ * @param data 抽奖数据
976
+ */
977
+ function LuckyScratch(config, data) {
978
+ var _this = _super.call(this, config, {
979
+ width: data.width || '300px',
980
+ height: data.height || '150px',
981
+ }) || this;
982
+ _this.mask = {};
983
+ _this.scratch = {
984
+ radius: 20,
985
+ percent: 0.5,
986
+ };
987
+ // 记录当前刮开的比例
988
+ _this.progress = 0;
989
+ // 是否正在刮
990
+ _this.isScratching = false;
991
+ // 是否已完成(刮开达到阈值)
992
+ _this.isCompleted = false;
993
+ // 是否禁用刮奖
994
+ _this.disabled = false;
995
+ // 是否是第一次刮动
996
+ _this.isFirstScratch = true;
997
+ // 事件是否已绑定
998
+ _this.eventsInitialized = false;
999
+ _this.initData(data);
1000
+ // 首次初始化
1001
+ _this.init();
1002
+ return _this;
1003
+ }
1004
+ LuckyScratch.prototype.initData = function (data) {
1005
+ this.$set(this, 'mask', __assign({ type: 'color', color: '#ccc' }, data.mask));
1006
+ this.$set(this, 'scratch', __assign({ radius: 20, percent: 0.5 }, data.scratch));
1007
+ this.$set(this, 'onceBeforeStartCallback', data.onceBeforeStart);
1008
+ this.$set(this, 'beforeStartCallback', data.beforeStart);
1009
+ this.$set(this, 'startCallback', data.start);
1010
+ this.$set(this, 'endCallback', data.end);
1011
+ this.$set(this, 'successCallback', data.success);
1012
+ this.$set(this, 'afterInitCallback', data.afterInit);
1013
+ };
1014
+ /**
1015
+ * 重写 resize 方法,在尺寸变化后重新绘制蒙层
1016
+ */
1017
+ LuckyScratch.prototype.resize = function () {
1018
+ var _a, _b;
1019
+ _super.prototype.resize.call(this);
1020
+ // resize 后需要重新绘制蒙层,否则 canvas 尺寸变化会清空内容
1021
+ // 注意:draw() 是 async 方法,但这里不需要 await,
1022
+ // 因为对于颜色类型的蒙层,绘制是同步的;只有图片类型才需要异步加载
1023
+ this.draw();
1024
+ (_b = (_a = this.config).afterResize) === null || _b === void 0 ? void 0 : _b.call(_a);
1025
+ };
1026
+ LuckyScratch.prototype.init = function () {
1027
+ var _a;
1028
+ return __awaiter(this, void 0, void 0, function () {
1029
+ return __generator(this, function (_b) {
1030
+ switch (_b.label) {
1031
+ case 0:
1032
+ this.initLucky();
1033
+ // 重置状态
1034
+ this.progress = 0;
1035
+ this.isScratching = false;
1036
+ this.isCompleted = false;
1037
+ this.isFirstScratch = true; // 重置第一次刮动标志
1038
+ return [4 /*yield*/, this.draw()
1039
+ // 绑定事件
1040
+ ];
1041
+ case 1:
1042
+ _b.sent();
1043
+ // 绑定事件
1044
+ this.handleBindEvents();
1045
+ // 触发初始化完成回调
1046
+ (_a = this.afterInitCallback) === null || _a === void 0 ? void 0 : _a.call(this);
1047
+ return [2 /*return*/];
1048
+ }
1049
+ });
1050
+ });
1051
+ };
1052
+ LuckyScratch.prototype.draw = function () {
1053
+ var _a, _b, _c, _d;
1054
+ return __awaiter(this, void 0, void 0, function () {
1055
+ var _e, mask, imgObj;
1056
+ return __generator(this, function (_f) {
1057
+ switch (_f.label) {
1058
+ case 0:
1059
+ // 触发开始回调
1060
+ (_b = (_a = this.config).beforeInit) === null || _b === void 0 ? void 0 : _b.call(_a);
1061
+ // 清空画布
1062
+ this.clearCanvas();
1063
+ _e = this, mask = _e.mask, _e.scratch;
1064
+ if (!(mask.type === 'image' && mask.src)) return [3 /*break*/, 2];
1065
+ return [4 /*yield*/, this.loadImg(mask.src, { src: mask.src })];
1066
+ case 1:
1067
+ imgObj = _f.sent();
1068
+ this.ctx.drawImage(imgObj, 0, 0, this.boxWidth, this.boxHeight);
1069
+ return [3 /*break*/, 3];
1070
+ case 2:
1071
+ // 默认为颜色
1072
+ this.ctx.fillStyle = mask.color || '#ccc';
1073
+ this.ctx.fillRect(0, 0, this.boxWidth, this.boxHeight);
1074
+ _f.label = 3;
1075
+ case 3:
1076
+ // 触发绘制结束回调
1077
+ (_d = (_c = this.config).afterInit) === null || _d === void 0 ? void 0 : _d.call(_c);
1078
+ return [2 /*return*/];
1079
+ }
1080
+ });
1081
+ });
1082
+ };
1083
+ LuckyScratch.prototype.handleBindEvents = function () {
1084
+ var _this = this;
1085
+ // 防止重复绑定事件
1086
+ if (this.eventsInitialized)
1087
+ return;
1088
+ this.eventsInitialized = true;
1089
+ var canvas = this.config.canvasElement;
1090
+ if (!canvas)
1091
+ return;
1092
+ // 移动端 touch 事件
1093
+ canvas.addEventListener('touchstart', function (e) {
1094
+ _this.handleStart(e.touches[0]);
1095
+ });
1096
+ canvas.addEventListener('touchmove', function (e) {
1097
+ e.preventDefault();
1098
+ _this.handleMove(e.touches[0]);
1099
+ });
1100
+ canvas.addEventListener('touchend', function () {
1101
+ _this.handleEnd();
1102
+ });
1103
+ // PC 端 mouse 事件
1104
+ canvas.addEventListener('mousedown', function (e) {
1105
+ _this.handleStart(e);
1106
+ });
1107
+ canvas.addEventListener('mousemove', function (e) {
1108
+ e.preventDefault();
1109
+ _this.handleMove(e);
1110
+ });
1111
+ document.addEventListener('mouseup', function () {
1112
+ _this.handleEnd();
1113
+ });
1114
+ };
1115
+ LuckyScratch.prototype.handleStart = function (e) {
1116
+ var _a;
1117
+ return __awaiter(this, void 0, void 0, function () {
1118
+ var result, err_1, result, err_2;
1119
+ return __generator(this, function (_b) {
1120
+ switch (_b.label) {
1121
+ case 0:
1122
+ // 如果已完成或被禁用,不再响应事件
1123
+ if (this.isCompleted || this.disabled)
1124
+ return [2 /*return*/];
1125
+ if (!(this.isFirstScratch && this.onceBeforeStartCallback)) return [3 /*break*/, 5];
1126
+ _b.label = 1;
1127
+ case 1:
1128
+ _b.trys.push([1, 3, , 4]);
1129
+ return [4 /*yield*/, this.onceBeforeStartCallback()];
1130
+ case 2:
1131
+ result = _b.sent();
1132
+ if (result === false) {
1133
+ // 校验失败,阻止刮奖
1134
+ return [2 /*return*/];
1135
+ }
1136
+ return [3 /*break*/, 4];
1137
+ case 3:
1138
+ err_1 = _b.sent();
1139
+ console.error('onceBeforeStart 回调执行出错:', err_1);
1140
+ return [2 /*return*/];
1141
+ case 4:
1142
+ // 标记已经不是第一次刮动了
1143
+ this.isFirstScratch = false;
1144
+ _b.label = 5;
1145
+ case 5:
1146
+ if (!this.beforeStartCallback) return [3 /*break*/, 9];
1147
+ _b.label = 6;
1148
+ case 6:
1149
+ _b.trys.push([6, 8, , 9]);
1150
+ return [4 /*yield*/, this.beforeStartCallback()];
1151
+ case 7:
1152
+ result = _b.sent();
1153
+ if (result === false) {
1154
+ // 校验失败,阻止刮奖
1155
+ return [2 /*return*/];
1156
+ }
1157
+ return [3 /*break*/, 9];
1158
+ case 8:
1159
+ err_2 = _b.sent();
1160
+ console.error('beforeStart 回调执行出错:', err_2);
1161
+ return [2 /*return*/];
1162
+ case 9:
1163
+ this.isScratching = true;
1164
+ (_a = this.startCallback) === null || _a === void 0 ? void 0 : _a.call(this);
1165
+ this.drawArc(e);
1166
+ return [2 /*return*/];
1167
+ }
1168
+ });
1169
+ });
1170
+ };
1171
+ LuckyScratch.prototype.handleMove = function (e) {
1172
+ if (!this.isScratching || this.isCompleted || this.disabled)
1173
+ return;
1174
+ this.drawArc(e);
1175
+ };
1176
+ LuckyScratch.prototype.handleEnd = function () {
1177
+ var _a;
1178
+ if (!this.isScratching || this.isCompleted || this.disabled)
1179
+ return;
1180
+ this.isScratching = false;
1181
+ // 计算刮开比例
1182
+ this.checkProgress();
1183
+ (_a = this.endCallback) === null || _a === void 0 ? void 0 : _a.call(this);
1184
+ };
1185
+ LuckyScratch.prototype.drawArc = function (e) {
1186
+ var canvas = this.config.canvasElement;
1187
+ if (!canvas)
1188
+ return;
1189
+ var rect = canvas.getBoundingClientRect();
1190
+ var x = (e.clientX - rect.left) * this.config.dpr;
1191
+ var y = (e.clientY - rect.top) * this.config.dpr;
1192
+ this.drawArcAt(x, y);
1193
+ };
1194
+ /**
1195
+ * 在指定坐标绘制刮痕
1196
+ * @param x 相对于 canvas 的 x 坐标(物理像素,已乘以 dpr)
1197
+ * @param y 相对于 canvas 的 y 坐标(物理像素,已乘以 dpr)
1198
+ */
1199
+ LuckyScratch.prototype.drawArcAt = function (x, y) {
1200
+ this.ctx.globalCompositeOperation = 'destination-out';
1201
+ this.ctx.beginPath();
1202
+ this.ctx.arc(x, y, this.scratch.radius * this.config.dpr, 0, Math.PI * 2);
1203
+ this.ctx.fill();
1204
+ // 恢复混合模式
1205
+ this.ctx.globalCompositeOperation = 'source-over';
1206
+ };
1207
+ LuckyScratch.prototype.checkProgress = function () {
1208
+ var _a;
1209
+ try {
1210
+ var imageData = this.ctx.getImageData(0, 0, this.boxWidth * this.config.dpr, this.boxHeight * this.config.dpr);
1211
+ var pixels = imageData.data;
1212
+ var count = 0;
1213
+ for (var i = 0; i < pixels.length; i += 4) {
1214
+ // alpha 通道小于 128 认为已刮开
1215
+ if (pixels[i + 3] < 128) {
1216
+ count++;
1217
+ }
1218
+ }
1219
+ this.progress = count / (pixels.length / 4);
1220
+ // 如果达到阈值
1221
+ if (this.progress >= this.scratch.percent) {
1222
+ this.isCompleted = true; // 标记为已完成
1223
+ this.clearCanvas(); // 自动全部刮开
1224
+ (_a = this.successCallback) === null || _a === void 0 ? void 0 : _a.call(this, this.progress);
1225
+ }
1226
+ }
1227
+ catch (err) {
1228
+ // 可能会遇到跨域图片污染画布的问题
1229
+ console.error('无法计算刮开进度,可能是因为图片跨域:', err);
1230
+ }
1231
+ };
1232
+ /**
1233
+ * 动态设置禁用状态
1234
+ * @param disabled 是否禁用
1235
+ */
1236
+ LuckyScratch.prototype.setDisabled = function (disabled) {
1237
+ this.disabled = disabled;
1238
+ };
1239
+ /**
1240
+ * 公共方法:处理触摸/鼠标开始事件
1241
+ * 供小程序、uni-app 等非 DOM 环境使用
1242
+ * @param x 相对于 canvas 的 x 坐标(逻辑像素)
1243
+ * @param y 相对于 canvas 的 y 坐标(逻辑像素)
1244
+ */
1245
+ LuckyScratch.prototype.handleTouchStart = function (x, y) {
1246
+ var _a;
1247
+ return __awaiter(this, void 0, void 0, function () {
1248
+ var result, err_3, result, err_4;
1249
+ return __generator(this, function (_b) {
1250
+ switch (_b.label) {
1251
+ case 0:
1252
+ // 如果已完成或被禁用,不再响应事件
1253
+ if (this.isCompleted || this.disabled)
1254
+ return [2 /*return*/];
1255
+ if (!(this.isFirstScratch && this.onceBeforeStartCallback)) return [3 /*break*/, 5];
1256
+ _b.label = 1;
1257
+ case 1:
1258
+ _b.trys.push([1, 3, , 4]);
1259
+ return [4 /*yield*/, this.onceBeforeStartCallback()];
1260
+ case 2:
1261
+ result = _b.sent();
1262
+ if (result === false) {
1263
+ // 校验失败,阻止刮奖
1264
+ return [2 /*return*/];
1265
+ }
1266
+ return [3 /*break*/, 4];
1267
+ case 3:
1268
+ err_3 = _b.sent();
1269
+ console.error('onceBeforeStart 回调执行出错:', err_3);
1270
+ return [2 /*return*/];
1271
+ case 4:
1272
+ // 标记已经不是第一次刮动了
1273
+ this.isFirstScratch = false;
1274
+ _b.label = 5;
1275
+ case 5:
1276
+ if (!this.beforeStartCallback) return [3 /*break*/, 9];
1277
+ _b.label = 6;
1278
+ case 6:
1279
+ _b.trys.push([6, 8, , 9]);
1280
+ return [4 /*yield*/, this.beforeStartCallback()];
1281
+ case 7:
1282
+ result = _b.sent();
1283
+ if (result === false) {
1284
+ // 校验失败,阻止刮奖
1285
+ return [2 /*return*/];
1286
+ }
1287
+ return [3 /*break*/, 9];
1288
+ case 8:
1289
+ err_4 = _b.sent();
1290
+ console.error('beforeStart 回调执行出错:', err_4);
1291
+ return [2 /*return*/];
1292
+ case 9:
1293
+ this.isScratching = true;
1294
+ (_a = this.startCallback) === null || _a === void 0 ? void 0 : _a.call(this);
1295
+ // 将逻辑像素转换为物理像素(乘以 dpr)
1296
+ this.drawArcAt(x * this.config.dpr, y * this.config.dpr);
1297
+ return [2 /*return*/];
1298
+ }
1299
+ });
1300
+ });
1301
+ };
1302
+ /**
1303
+ * 公共方法:处理触摸/鼠标移动事件
1304
+ * 供小程序、uni-app 等非 DOM 环境使用
1305
+ * @param x 相对于 canvas 的 x 坐标(逻辑像素)
1306
+ * @param y 相对于 canvas 的 y 坐标(逻辑像素)
1307
+ */
1308
+ LuckyScratch.prototype.handleTouchMove = function (x, y) {
1309
+ if (!this.isScratching || this.isCompleted || this.disabled)
1310
+ return;
1311
+ // 将逻辑像素转换为物理像素(乘以 dpr)
1312
+ this.drawArcAt(x * this.config.dpr, y * this.config.dpr);
1313
+ };
1314
+ /**
1315
+ * 公共方法:处理触摸/鼠标结束事件
1316
+ * 供小程序、uni-app 等非 DOM 环境使用
1317
+ */
1318
+ LuckyScratch.prototype.handleTouchEnd = function () {
1319
+ var _a;
1320
+ if (!this.isScratching || this.isCompleted || this.disabled)
1321
+ return;
1322
+ this.isScratching = false;
1323
+ // 计算刮开比例
1324
+ this.checkProgress();
1325
+ (_a = this.endCallback) === null || _a === void 0 ? void 0 : _a.call(this);
1326
+ };
1327
+ return LuckyScratch;
1328
+ }(Lucky));
1329
+
1330
+ /**
1331
+ * 转换为运算角度
1332
+ * @param { number } deg 数学角度
1333
+ * @return { number } 运算角度
1334
+ */
1335
+ // 使用 arc 绘制圆角矩形
1336
+ var roundRectByArc = function (ctx) {
1337
+ var _a = [];
1338
+ for (var _i = 1; _i < arguments.length; _i++) {
1339
+ _a[_i - 1] = arguments[_i];
1340
+ }
1341
+ var x = _a[0], y = _a[1], w = _a[2], h = _a[3], r = _a[4];
1342
+ var min = Math.min(w, h), PI = Math.PI;
1343
+ if (r > min / 2)
1344
+ r = min / 2;
1345
+ ctx.beginPath();
1346
+ ctx.moveTo(x + r, y);
1347
+ ctx.lineTo(x + r, y);
1348
+ ctx.lineTo(x + w - r, y);
1349
+ ctx.arc(x + w - r, y + r, r, -PI / 2, 0);
1350
+ ctx.lineTo(x + w, y + h - r);
1351
+ ctx.arc(x + w - r, y + h - r, r, 0, PI / 2);
1352
+ ctx.lineTo(x + r, y + h);
1353
+ ctx.arc(x + r, y + h - r, r, PI / 2, PI);
1354
+ ctx.lineTo(x, y + r);
1355
+ ctx.arc(x + r, y + r, r, PI, -PI / 2);
1356
+ ctx.closePath();
1357
+ };
1358
+ // // 根据三点画圆弧
1359
+ // export const drawRadian = (
1360
+ // ctx: CanvasRenderingContext2D,
1361
+ // r: number,
1362
+ // start: number,
1363
+ // end: number,
1364
+ // direction: boolean = true
1365
+ // ) => {
1366
+ // // 如果角度大于等于180度, 则分两次绘制, 因为 arcTo 无法绘制180度的圆弧
1367
+ // if (Math.abs(end - start).toFixed(8) >= getAngle(180).toFixed(8)) {
1368
+ // let middle = (end + start) / 2
1369
+ // if (direction) {
1370
+ // drawRadian(ctx, r, start, middle, direction)
1371
+ // drawRadian(ctx, r, middle, end, direction)
1372
+ // } else {
1373
+ // drawRadian(ctx, r, middle, end, direction)
1374
+ // drawRadian(ctx, r, start, middle, direction)
1375
+ // }
1376
+ // return false
1377
+ // }
1378
+ // // 如果方法相反, 则交换起点和终点
1379
+ // if (!direction) [start, end] = [end, start]
1380
+ // const [x1, y1] = getArcPointerByDeg(start, r)
1381
+ // const [x2, y2] = getArcPointerByDeg(end, r)
1382
+ // const [k1, b1] = getTangentByPointer(x1, y1)
1383
+ // const [k2, b2] = getTangentByPointer(x2, y2)
1384
+ // // 计算两条切线的交点
1385
+ // let x0 = (b2 - b1) / (k1 - k2)
1386
+ // let y0 = (k2 * b1 - k1 * b2) / (k2 - k1)
1387
+ // // 如果有任何一条切线垂直于x轴, 则斜率不存在
1388
+ // if (isNaN(x0)) {
1389
+ // Math.abs(x1) === +r.toFixed(8) && (x0 = x1)
1390
+ // Math.abs(x2) === +r.toFixed(8) && (x0 = x2)
1391
+ // }
1392
+ // if (k1 === Infinity || k1 === -Infinity) {
1393
+ // y0 = k2 * x0 + b2
1394
+ // }
1395
+ // else if (k2 === Infinity || k2 === -Infinity) {
1396
+ // y0 = k1 * x0 + b1
1397
+ // }
1398
+ // ctx.lineTo(x1, y1)
1399
+ // // 微信小程序下 arcTo 在安卓真机下绘制有 bug
1400
+ // ctx.arcTo(x0, y0, x2, y2, r)
1401
+ // }
1402
+ // // 使用 arcTo 绘制扇形 (弃用)
1403
+ // export const drawSectorByArcTo = (
1404
+ // ctx: CanvasRenderingContext2D,
1405
+ // minRadius: number,
1406
+ // maxRadius: number,
1407
+ // start: number,
1408
+ // end: number,
1409
+ // gutter: number,
1410
+ // ) => {
1411
+ // if (!minRadius) minRadius = gutter
1412
+ // // 内外圆弧分别进行等边缩放
1413
+ // let maxGutter = getAngle(90 / Math.PI / maxRadius * gutter)
1414
+ // let minGutter = getAngle(90 / Math.PI / minRadius * gutter)
1415
+ // let maxStart = start + maxGutter
1416
+ // let maxEnd = end - maxGutter
1417
+ // let minStart = start + minGutter
1418
+ // let minEnd = end - minGutter
1419
+ // ctx.beginPath()
1420
+ // ctx.moveTo(...getArcPointerByDeg(maxStart, maxRadius))
1421
+ // drawRadian(ctx, maxRadius, maxStart, maxEnd, true)
1422
+ // // 如果 getter 比按钮短就绘制圆弧, 反之计算新的坐标点
1423
+ // if (minEnd > minStart) {
1424
+ // drawRadian(ctx, minRadius, minStart, minEnd, false)
1425
+ // } else {
1426
+ // ctx.lineTo(
1427
+ // ...getArcPointerByDeg(
1428
+ // (start + end) / 2,
1429
+ // gutter / 2 / Math.abs(Math.sin((start - end) / 2))
1430
+ // )
1431
+ // )
1432
+ // }
1433
+ // ctx.closePath()
1434
+ // }
1435
+ // // 使用 arcTo 绘制圆角矩形 (弃用)
1436
+ // export const roundRectByArcTo = (
1437
+ // ctx: CanvasRenderingContext2D,
1438
+ // ...[x, y, w, h, r]: number[]
1439
+ // ) => {
1440
+ // let min = Math.min(w, h)
1441
+ // if (r > min / 2) r = min / 2
1442
+ // ctx.beginPath()
1443
+ // ctx.moveTo(x + r, y)
1444
+ // ctx.lineTo(x + r, y)
1445
+ // ctx.lineTo(x + w - r, y)
1446
+ // ctx.arcTo(x + w, y, x + w, y + r, r)
1447
+ // ctx.lineTo(x + w, y + h - r)
1448
+ // ctx.arcTo(x + w, y + h, x + w - r, y + h, r)
1449
+ // ctx.lineTo(x + r, y + h)
1450
+ // ctx.arcTo(x, y + h, x, y + h - r, r)
1451
+ // ctx.lineTo(x, y + r)
1452
+ // ctx.arcTo(x, y, x + r, y, r)
1453
+ // }
1454
+
1455
+ /**
1456
+ * 切割圆角
1457
+ * @param img 将要裁剪的图片对象
1458
+ * @param radius 裁剪的圆角半径
1459
+ * @returns 返回一个离屏 canvas 用于渲染
1460
+ */
1461
+ var cutRound = function (img, radius) {
1462
+ var canvas = document.createElement('canvas');
1463
+ var ctx = canvas.getContext('2d');
1464
+ var width = img.width, height = img.height;
1465
+ canvas.width = width;
1466
+ canvas.height = height;
1467
+ roundRectByArc(ctx, 0, 0, width, height, radius);
1468
+ ctx.clip();
1469
+ ctx.drawImage(img, 0, 0, width, height);
1470
+ return canvas;
1471
+ };
1472
+ /**
1473
+ * 透明度
1474
+ * @param img 将要处理的图片对象
1475
+ * @param opacity 透明度
1476
+ * @returns 返回一个离屏 canvas 用于渲染
1477
+ */
1478
+ var opacity = function (img, opacity) {
1479
+ var canvas = document.createElement('canvas');
1480
+ var ctx = canvas.getContext('2d');
1481
+ var width = img.width, height = img.height;
1482
+ canvas.width = width;
1483
+ canvas.height = height;
1484
+ // 绘制图片, 部分浏览器不支持 filter 属性, 需要处理兼容
1485
+ if (typeof ctx.filter === 'string') {
1486
+ ctx.filter = "opacity(".concat(opacity * 100, "%)");
1487
+ ctx.drawImage(img, 0, 0, width, height);
1488
+ }
1489
+ else {
1490
+ ctx.drawImage(img, 0, 0, width, height);
1491
+ var imageData = ctx.getImageData(0, 0, width, height);
1492
+ var data = imageData.data;
1493
+ var len = data.length;
1494
+ for (var i = 0; i < len; i += 4) {
1495
+ var alpha = data[i + 3];
1496
+ if (alpha !== 0)
1497
+ data[i + 3] = alpha * opacity;
1498
+ }
1499
+ ctx.putImageData(imageData, 0, 0);
1500
+ }
1501
+ return canvas;
1502
+ };
1503
+
1504
+ export { LuckyScratch, cutRound, opacity };
16
1505
  //# sourceMappingURL=index.esm.js.map