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