1
0

index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 = 2);
  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. /***/ (function(module, exports, __webpack_require__) {
  145. "use strict";
  146. var _luckyCanvas = __webpack_require__(0);
  147. var _utils = __webpack_require__(1);
  148. Component({
  149. properties: {
  150. width: { type: String, value: '600rpx' },
  151. height: { type: String, value: '600rpx' },
  152. blocks: { type: Array, value: [] },
  153. prizes: { type: Array, value: [] },
  154. buttons: { type: Array, value: [] },
  155. defaultConfig: { type: Object, value: {} },
  156. defaultStyle: { type: Object, value: {} },
  157. start: { type: null, value: function value() {} },
  158. end: { type: null, value: function value() {} }
  159. },
  160. data: {
  161. isShow: false,
  162. luckyImg: '',
  163. showCanvas: true
  164. },
  165. observers: {
  166. 'blocks.**': function blocks(newData, oldData) {
  167. this.$lucky && (this.$lucky.blocks = newData);
  168. },
  169. 'prizes.**': function prizes(newData, oldData) {
  170. this.$lucky && (this.$lucky.prizes = newData);
  171. },
  172. 'buttons.**': function buttons(newData, oldData) {
  173. this.$lucky && (this.$lucky.buttons = newData);
  174. }
  175. },
  176. ready: function ready() {
  177. var _this = this;
  178. wx.createSelectorQuery().in(this).select('#lucky-wheel').fields({
  179. node: true, size: true
  180. }).exec(function (res) {
  181. if (!res[0] || !res[0].node) {
  182. return console.error('lucky-canvas 获取不到 canvas 标签');
  183. }
  184. var canvas = _this.canvas = res[0].node;
  185. var dpr = _this.dpr = wx.getSystemInfoSync().pixelRatio;
  186. var ctx = _this.ctx = canvas.getContext('2d');
  187. var data = _this.data;
  188. canvas.width = res[0].width * dpr;
  189. canvas.height = res[0].height * dpr;
  190. ctx.scale(dpr, dpr);
  191. _this.$lucky = new _luckyCanvas.LuckyWheel({
  192. flag: 'MP-WX',
  193. ctx: ctx,
  194. dpr: dpr,
  195. width: res[0].width,
  196. height: res[0].height,
  197. // rAF: canvas.requestAnimationFrame, // 帧动画真机调试会报错!
  198. setTimeout: setTimeout,
  199. clearTimeout: clearTimeout,
  200. setInterval: setInterval,
  201. clearInterval: clearInterval,
  202. unitFunc: function unitFunc(num, unit) {
  203. return (0, _utils.changeUnits)(num + unit);
  204. },
  205. beforeCreate: function beforeCreate() {
  206. var Radius = Math.min(this.config.width, this.config.height) / 2;
  207. // 设置坐标轴
  208. ctx.translate(Radius, Radius);
  209. },
  210. beforeInit: function beforeInit() {
  211. // 重置坐标轴
  212. ctx.translate(-this.Radius, -this.Radius);
  213. },
  214. afterStart: function afterStart() {
  215. // 隐藏图片并显示canvas
  216. _this.$lucky.draw();
  217. _this.setData({
  218. luckyImg: '',
  219. showCanvas: true
  220. });
  221. }
  222. }, {
  223. blocks: data.blocks,
  224. prizes: data.prizes,
  225. buttons: data.buttons,
  226. defaultConfig: data.defaultConfig,
  227. defaultStyle: data.defaultStyle,
  228. start: function start() {
  229. for (var _len = arguments.length, rest = Array(_len), _key = 0; _key < _len; _key++) {
  230. rest[_key] = arguments[_key];
  231. }
  232. _this.triggerEvent.apply(_this, ['start'].concat(rest));
  233. },
  234. end: function end() {
  235. for (var _len2 = arguments.length, rest = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  236. rest[_key2] = arguments[_key2];
  237. }
  238. _this.triggerEvent.apply(_this, ['end'].concat(rest));
  239. _utils.getImage.call(_this).then(function (res) {
  240. _this.setData({ luckyImg: res.tempFilePath });
  241. });
  242. }
  243. });
  244. // 为了保证 onload 回调准确
  245. _this.setData({ isShow: true });
  246. });
  247. },
  248. methods: {
  249. imgBindload: function imgBindload(e) {
  250. var _e$currentTarget$data = e.currentTarget.dataset,
  251. name = _e$currentTarget$data.name,
  252. index = _e$currentTarget$data.index,
  253. i = _e$currentTarget$data.i;
  254. var img = this.data[name][index].imgs[i];
  255. (0, _utils.resolveImage)(e, img, this.canvas);
  256. },
  257. luckyImgLoad: function luckyImgLoad() {
  258. this.setData({ showCanvas: false });
  259. this.$lucky.clearCanvas();
  260. },
  261. handleClickOfImg: function handleClickOfImg(e) {
  262. var _this2 = this;
  263. var _e$changedTouches$ = e.changedTouches[0],
  264. x = _e$changedTouches$.clientX,
  265. y = _e$changedTouches$.clientY;
  266. wx.createSelectorQuery().in(this).select('.lucky-img').fields({
  267. rect: true
  268. }).exec(function (res) {
  269. var _res$ = res[0],
  270. left = _res$.left,
  271. top = _res$.top;
  272. _this2.toPlay(x - left, y - top);
  273. });
  274. },
  275. handleClickOfCanvas: function handleClickOfCanvas(e) {
  276. var _e$changedTouches$2 = e.changedTouches[0],
  277. x = _e$changedTouches$2.x,
  278. y = _e$changedTouches$2.y;
  279. this.toPlay(x, y);
  280. },
  281. toPlay: function toPlay(x, y) {
  282. var ctx = this.ctx;
  283. ctx.beginPath();
  284. ctx.arc(0, 0, this.$lucky.maxBtnRadius, 0, Math.PI * 2, false);
  285. if (!ctx.isPointInPath(x * this.dpr, y * this.dpr)) return;
  286. // 触发抽奖逻辑
  287. this.$lucky.startCallback();
  288. },
  289. init: function init() {
  290. var _$lucky;
  291. (_$lucky = this.$lucky).init.apply(_$lucky, arguments);
  292. },
  293. play: function play() {
  294. var _$lucky2;
  295. (_$lucky2 = this.$lucky).play.apply(_$lucky2, arguments);
  296. },
  297. stop: function stop() {
  298. var _$lucky3;
  299. (_$lucky3 = this.$lucky).stop.apply(_$lucky3, arguments);
  300. }
  301. }
  302. });
  303. /***/ })
  304. /******/ ]);
  305. //# sourceMappingURL=index.js.map