index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. module.exports =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/
  6. /******/ // The require function
  7. /******/ function __webpack_require__(moduleId) {
  8. /******/
  9. /******/ // Check if module is in cache
  10. /******/ if(installedModules[moduleId]) {
  11. /******/ return installedModules[moduleId].exports;
  12. /******/ }
  13. /******/ // Create a new module (and put it into the cache)
  14. /******/ var module = installedModules[moduleId] = {
  15. /******/ i: moduleId,
  16. /******/ l: false,
  17. /******/ exports: {}
  18. /******/ };
  19. /******/
  20. /******/ // Execute the module function
  21. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/ // Flag the module as loaded
  24. /******/ module.l = true;
  25. /******/
  26. /******/ // Return the exports of the module
  27. /******/ return module.exports;
  28. /******/ }
  29. /******/
  30. /******/
  31. /******/ // expose the modules object (__webpack_modules__)
  32. /******/ __webpack_require__.m = modules;
  33. /******/
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/
  37. /******/ // define getter function for harmony exports
  38. /******/ __webpack_require__.d = function(exports, name, getter) {
  39. /******/ if(!__webpack_require__.o(exports, name)) {
  40. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/ }
  42. /******/ };
  43. /******/
  44. /******/ // define __esModule on exports
  45. /******/ __webpack_require__.r = function(exports) {
  46. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/ }
  49. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/ };
  51. /******/
  52. /******/ // create a fake namespace object
  53. /******/ // mode & 1: value is a module id, require it
  54. /******/ // mode & 2: merge all properties of value into the ns
  55. /******/ // mode & 4: return value when already ns object
  56. /******/ // mode & 8|1: behave like require
  57. /******/ __webpack_require__.t = function(value, mode) {
  58. /******/ if(mode & 1) value = __webpack_require__(value);
  59. /******/ if(mode & 8) return value;
  60. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/ var ns = Object.create(null);
  62. /******/ __webpack_require__.r(ns);
  63. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/ return ns;
  66. /******/ };
  67. /******/
  68. /******/ // getDefaultExport function for compatibility with non-harmony modules
  69. /******/ __webpack_require__.n = function(module) {
  70. /******/ var getter = module && module.__esModule ?
  71. /******/ function getDefault() { return module['default']; } :
  72. /******/ function getModuleExports() { return module; };
  73. /******/ __webpack_require__.d(getter, 'a', getter);
  74. /******/ return getter;
  75. /******/ };
  76. /******/
  77. /******/ // Object.prototype.hasOwnProperty.call
  78. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/ // __webpack_public_path__
  81. /******/ __webpack_require__.p = "";
  82. /******/
  83. /******/
  84. /******/ // Load entry module and return exports
  85. /******/ return __webpack_require__(__webpack_require__.s = 3);
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ([
  89. /* 0 */
  90. /***/ (function(module, exports) {
  91. module.exports = require("lucky-canvas");
  92. /***/ }),
  93. /* 1 */
  94. /***/ (function(module, exports, __webpack_require__) {
  95. "use strict";
  96. exports.__esModule = true;
  97. exports.getImage = getImage;
  98. var windowWidth = wx.getSystemInfoSync().windowWidth;
  99. var rpx2px = exports.rpx2px = function rpx2px(value) {
  100. if (typeof value === 'string') value = Number(value.replace(/[a-z]*/g, ''));
  101. return windowWidth / 750 * value;
  102. };
  103. var changeUnits = exports.changeUnits = function changeUnits(value) {
  104. return Number(value.replace(/^(\-*[0-9.]*)([a-z%]*)$/, function (value, num, unit) {
  105. switch (unit) {
  106. case 'px':
  107. num *= 1;
  108. break;
  109. case 'rpx':
  110. num = rpx2px(num);
  111. break;
  112. default:
  113. num *= 1;
  114. break;
  115. }
  116. return num;
  117. }));
  118. };
  119. var resolveImage = exports.resolveImage = function resolveImage(e, img, canvas) {
  120. var srcName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'src';
  121. var resolveName = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : '$resolve';
  122. var imgObj = canvas.createImage();
  123. imgObj.onload = function () {
  124. img[resolveName](imgObj);
  125. };
  126. imgObj.src = img[srcName];
  127. };
  128. function getImage() {
  129. var _this = this;
  130. return new Promise(function (resolve, reject) {
  131. wx.canvasToTempFilePath({
  132. canvas: _this.canvas,
  133. success: function success(res) {
  134. return resolve(res);
  135. },
  136. fail: function fail(err) {
  137. return reject(err);
  138. }
  139. });
  140. });
  141. }
  142. /***/ }),
  143. /* 2 */,
  144. /* 3 */
  145. /***/ (function(module, exports, __webpack_require__) {
  146. "use strict";
  147. var _luckyCanvas = __webpack_require__(0);
  148. var _utils = __webpack_require__(1);
  149. Component({
  150. properties: {
  151. width: { type: String, value: '600rpx' },
  152. height: { type: String, value: '600rpx' },
  153. rows: { type: String, optionalTypes: [Number], value: '3' },
  154. cols: { type: String, optionalTypes: [Number], value: '3' },
  155. blocks: { type: Array, value: [] },
  156. prizes: { type: Array, value: [] },
  157. buttons: { type: Array, value: [] },
  158. defaultConfig: { type: Object, value: {} },
  159. defaultStyle: { type: Object, value: {} },
  160. activeStyle: { type: Object, value: {} },
  161. start: { type: null, value: function value() {} },
  162. end: { type: null, value: function value() {} }
  163. },
  164. data: {
  165. isShow: false,
  166. luckyImg: '',
  167. showCanvas: true
  168. },
  169. observers: {
  170. 'prizes.**': function prizes(newData, oldData) {
  171. this.$lucky && (this.$lucky.prizes = newData);
  172. },
  173. 'buttons.**': function buttons(newData, oldData) {
  174. this.$lucky && (this.$lucky.buttons = newData);
  175. }
  176. },
  177. ready: function ready() {
  178. var _this = this;
  179. wx.createSelectorQuery().in(this).select('#lucky-grid').fields({
  180. node: true, size: true
  181. }).exec(function (res) {
  182. if (!res[0] || !res[0].node) {
  183. console.error('lucky-canvas 获取不到 canvas 标签');
  184. return;
  185. }
  186. var canvas = _this.canvas = res[0].node;
  187. var dpr = _this.dpr = wx.getSystemInfoSync().pixelRatio;
  188. var ctx = _this.ctx = canvas.getContext('2d');
  189. var data = _this.data;
  190. canvas.width = res[0].width * dpr;
  191. canvas.height = res[0].height * dpr;
  192. ctx.scale(dpr, dpr);
  193. _this.$lucky = new _luckyCanvas.LuckyGrid({
  194. flag: 'MP-WX',
  195. ctx: ctx,
  196. dpr: dpr,
  197. width: res[0].width,
  198. height: res[0].height,
  199. // rAF: canvas.requestAnimationFrame, // 帧动画真机调试会报错!
  200. setTimeout: setTimeout,
  201. clearTimeout: clearTimeout,
  202. setInterval: setInterval,
  203. clearInterval: clearInterval,
  204. unitFunc: function unitFunc(num, unit) {
  205. return (0, _utils.changeUnits)(num + unit);
  206. },
  207. afterStart: function afterStart() {
  208. // 隐藏图片并显示canvas
  209. _this.$lucky.draw();
  210. _this.setData({
  211. luckyImg: '',
  212. showCanvas: true
  213. });
  214. }
  215. }, {
  216. rows: data.rows,
  217. cols: data.cols,
  218. blocks: data.blocks,
  219. prizes: data.prizes,
  220. buttons: data.buttons,
  221. defaultConfig: data.defaultConfig,
  222. defaultStyle: data.defaultStyle,
  223. activeStyle: data.activeStyle,
  224. start: function start() {
  225. for (var _len = arguments.length, rest = Array(_len), _key = 0; _key < _len; _key++) {
  226. rest[_key] = arguments[_key];
  227. }
  228. _this.triggerEvent.apply(_this, ['start'].concat(rest));
  229. },
  230. end: function end() {
  231. for (var _len2 = arguments.length, rest = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  232. rest[_key2] = arguments[_key2];
  233. }
  234. _this.triggerEvent.apply(_this, ['end'].concat(rest));
  235. _utils.getImage.call(_this).then(function (res) {
  236. _this.setData({ luckyImg: res.tempFilePath });
  237. });
  238. }
  239. });
  240. // 为了保证 onload 回调准确
  241. _this.setData({ isShow: true });
  242. });
  243. },
  244. methods: {
  245. imgBindload: function imgBindload(e) {
  246. var _e$currentTarget$data = e.currentTarget.dataset,
  247. name = _e$currentTarget$data.name,
  248. index = _e$currentTarget$data.index,
  249. i = _e$currentTarget$data.i;
  250. var img = this.data[name][index].imgs[i];
  251. (0, _utils.resolveImage)(e, img, this.canvas);
  252. },
  253. imgBindloadActive: function imgBindloadActive(e) {
  254. var _e$currentTarget$data2 = e.currentTarget.dataset,
  255. name = _e$currentTarget$data2.name,
  256. index = _e$currentTarget$data2.index,
  257. i = _e$currentTarget$data2.i;
  258. var img = this.data[name][index].imgs[i];
  259. (0, _utils.resolveImage)(e, img, this.canvas, 'activeSrc', '$activeResolve');
  260. },
  261. luckyImgLoad: function luckyImgLoad() {
  262. this.setData({ showCanvas: false });
  263. this.$lucky.clearCanvas();
  264. },
  265. handleClickOfImg: function handleClickOfImg(e) {
  266. var _this2 = this;
  267. var _e$changedTouches$ = e.changedTouches[0],
  268. x = _e$changedTouches$.clientX,
  269. y = _e$changedTouches$.clientY;
  270. wx.createSelectorQuery().in(this).select('.lucky-img').fields({
  271. rect: true
  272. }).exec(function (res) {
  273. var _res$ = res[0],
  274. left = _res$.left,
  275. top = _res$.top;
  276. _this2.toPlay(x - left, y - top);
  277. });
  278. },
  279. handleClickOfCanvas: function handleClickOfCanvas(e) {
  280. var _e$changedTouches$2 = e.changedTouches[0],
  281. x = _e$changedTouches$2.x,
  282. y = _e$changedTouches$2.y;
  283. this.toPlay(x, y);
  284. },
  285. toPlay: function toPlay(x, y) {
  286. var _this3 = this;
  287. var ctx = this.ctx;
  288. this.data.buttons.forEach(function (btn) {
  289. if (!btn) return;
  290. ctx.beginPath();
  291. ctx.rect.apply(ctx, _this3.$lucky.getGeometricProperty([btn.x, btn.y, btn.col || 1, btn.row || 1]));
  292. if (!ctx.isPointInPath(x * _this3.dpr, y * _this3.dpr)) return;
  293. // 触发抽奖逻辑
  294. _this3.$lucky.startCallback();
  295. });
  296. },
  297. init: function init() {
  298. var _$lucky;
  299. (_$lucky = this.$lucky).init.apply(_$lucky, arguments);
  300. },
  301. play: function play() {
  302. var _$lucky2;
  303. (_$lucky2 = this.$lucky).play.apply(_$lucky2, arguments);
  304. },
  305. stop: function stop() {
  306. var _$lucky3;
  307. (_$lucky3 = this.$lucky).stop.apply(_$lucky3, arguments);
  308. }
  309. }
  310. });
  311. /***/ })
  312. /******/ ]);
  313. //# sourceMappingURL=index.js.map