lucky-canvas-z
Version:
一个基于原生 js 的(大转盘 / 九宫格 / 老虎机)抽奖插件
1,463 lines (1,430 loc) • 147 kB
JavaScript
(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);
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
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;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
function __spreadArray(to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
}
/**
* 由于部分低版本下的某些 app 可能会缺少某些原型方法, 这里增加兼容
*/
// ie11 不兼容 includes 方法
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function value(valueToFind, fromIndex) {
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
// 1. Let O be ? ToObject(this value).
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If len is 0, return false.
if (len === 0) {
return false;
}
// 4. Let n be ? ToInteger(fromIndex).
// (If fromIndex is undefined, this step produces the value 0.)
var n = fromIndex | 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
function sameValueZero(x, y) {
return x === y || typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y);
}
// 7. Repeat, while k < len
while (k < len) {
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(valueToFind, elementK) is true, return true.
if (sameValueZero(o[k], valueToFind)) {
return true;
}
// c. Increase k by 1.
k++;
}
// 8. Return false
return false;
}
});
}
// vivo x7 下网易云游戏 app 缺少 includes 方法
if (!String.prototype.includes) {
String.prototype.includes = function (search, start) {
if (typeof start !== 'number') {
start = 0;
}
if (start + search.length > this.length) {
return false;
} else {
return this.indexOf(search, start) !== -1;
}
};
}
// vivo x7 下网易云游戏 app 缺少 find 方法
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
value: function value(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];
// 5. Let k be 0.
var k = 0;
// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}
// 7. Return undefined.
return void 0;
}
});
}
/**
* 判断是否是期望的类型
* @param { unknown } param 将要判断的变量
* @param { ...string } types 期望的类型
* @return { boolean } 返回期望是否正确
*/
var isExpectType = function (param) {
var types = [];
for (var _i = 1; _i < arguments.length; _i++) {
types[_i - 1] = arguments[_i];
}
return types.some(function (type) { return Object.prototype.toString.call(param).slice(8, -1).toLowerCase() === type; });
};
var has = function (data, key) {
return Object.prototype.hasOwnProperty.call(data, key);
};
/**
* 移除\n
* @param { string } str 将要处理的字符串
* @return { string } 返回新的字符串
*/
var removeEnter = function (str) {
return [].filter.call(str, function (s) { return s !== '\n'; }).join('');
};
/**
* 把任何数据类型转成数字
* @param num
*/
var getNumber = function (num) {
if (num === null)
return 0;
if (typeof num === 'object')
return NaN;
if (typeof num === 'number')
return num;
if (typeof num === 'string') {
if (num[num.length - 1] === '%') {
return Number(num.slice(0, -1)) / 100;
}
return Number(num);
}
return NaN;
};
/**
* 判断颜色是否有效 (透明色 === 无效)
* @param color 颜色
*/
var hasBackground = function (color) {
if (typeof color !== 'string')
return false;
color = color.toLocaleLowerCase().trim();
if (color === 'transparent')
return false;
if (/^rgba/.test(color)) {
var alpha = /([^\s,]+)\)$/.exec(color);
if (getNumber(alpha) === 0)
return false;
}
return true;
};
/**
* 通过padding计算
* @return { object } block 边框信息
*/
var computePadding = function (block, getLength) {
var _a;
var padding = ((_a = block.padding) === null || _a === void 0 ? void 0 : _a.split(' ').map(function (n) { return getLength(n); })) || [0], paddingTop = 0, paddingBottom = 0, paddingLeft = 0, paddingRight = 0;
switch (padding.length) {
case 1:
paddingTop = paddingBottom = paddingLeft = paddingRight = padding[0];
break;
case 2:
paddingTop = paddingBottom = padding[0];
paddingLeft = paddingRight = padding[1];
break;
case 3:
paddingTop = padding[0];
paddingLeft = paddingRight = padding[1];
paddingBottom = padding[2];
break;
default:
paddingTop = padding[0];
paddingBottom = padding[1];
paddingLeft = padding[2];
paddingRight = padding[3];
}
// 检查是否单独传入值, 并且不是0
var res = { paddingTop: paddingTop, paddingBottom: paddingBottom, paddingLeft: paddingLeft, paddingRight: paddingRight };
for (var key in res) {
// 是否含有这个属性, 并且是数字或字符串
res[key] = has(block, key) && isExpectType(block[key], 'string', 'number')
? getLength(block[key])
: res[key];
}
return [paddingTop, paddingBottom, paddingLeft, paddingRight];
};
/**
* 节流函数
* @param fn 将要处理的函数
* @param wait 时间, 单位为毫秒
* @returns 包装好的节流函数
*/
var throttle = function (fn, wait) {
if (wait === void 0) { wait = 300; }
var timeId = null;
return function () {
var _this = this;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (timeId)
return;
timeId = setTimeout(function () {
fn.apply(_this, args);
clearTimeout(timeId);
timeId = null;
}, wait);
};
};
/**
* 通过概率计算出一个奖品索引
* @param { Array<number | undefined> } rangeArr 概率
* @returns { number } 中奖索引
*/
var computeRange = function (rangeArr) {
var ascendingArr = [];
// 额外增加 map 来优化 ts 的类型推断
var sum = rangeArr.map(function (num) { return Number(num); }).reduce(function (prev, curr) {
if (curr > 0) { // 大于0
var res = prev + curr;
ascendingArr.push(res);
return res;
}
else { // 小于等于0或NaN
ascendingArr.push(NaN);
return prev;
}
}, 0);
var random = Math.random() * sum;
return ascendingArr.findIndex(function (num) { return random <= num; });
};
/**
* 根据宽度分割字符串, 来达到换行的效果
* @param text
* @param maxWidth
* @returns
*/
var splitText = function (ctx, text, getWidth, lineClamp) {
if (lineClamp === void 0) { lineClamp = Infinity; }
// 如果 lineClamp 设置不正确, 则忽略该属性
if (lineClamp <= 0)
lineClamp = Infinity;
var str = '';
var lines = [];
var EndWidth = ctx.measureText('...').width;
for (var i = 0; i < text.length; i++) {
str += text[i];
var currWidth = ctx.measureText(str).width;
var maxWidth = getWidth(lines);
// 如果正在计算最后一行, 则加上三个小点的宽度
if (lineClamp === lines.length + 1)
currWidth += EndWidth;
// 如果已经没有宽度了, 就没有必要再计算了
if (maxWidth < 0)
return lines;
// 如果当前一行的宽度不够了, 则处理下一行
if (currWidth > maxWidth) {
lines.push(str.slice(0, -1));
str = text[i];
}
// 如果现在是最后一行, 则加上三个小点并跳出
if (lineClamp === lines.length) {
lines[lines.length - 1] += '...';
return lines;
}
}
if (str)
lines.push(str);
if (!lines.length)
lines.push(text);
return lines;
};
// 获取一个重新排序的数组
var getSortedArrayByIndex = function (arr, order) {
var map = {}, res = [];
for (var i = 0; i < arr.length; i++) {
map[i] = arr[i];
}
for (var i = 0; i < order.length; i++) {
var curr = map[order[i]];
if (curr)
(res[i] = curr);
}
return res;
};
var name = "lucky-canvas-z";
var version = "1.7.37";
var Dep = /** @class */ (function () {
/**
* 订阅中心构造器
*/
function Dep() {
this.subs = [];
}
/**
* 收集依赖
* @param {*} sub
*/
Dep.prototype.addSub = function (sub) {
// 此处临时使用includes防重复添加
if (!this.subs.includes(sub)) {
this.subs.push(sub);
}
};
/**
* 派发更新
*/
Dep.prototype.notify = function () {
this.subs.forEach(function (sub) {
sub.update();
});
};
return Dep;
}());
var hasProto = '__proto__' in {};
function def(obj, key, val, enumerable) {
Object.defineProperty(obj, key, {
value: val,
enumerable: !!enumerable,
writable: true,
configurable: true
});
}
function parsePath(path) {
path += '.';
var segments = [], segment = '';
for (var i = 0; i < path.length; i++) {
var curr = path[i];
if (/\[|\./.test(curr)) {
segments.push(segment);
segment = '';
}
else if (/\W/.test(curr)) {
continue;
}
else {
segment += curr;
}
}
return function (data) {
return segments.reduce(function (data, key) {
return data[key];
}, data);
};
}
function traverse(value) {
// const seenObjects = new Set()
var dfs = function (data) {
if (!isExpectType(data, 'array', 'object'))
return;
Object.keys(data).forEach(function (key) {
var value = data[key];
dfs(value);
});
};
dfs(value);
// seenObjects.clear()
}
/**
* 重写数组的原型方法
*/
var oldArrayProto = Array.prototype;
var newArrayProto = Object.create(oldArrayProto);
var methods = ['push', 'pop', 'shift', 'unshift', 'sort', 'splice', 'reverse'];
methods.forEach(function (method) {
newArrayProto[method] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var res = oldArrayProto[method].apply(this, args);
var luckyOb = this['__luckyOb__'];
if (['push', 'unshift', 'splice'].includes(method))
luckyOb.walk(this);
luckyOb.dep.notify();
return res;
};
});
var Observer = /** @class */ (function () {
/**
* 观察者构造器
* @param value
*/
function Observer(value) {
// this.value = value
this.dep = new Dep();
// 将响应式对象代理到当前value上面, 并且将当前的enumerable设置为false
def(value, '__luckyOb__', this);
if (Array.isArray(value)) { // 如果是数组, 则重写原型方法
if (hasProto) {
value['__proto__'] = newArrayProto;
}
else {
Object.getOwnPropertyNames(newArrayProto).forEach(function (key) {
def(value, key, newArrayProto[key]);
});
}
}
this.walk(value);
}
Observer.prototype.walk = function (data) {
Object.keys(data).forEach(function (key) {
defineReactive(data, key, data[key]);
});
};
return Observer;
}());
/**
* 处理响应式
* @param { Object | Array } data
*/
function observe(data) {
if (!data || typeof data !== 'object')
return;
var luckyOb;
if ('__luckyOb__' in data) {
luckyOb = data['__luckyOb__'];
}
else {
luckyOb = new Observer(data);
}
return luckyOb;
}
/**
* 重写 setter / getter
* @param {*} data
* @param {*} key
* @param {*} val
*/
function defineReactive(data, key, val) {
var dep = new Dep();
var property = Object.getOwnPropertyDescriptor(data, key);
if (property && property.configurable === false) {
return;
}
var getter = property && property.get;
var setter = property && property.set;
if ((!getter || setter) && arguments.length === 2) {
val = data[key];
}
var childOb = observe(val);
Object.defineProperty(data, key, {
get: function () {
var value = getter ? getter.call(data) : val;
if (Dep.target) {
dep.addSub(Dep.target);
if (childOb) {
childOb.dep.addSub(Dep.target);
}
}
return value;
},
set: function (newVal) {
if (newVal === val)
return;
val = newVal;
if (getter && !setter)
return;
if (setter) {
setter.call(data, newVal);
}
else {
val = newVal;
}
childOb = observe(newVal);
dep.notify();
}
});
}
var uid = 0;
var Watcher = /** @class */ (function () {
/**
* 观察者构造器
* @param {*} $lucky
* @param {*} expr
* @param {*} cb
*/
function Watcher($lucky, expr, cb, options) {
if (options === void 0) { options = {}; }
this.id = uid++;
this.$lucky = $lucky;
this.expr = expr;
this.deep = !!options.deep;
if (typeof expr === 'function') {
this.getter = expr;
}
else {
this.getter = parsePath(expr);
}
this.cb = cb;
this.value = this.get();
}
/**
* 根据表达式获取新值
*/
Watcher.prototype.get = function () {
Dep.target = this;
var value = this.getter.call(this.$lucky, this.$lucky);
// 处理深度监听
if (this.deep) {
traverse(value);
}
Dep.target = null;
return value;
};
/**
* 触发 watcher 更新
*/
Watcher.prototype.update = function () {
// get获取新值
var newVal = this.get();
// 读取之前存储的旧值
var oldVal = this.value;
this.value = newVal;
// 触发 watch 回调
this.cb.call(this.$lucky, newVal, oldVal);
};
return Watcher;
}());
var Lucky = /** @class */ (function () {
/**
* 公共构造器
* @param config
*/
function Lucky(config, data) {
var _this = this;
this.version = version;
this.htmlFontSize = 16;
this.rAF = function () { };
this.boxWidth = 0;
this.boxHeight = 0;
// 兼容代码开始: 为了处理 v1.0.6 版本在这里传入了一个 dom
if (typeof config === "string")
config = { el: config };
else if (config.nodeType === 1)
config = { el: "", divElement: config };
// 这里先野蛮的处理, 等待后续优化, 对外暴露的类型是UserConfigType, 但内部期望是ConfigType
config = config;
this.config = config;
this.data = data;
// 开始初始化
if (!config.flag)
config.flag = "WEB";
if (config.el)
config.divElement = document.querySelector(config.el);
// 如果存在父盒子, 就创建canvas标签
if (config.divElement) {
// 无论盒子内有没有canvas都执行覆盖逻辑
config.canvasElement = document.createElement("canvas");
config.divElement.appendChild(config.canvasElement);
}
// 获取 canvas 上下文
if (config.canvasElement) {
config.ctx = config.canvasElement.getContext("2d");
// 添加版本信息到标签上, 方便定位版本问题
config.canvasElement.setAttribute("package", "".concat(name, "@").concat(version));
config.canvasElement.addEventListener("click", function (e) {
return _this.handleClick(e);
});
}
this.ctx = config.ctx;
// 初始化 window 方法
this.initWindowFunction();
// 如果最后得不到 canvas 上下文那就无法进行绘制
if (!this.config.ctx) {
console.error("无法获取到 CanvasContext2D");
}
// 监听 window 触发 resize 时重置
if (window && typeof window.addEventListener === "function") {
window.addEventListener("resize", throttle(function () { return _this.resize(); }, 300));
}
// 监听异步设置 html 的 fontSize 并重新绘制
if (window && typeof window.MutationObserver === "function") {
new window.MutationObserver(function () {
_this.resize();
}).observe(document.documentElement, { attributes: true });
}
}
/**
* 初始化组件大小/单位
*/
Lucky.prototype.resize = function () {
var _a, _b;
(_b = (_a = this.config).beforeResize) === null || _b === void 0 ? void 0 : _b.call(_a);
// 先初始化 fontSize 以防后面有 rem 单位
this.setHTMLFontSize();
// 拿到 config 即可设置 dpr
this.setDpr();
// 初始化宽高
this.resetWidthAndHeight();
// 根据 dpr 来缩放 canvas
this.zoomCanvas();
};
/**
* 初始化方法
*/
Lucky.prototype.initLucky = function () {
this.resize();
if (!this.boxWidth || !this.boxHeight) {
return console.error("无法获取到宽度或高度");
}
};
/**
* 鼠标点击事件
* @param e 事件参数
*/
Lucky.prototype.handleClick = function (e) { };
/**
* 根标签的字体大小
*/
Lucky.prototype.setHTMLFontSize = function () {
if (!window)
return;
this.htmlFontSize = +window
.getComputedStyle(document.documentElement)
.fontSize.slice(0, -2);
};
// 清空画布
Lucky.prototype.clearCanvas = function () {
var _a = [this.boxWidth, this.boxHeight], width = _a[0], height = _a[1];
this.ctx.clearRect(-width, -height, width * 2, height * 2);
};
/**
* 设备像素比
* window 环境下自动获取, 其余环境手动传入
*/
Lucky.prototype.setDpr = function () {
var config = this.config;
if (config.dpr) ;
else if (window) {
window["dpr"] = config.dpr = window.devicePixelRatio || 1;
}
else if (!config.dpr) {
console.error(config, "未传入 dpr 可能会导致绘制异常");
}
};
/**
* 重置盒子和canvas的宽高
*/
Lucky.prototype.resetWidthAndHeight = function () {
var _a = this, config = _a.config, data = _a.data;
// 如果是浏览器环境并且存在盒子
var boxWidth = 0, boxHeight = 0;
if (config.divElement) {
boxWidth = config.divElement.offsetWidth;
boxHeight = config.divElement.offsetHeight;
}
// 先从 data 里取宽高, 如果 config 上面没有, 就从 style 上面取
this.boxWidth = this.getLength(data.width || config["width"]) || boxWidth;
this.boxHeight =
this.getLength(data.height || config["height"]) || boxHeight;
// 重新把宽高赋给盒子
if (config.divElement) {
config.divElement.style.overflow = "hidden";
config.divElement.style.width = this.boxWidth + "px";
config.divElement.style.height = this.boxHeight + "px";
}
};
/**
* 根据 dpr 缩放 canvas 并处理位移
*/
Lucky.prototype.zoomCanvas = function () {
var _a = this, config = _a.config, ctx = _a.ctx;
var canvasElement = config.canvasElement, dpr = config.dpr;
var _b = [this.boxWidth * dpr, this.boxHeight * dpr], width = _b[0], height = _b[1];
if (!canvasElement)
return;
canvasElement.width = width;
canvasElement.height = height;
canvasElement.style.width = "".concat(width, "px");
canvasElement.style.height = "".concat(height, "px");
canvasElement.style["transform-origin"] = "left top";
canvasElement.style.transform = "scale(".concat(1 / dpr, ")");
ctx.scale(dpr, dpr);
};
/**
* 从 window 对象上获取一些方法
*/
Lucky.prototype.initWindowFunction = function () {
var config = this.config;
if (window) {
this.rAF =
window.requestAnimationFrame ||
window["webkitRequestAnimationFrame"] ||
window["mozRequestAnimationFrame"] ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
config.setTimeout = window.setTimeout;
config.setInterval = window.setInterval;
config.clearTimeout = window.clearTimeout;
config.clearInterval = window.clearInterval;
return;
}
if (config.rAF) {
// 优先使用帧动画
this.rAF = config.rAF;
}
else if (config.setTimeout) {
// 其次使用定时器
var timeout_1 = config.setTimeout;
this.rAF = function (callback) { return timeout_1(callback, 16.7); };
}
else {
// 如果config里面没有提供, 那就假设全局方法存在setTimeout
this.rAF = function (callback) { return setTimeout(callback, 16.7); };
}
};
Lucky.prototype.isWeb = function () {
return ["WEB", "UNI-H5", "TARO-H5"].includes(this.config.flag);
};
/**
* 异步加载图片并返回图片的几何信息
* @param src 图片路径
* @param info 图片信息
*/
Lucky.prototype.loadImg = function (src, info, resolveName) {
var _this = this;
if (resolveName === void 0) { resolveName = "$resolve"; }
return new Promise(function (resolve, reject) {
if (!src)
reject("=> '".concat(info.src, "' \u4E0D\u80FD\u4E3A\u7A7A\u6216\u4E0D\u5408\u6CD5"));
if (_this.config.flag === "WEB") {
var imgObj_1 = new Image();
imgObj_1["crossorigin"] = "anonymous";
imgObj_1.onload = function () { return resolve(imgObj_1); };
imgObj_1.onerror = function () { return reject("=> '".concat(info.src, "' \u56FE\u7247\u52A0\u8F7D\u5931\u8D25")); };
imgObj_1.src = src;
}
else {
// 其余平台向外暴露, 交给外部自行处理
info[resolveName] = resolve;
info["$reject"] = reject;
return;
}
});
};
/**
* 公共绘制图片的方法
* @param imgObj 图片对象
* @param rectInfo: [x轴位置, y轴位置, 渲染宽度, 渲染高度]
*/
Lucky.prototype.drawImage = function (ctx, imgObj) {
var _a, _b;
var rectInfo = [];
for (var _i = 2; _i < arguments.length; _i++) {
rectInfo[_i - 2] = arguments[_i];
}
var drawImg;
var _c = this.config, flag = _c.flag, dpr = _c.dpr;
if (["WEB", "MP-WX"].includes(flag)) {
// 浏览器和新版小程序中直接绘制即可
drawImg = imgObj;
}
else if (["UNI-H5", "UNI-MP", "TARO-H5", "TARO-MP"].includes(flag)) {
drawImg = imgObj.path;
}
else {
// 如果传入了未知的标识
return console.error("意料之外的 flag, 该平台尚未兼容!");
}
var miniProgramOffCtx = (_b = (_a = (drawImg["canvas"] || drawImg)).getContext) === null || _b === void 0 ? void 0 : _b.call(_a, "2d");
if (miniProgramOffCtx && !this.isWeb()) {
rectInfo = rectInfo.map(function (val) { return val * dpr; });
var temp = miniProgramOffCtx.getImageData.apply(miniProgramOffCtx, rectInfo.slice(0, 4));
ctx.putImageData.apply(ctx, __spreadArray([temp], rectInfo.slice(4, 6), false));
}
else {
if (rectInfo.length === 8) {
rectInfo = rectInfo.map(function (val, index) {
return index < 4 ? val * dpr : val;
});
}
// 尝试捕获错误
try {
ctx.drawImage.apply(ctx, __spreadArray([drawImg], rectInfo, false));
}
catch (err) {
/**
* TODO: safari浏览器下, init() 会出现奇怪的报错
* IndexSizeError: The index is not in the allowed range
* 但是这个报错并不影响实际的绘制, 目前先放一放, 等待有缘人
*/
// console.log(err)
}
}
};
/**
* 计算图片的渲染宽高
* @param imgObj 图片标签元素
* @param imgInfo 图片信息
* @param maxWidth 最大宽度
* @param maxHeight 最大高度
* @return [渲染宽度, 渲染高度]
*/
Lucky.prototype.computedWidthAndHeight = function (imgObj, imgInfo, maxWidth, maxHeight) {
// 根据配置的样式计算图片的真实宽高
if (!imgInfo.width && !imgInfo.height) {
// 如果没有配置宽高, 则使用图片本身的宽高
return [imgObj.width, imgObj.height];
}
else if (imgInfo.width && !imgInfo.height) {
// 如果只填写了宽度, 没填写高度
var trueWidth = this.getLength(imgInfo.width, maxWidth);
// 那高度就随着宽度进行等比缩放
return [trueWidth, imgObj.height * (trueWidth / imgObj.width)];
}
else if (!imgInfo.width && imgInfo.height) {
// 如果只填写了宽度, 没填写高度
var trueHeight = this.getLength(imgInfo.height, maxHeight);
// 那宽度就随着高度进行等比缩放
return [imgObj.width * (trueHeight / imgObj.height), trueHeight];
}
// 如果宽度和高度都填写了, 就如实计算
return [
this.getLength(imgInfo.width, maxWidth),
this.getLength(imgInfo.height, maxHeight),
];
};
/**
* 转换单位
* @param { string } value 将要转换的值
* @param { number } denominator 分子
* @return { number } 返回新的字符串
*/
Lucky.prototype.changeUnits = function (value, denominator) {
var _this = this;
if (denominator === void 0) { denominator = 1; }
var config = this.config;
return Number(value.replace(/^([-]*[0-9.]*)([a-z%]*)$/, function (val, num, unit) {
var handleCssUnit = {
"%": function (n) { return n * (denominator / 100); },
px: function (n) { return n * 1; },
rem: function (n) { return n * _this.htmlFontSize; },
vw: function (n) { return (n / 100) * window.innerWidth; },
}[unit];
if (handleCssUnit)
return handleCssUnit(num);
// 如果找不到默认单位, 就交给外面处理
var otherHandleCssUnit = config.handleCssUnit || config["unitFunc"];
return otherHandleCssUnit ? otherHandleCssUnit(num, unit) : num;
}));
};
/**
* 获取长度
* @param length 将要转换的长度
* @param maxLength 最大长度
* @return 返回长度
*/
Lucky.prototype.getLength = function (length, maxLength) {
if (isExpectType(length, "number"))
return length;
if (isExpectType(length, "string"))
return this.changeUnits(length, maxLength);
return 0;
};
/**
* 获取相对(居中)X坐标
* @param width
* @param col
*/
Lucky.prototype.getOffsetX = function (width, maxWidth) {
if (maxWidth === void 0) { maxWidth = 0; }
return (maxWidth - width) / 2;
};
Lucky.prototype.getOffscreenCanvas = function (width, height) {
if (!has(this, "_offscreenCanvas")) {
if (window && window.document && this.config.flag === "WEB") {
this["_offscreenCanvas"] = document.createElement("canvas");
}
else {
this["_offscreenCanvas"] = this.config["offscreenCanvas"];
}
if (!this["_offscreenCanvas"])
return console.error("离屏 Canvas 无法渲染!");
}
var dpr = this.config.dpr;
var _offscreenCanvas = this["_offscreenCanvas"];
_offscreenCanvas.width = (width || 300) * dpr;
_offscreenCanvas.height = (height || 150) * dpr;
var _ctx = _offscreenCanvas.getContext("2d");
_ctx.clearRect(0, 0, width, height);
_ctx.scale(dpr, dpr);
_ctx["dpr"] = dpr;
return { _offscreenCanvas: _offscreenCanvas, _ctx: _ctx };
};
/**
* 添加一个新的响应式数据 (临时)
* @param data 数据
* @param key 属性
* @param value 新值
*/
Lucky.prototype.$set = function (data, key, value) {
if (!data || typeof data !== "object")
return;
defineReactive(data, key, value);
};
/**
* 添加一个属性计算 (临时)
* @param data 源数据
* @param key 属性名
* @param callback 回调函数
*/
Lucky.prototype.$computed = function (data, key, callback) {
var _this = this;
Object.defineProperty(data, key, {
get: function () {
return callback.call(_this);
},
});
};
/**
* 添加一个观察者 create user watcher
* @param expr 表达式
* @param handler 回调函数
* @param watchOpt 配置参数
* @return 卸载当前观察者的函数 (暂未返回)
*/
Lucky.prototype.$watch = function (expr, handler, watchOpt) {
if (watchOpt === void 0) { watchOpt = {}; }
if (typeof handler === "object") {
watchOpt = handler;
handler = watchOpt.handler;
}
// 创建 user watcher
var watcher = new Watcher(this, expr, handler, watchOpt);
// 判断是否需要初始化时触发回调
if (watchOpt.immediate) {
handler.call(this, watcher.value);
}
// 返回一个卸载当前观察者的函数
return function unWatchFn() { };
};
Lucky.version = version;
return Lucky;
}());
/**
* 转换为运算角度
* @param { number } deg 数学角度
* @return { number } 运算角度
*/
var getAngle = function (deg) {
return (Math.PI / 180) * deg;
};
/**
* 根据角度计算圆上的点
* @param { number } deg 运算角度
* @param { number } r 半径
* @return { Array<number> } 坐标[x, y]
*/
var getArcPointerByDeg = function (deg, r) {
return [+(Math.cos(deg) * r).toFixed(8), +(Math.sin(deg) * r).toFixed(8)];
};
// 使用 arc 绘制扇形
var fanShapedByArc = function (ctx, minRadius, maxRadius, start, end, gutter) {
ctx.beginPath();
var maxGutter = getAngle((90 / Math.PI / maxRadius) * gutter);
var maxStart = start + maxGutter;
var maxEnd = end - maxGutter;
ctx.arc(0, 0, maxRadius, maxStart, maxEnd, false);
// 如果 getter 比按钮短就绘制圆弧, 反之计算新的坐标点
// if (minEnd > minStart) {
// ctx.arc(0, 0, minRadius, minEnd, minStart, true)
// } else {
ctx.lineTo.apply(ctx, getArcPointerByDeg((start + end) / 2, gutter / 2 / Math.abs(Math.sin((start - end) / 2))));
// }
ctx.closePath();
};
// 使用 arc 绘制圆角矩形
var roundRectByArc = function (ctx) {
var _a = [];
for (var _i = 1; _i < arguments.length; _i++) {
_a[_i - 1] = arguments[_i];
}
var x = _a[0], y = _a[1], w = _a[2], h = _a[3], r = _a[4];
var min = Math.min(w, h), PI = Math.PI;
if (r > min / 2)
r = min / 2;
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.arc(x + w - r, y + r, r, -PI / 2, 0);
ctx.lineTo(x + w, y + h - r);
ctx.arc(x + w - r, y + h - r, r, 0, PI / 2);
ctx.lineTo(x + r, y + h);
ctx.arc(x + r, y + h - r, r, PI / 2, PI);
ctx.lineTo(x, y + r);
ctx.arc(x + r, y + r, r, PI, -PI / 2);
ctx.closePath();
};
/**
* 创建线性渐变色
*/
var getLinearGradient = function (ctx, x, y, w, h, background) {
var context = /linear-gradient\((.+)\)/.exec(background)[1]
.split(",") // 根据逗号分割
.map(function (text) { return text.trim(); }); // 去除两边空格
var deg = context.shift(), direction = [0, 0, 0, 0];
// 通过起始点和角度计算渐变终点的坐标点, 这里感谢泽宇大神提醒我使用勾股定理....
if (deg.includes("deg")) {
deg = deg.slice(0, -3) % 360;
// 根据4个象限定义起点坐标, 根据45度划分8个区域计算终点坐标
var getLenOfTanDeg = function (deg) { return Math.tan((deg / 180) * Math.PI); };
if (deg >= 0 && deg < 45)
direction = [x, y + h, x + w, y + h - w * getLenOfTanDeg(deg - 0)];
else if (deg >= 45 && deg < 90)
direction = [x, y + h, x + w - h * getLenOfTanDeg(deg - 45), y];
else if (deg >= 90 && deg < 135)
direction = [x + w, y + h, x + w - h * getLenOfTanDeg(deg - 90), y];
else if (deg >= 135 && deg < 180)
direction = [x + w, y + h, x, y + w * getLenOfTanDeg(deg - 135)];
else if (deg >= 180 && deg < 225)
direction = [x + w, y, x, y + w * getLenOfTanDeg(deg - 180)];
else if (deg >= 225 && deg < 270)
direction = [x + w, y, x + h * getLenOfTanDeg(deg - 225), y + h];
else if (deg >= 270 && deg < 315)
direction = [x, y, x + h * getLenOfTanDeg(deg - 270), y + h];
else if (deg >= 315 && deg < 360)
direction = [x, y, x + w, y + h - w * getLenOfTanDeg(deg - 315)];
}
// 创建四个简单的方向坐标
else if (deg.includes("top"))
direction = [x, y + h, x, y];
else if (deg.includes("bottom"))
direction = [x, y, x, y + h];
else if (deg.includes("left"))
direction = [x + w, y, x, y];
else if (deg.includes("right"))
direction = [x, y, x + w, y];
// 创建线性渐变必须使用整数坐标
var gradient = ctx.createLinearGradient.apply(ctx, direction.map(function (n) { return n >> 0; }));
// 这里后期重构, 先用any代替
return context.reduce(function (gradient, item, index) {
var info = item.split(" ");
if (info.length === 1)
gradient.addColorStop(index, info[0]);
else if (info.length === 2)
gradient.addColorStop.apply(gradient, info);
return gradient;
}, gradient);
};
// // 根据三点画圆弧
// export const drawRadian = (
// ctx: CanvasRenderingContext2D,
// r: number,
// start: number,
// end: number,
// direction: boolean = true
// ) => {
// // 如果角度大于等于180度, 则分两次绘制, 因为 arcTo 无法绘制180度的圆弧
// if (Math.abs(end - start).toFixed(8) >= getAngle(180).toFixed(8)) {
// let middle = (end + start) / 2
// if (direction) {
// drawRadian(ctx, r, start, middle, direction)
// drawRadian(ctx, r, middle, end, direction)
// } else {
// drawRadian(ctx, r, middle, end, direction)
// drawRadian(ctx, r, start, middle, direction)
// }
// return false
// }
// // 如果方法相反, 则交换起点和终点
// if (!direction) [start, end] = [end, start]
// const [x1, y1] = getArcPointerByDeg(start, r)
// const [x2, y2] = getArcPointerByDeg(end, r)
// const [k1, b1] = getTangentByPointer(x1, y1)
// const [k2, b2] = getTangentByPointer(x2, y2)
// // 计算两条切线的交点
// let x0 = (b2 - b1) / (k1 - k2)
// let y0 = (k2 * b1 - k1 * b2) / (k2 - k1)
// // 如果有任何一条切线垂直于x轴, 则斜率不存在
// if (isNaN(x0)) {
// Math.abs(x1) === +r.toFixed(8) && (x0 = x1)
// Math.abs(x2) === +r.toFixed(8) && (x0 = x2)
// }
// if (k1 === Infinity || k1 === -Infinity) {
// y0 = k2 * x0 + b2
// }
// else if (k2 === Infinity || k2 === -Infinity) {
// y0 = k1 * x0 + b1
// }
// ctx.lineTo(x1, y1)
// // 微信小程序下 arcTo 在安卓真机下绘制有 bug
// ctx.arcTo(x0, y0, x2, y2, r)
// }
// // 使用 arcTo 绘制扇形 (弃用)
// export const drawSectorByArcTo = (
// ctx: CanvasRenderingContext2D,
// minRadius: number,
// maxRadius: number,
// start: number,
// end: number,
// gutter: number,
// ) => {
// if (!minRadius) minRadius = gutter
// // 内外圆弧分别进行等边缩放
// let maxGutter = getAngle(90 / Math.PI / maxRadius * gutter)
// let minGutter = getAngle(90 / Math.PI / minRadius * gutter)
// let maxStart = start + maxGutter
// let maxEnd = end - maxGutter
// let minStart = start + minGutter
// let minEnd = end - minGutter
// ctx.beginPath()
// ctx.moveTo(...getArcPointerByDeg(maxStart, maxRadius))
// drawRadian(ctx, maxRadius, maxStart, maxEnd, true)
// // 如果 getter 比按钮短就绘制圆弧, 反之计算新的坐标点
// if (minEnd > minStart) {
// drawRadian(ctx, minRadius, minStart, minEnd, false)
// } else {
// ctx.lineTo(
// ...getArcPointerByDeg(
// (start + end) / 2,
// gutter / 2 / Math.abs(Math.sin((start - end) / 2))
// )
// )
// }
// ctx.closePath()
// }
// // 使用 arcTo 绘制圆角矩形 (弃用)
// export const roundRectByArcTo = (
// ctx: CanvasRenderingContext2D,
// ...[x, y, w, h, r]: number[]
// ) => {
// let min = Math.min(w, h)
// if (r > min / 2) r = min / 2
// ctx.beginPath()
// ctx.moveTo(x + r, y)
// ctx.lineTo(x + r, y)
// ctx.lineTo(x + w - r, y)
// ctx.arcTo(x + w, y, x + w, y + r, r)
// ctx.lineTo(x + w, y + h - r)
// ctx.arcTo(x + w, y + h, x + w - r, y + h, r)
// ctx.lineTo(x + r, y + h)
// ctx.arcTo(x, y + h, x, y + h - r, r)
// ctx.lineTo(x, y + r)
// ctx.arcTo(x, y, x + r, y, r)
// }
/**
* 缓动函数
* t: current time(当前时间)
* b: beginning value(初始值)
* c: change in value(变化量)
* d: duration(持续时间)
*
* 感谢张鑫旭大佬 https://github.com/zhangxinxu/Tween
*/
// 二次方的缓动
var quad = {
easeIn: function (t, b, c, d) {
if (t >= d)
t = d;
return c * (t /= d) * t + b;
},
easeOut: function (t, b, c, d) {
if (t >= d)
t = d;
return -c * (t /= d) * (t - 2) + b;
}
};
var LuckyWheel = /** @class */ (function (_super) {
__extends(LuckyWheel, _super);
/**
* 大转盘构造器
* @param config 配置项
* @param data 抽奖数据
*/
function LuckyWheel(config, data) {
var _this = this;
var _a;
_this = _super.call(this, config, {
width: data.width,
height: data.height,
}) || this;
_this.blocks = [];
_this.prizes = [];
_this.buttons = [];
_this.defaultConfig = {};
_this.defaultStyle = {};
_this._defaultConfig = {};
_this._defaultStyle = {};
_this.Radius = 0; // 大转盘半径
_this.prizeRadius = 0; // 奖品区域半径
_this.prizeDeg = 0; // 奖品数学角度
_this.prizeAng = 0; // 奖品运算角度
_this.rotateDeg = 0; // 转盘旋转角度
_this.maxBtnRadius = 0; // 最大按钮半径
_this.startTime = 0; // 开始时间戳
_this.endTime = 0; // 停止时间戳
_this.stopDeg = 0; // 刻舟求剑
_this.endDeg = 0; // 停止角度
_this.FPS = 16.6; // 屏幕刷新率
_this.activeIndices = new Set(); // 改用集合存储多选索引
/**
* 游戏当前的阶段
* step = 0 时, 游戏尚未开始
* step = 1 时, 此时处于加速阶段
* step = 2 时, 此时处于匀速阶段
* step = 3 时, 此时处于减速阶段
*/
_this.step = 0;
_this.ImageCache = new Map();
_this.initData(data);
_this.initWatch();
_this.initComputed();
// 创建前回调函数
(_a = config.beforeCreate) === null || _a === void 0 ? void 0 : _a.call(_this);
// 首次初始化
_this.init();
return _this;
}
LuckyWheel.prototype.resize = function () {
var _a, _b;
_super.prototype.resize.call(this);
this.Radius = Math.min(this.boxWidth, this.boxHeight) / 2;
this.ctx.translate(this.Radius, this.Radius);
this.draw();
(_b = (_a = this.config).afterResize) === null || _b === void 0 ? void 0 : _b.call(_a);
};
LuckyWheel.prototype.initLucky = function () {
this.Radius = 0;
this.prizeRadius = 0;
this.prizeDeg = 0;
this.prizeAng = 0;
this.rotateDeg = 0;
this.maxBtnRadius = 0;
this.startTime = 0;
this.endTime = 0;
this.stopDeg = 0;
this.endDeg = 0;
this.FPS = 16.6;
this.prizeFlag = -1;
this.step = 0;
_super.prototype.initLucky.call(this);
};
/**
* 初始化数据
* @param data
*/
LuckyWheel.prototype.initData = function (data) {
this.$set(this, "width", data.width);
this.$set(this, "height", data.height);
this.$set(this, "blocks", data.blocks || []);
this.$set(this, "prizes", data.prizes || []);
this.$set(this, "buttons", data.buttons || []);
this.$set(this, "defaultConfig", data.defaultConfig || {});
this.$set(this, "defaultStyle", data.defaultStyle || {});
this.$set(this, "startCallback", data.start);
this.$set(this, "endCallback", data.end);
this.$set(this, "selectCallback", data.select);
};
/**
* 初始化属性计算
*/
LuckyWheel.prototype.initComputed = function () {
var _this = this;
// 默认配置
this.$computed(this, "_defaultConfig", function () {
var config = __assign({ gutter: "0px", offsetDegree: 0, speed: 20, autoAngle: true, canSelect: false, speedFunction: "quad", accelerationTime: 2500, decelerationTime: 2500, stopRange: 0 }, _this.defaultConfig);
return config;
});
// 默认样式
this.$computed(this, "_defaultStyle", function () {
var style = __assign({ fontSize: "18px", fontColor: "#000", fontStyle: "sans-serif", fontWeight: "400", background: "rgba(0,0,0,0)", wordWrap: true, lengthLimit: "90%", activeBackground: "#FFF9C4", activeBorder: "#FFA000", activeFontColor: "#333", activeBorderWidth: "2px", circleVisible: true, circleRadius: 6, circleColor: "", fontVisible: true }, _this.defaultStyle);
return style;
});
};
/**
* 初始化观察者
*/
LuckyWheel.prototype.initWatch = function () {
var _this = this;
// 重置宽度
this.$watch("width", function (newVal) {
_this.data.width = newVal;
_this.resize();
});
// 重置高度
this.$watch("height", function (newVal) {
_this.data.height = newVal;
_this.resize();
});
// 观察 blocks 变化收集图片
this.$watch("blocks", function (newData) {
_this.initImageCache();
}, { deep: true });
// 观察 prizes 变化收集图片
this.$watch("prizes", function (newData) {
_this.initImageCache();
}, { deep: true });
// 观察 buttons 变化收集图片
this.$watch("buttons", function (newData) {
_this.initImageCache();
}, { deep: true });
this.$watch("defaultConfig", function () { return _this.draw(); }, { deep: true });
this.$watch("defaultStyle", function () { return _this.draw(); }, { deep: true });
this.$watch("startCallback", function () { return _this.init(); });
this.$watch("endCallback", function () { return _this.init(); });
};
/**
* 初始化 canvas 抽奖
*/
LuckyWheel.prototype.init = function () {
var _a, _b;
return __awaiter(this, void 0, void 0, function () {
var config;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
this.initLucky();
config = this.config;
// 初始化前回调函数
(_a = config.beforeInit) === null || _a === void 0 ? void 0 : _a.call(this);
this.draw(); // 先画一次, 防止闪烁
this.draw(); // 再画一次, 拿到正确的按钮轮廓
// 异步加载图片
return [4 /*yield*/, this.initImageCache()];
case 1:
// 异步加载图片
_c.sent();
// 初始化后回调函数
(_b = config.afterInit) === null || _b === void 0 ? void 0 : _b.call(this);
return [2 /*return*/];
}
});
});
};
LuckyWheel.prototype.initImageCache = function () {
var _this = this;
return new Promise(function (resolve) {
var willUpdateImgs = {
blocks: _this.blocks.map(function (block) { return block.imgs; }),
prizes: _this.prizes.map(function (prize) { return prize.imgs; }),
buttons: _this.buttons.map(function (btn) { return btn.imgs; }),
};
Object.keys(willUpdateImgs).forEach(function (imgName) {
var willUpdate = willUpdateImgs[imgName];
// 循环遍历所有图片
var allPromise = [];
willUpdate &&
willUpdate.forEach(function (imgs, cellIndex) {
imgs &&