lucky-scratch 1.0.0
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/LICENSE +201 -0
- package/README.md +42 -0
- package/dist/index.cjs.js +17 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +16 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.umd.js +15 -0
- package/dist/index.umd.js.map +1 -0
- package/index.js +1 -0
- package/package.json +64 -0
- package/types/index.d.ts +246 -0
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/index.umd.js')
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lucky-scratch",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "一个基于原生 js 的刮刮卡抽奖插件",
|
|
5
|
+
"main": "dist/index.cjs.js",
|
|
6
|
+
"module": "dist/index.esm.js",
|
|
7
|
+
"unpkg": "dist/index.umd.js",
|
|
8
|
+
"jsdelivr": "dist/index.umd.js",
|
|
9
|
+
"types": "types/index.d.ts",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"dev": "rollup --config rollup.config.dev.js -w",
|
|
12
|
+
"build": "rollup --config rollup.config.build.js"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://100px.net",
|
|
15
|
+
"bugs": "https://github.com/LuckDraw/lucky-scratch/issues",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/LuckDraw/lucky-scratch.git",
|
|
19
|
+
"directory": "packages/lucky-scratch"
|
|
20
|
+
},
|
|
21
|
+
"author": "ldq <ldq404@qq.com>",
|
|
22
|
+
"license": "Apache-2.0",
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"types",
|
|
26
|
+
"index.js"
|
|
27
|
+
],
|
|
28
|
+
"keywords": [
|
|
29
|
+
"刮刮卡",
|
|
30
|
+
"刮奖",
|
|
31
|
+
"抽奖插件",
|
|
32
|
+
"js抽奖",
|
|
33
|
+
"移动端抽奖",
|
|
34
|
+
"canvas抽奖"
|
|
35
|
+
],
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@babel/core": "^7.12.3",
|
|
38
|
+
"@babel/plugin-transform-runtime": "^7.16.4",
|
|
39
|
+
"@babel/preset-env": "^7.12.1",
|
|
40
|
+
"@babel/runtime": "^7.16.3",
|
|
41
|
+
"@rollup/plugin-commonjs": "^16.0.0",
|
|
42
|
+
"@rollup/plugin-eslint": "^8.0.1",
|
|
43
|
+
"@rollup/plugin-json": "^4.1.0",
|
|
44
|
+
"@rollup/plugin-node-resolve": "^10.0.0",
|
|
45
|
+
"@rollup/plugin-typescript": "^6.1.0",
|
|
46
|
+
"@typescript-eslint/parser": "^4.14.0",
|
|
47
|
+
"babel-plugin-external-helpers": "^6.22.0",
|
|
48
|
+
"babel-preset-latest": "^6.24.1",
|
|
49
|
+
"core-js": "^3.19.2",
|
|
50
|
+
"eslint": "^7.18.0",
|
|
51
|
+
"eslint-plugin-prettier": "^3.3.1",
|
|
52
|
+
"prettier": "^2.2.1",
|
|
53
|
+
"rollup": "^2.33.1",
|
|
54
|
+
"rollup-plugin-babel": "^4.4.0",
|
|
55
|
+
"rollup-plugin-delete": "^2.0.0",
|
|
56
|
+
"rollup-plugin-dts": "^3.0.2",
|
|
57
|
+
"rollup-plugin-livereload": "^2.0.0",
|
|
58
|
+
"rollup-plugin-serve": "^1.1.0",
|
|
59
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
60
|
+
"rollup-plugin-typescript2": "^0.30.0",
|
|
61
|
+
"tslib": "^2.8.1",
|
|
62
|
+
"typescript": "^4.0.5"
|
|
63
|
+
}
|
|
64
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
type ImgType = HTMLImageElement | HTMLCanvasElement;
|
|
2
|
+
type ImgItemType = {
|
|
3
|
+
src: string;
|
|
4
|
+
top?: string | number;
|
|
5
|
+
left?: string | number;
|
|
6
|
+
width?: string;
|
|
7
|
+
height?: string;
|
|
8
|
+
formatter?: (img: ImgType) => ImgType;
|
|
9
|
+
$resolve?: Function;
|
|
10
|
+
$reject?: Function;
|
|
11
|
+
};
|
|
12
|
+
type ConfigType = {
|
|
13
|
+
nodeType?: number;
|
|
14
|
+
flag: 'WEB' | 'MP-WX' | 'UNI-H5' | 'UNI-MP' | 'TARO-H5' | 'TARO-MP';
|
|
15
|
+
el?: string;
|
|
16
|
+
divElement?: HTMLDivElement;
|
|
17
|
+
canvasElement?: HTMLCanvasElement;
|
|
18
|
+
ctx: CanvasRenderingContext2D;
|
|
19
|
+
dpr: number;
|
|
20
|
+
handleCssUnit?: (num: number, unit: string) => number;
|
|
21
|
+
rAF?: Function;
|
|
22
|
+
setTimeout: Function;
|
|
23
|
+
setInterval: Function;
|
|
24
|
+
clearTimeout: Function;
|
|
25
|
+
clearInterval: Function;
|
|
26
|
+
beforeCreate?: Function;
|
|
27
|
+
beforeResize?: Function;
|
|
28
|
+
afterResize?: Function;
|
|
29
|
+
beforeInit?: Function;
|
|
30
|
+
afterInit?: Function;
|
|
31
|
+
beforeDraw?: Function;
|
|
32
|
+
afterDraw?: Function;
|
|
33
|
+
afterStart?: Function;
|
|
34
|
+
};
|
|
35
|
+
type UserConfigType = Partial<ConfigType>;
|
|
36
|
+
type Tuple<T, Len extends number, Res extends T[] = []> = Res['length'] extends Len ? Res : Tuple<T, Len, [...Res, T]>;
|
|
37
|
+
|
|
38
|
+
interface WatchOptType {
|
|
39
|
+
handler?: () => Function;
|
|
40
|
+
immediate?: boolean;
|
|
41
|
+
deep?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare class Lucky {
|
|
45
|
+
static version: string;
|
|
46
|
+
protected readonly version: string;
|
|
47
|
+
protected readonly config: ConfigType;
|
|
48
|
+
protected readonly ctx: CanvasRenderingContext2D;
|
|
49
|
+
protected htmlFontSize: number;
|
|
50
|
+
protected rAF: Function;
|
|
51
|
+
protected boxWidth: number;
|
|
52
|
+
protected boxHeight: number;
|
|
53
|
+
protected data: {
|
|
54
|
+
width: string | number;
|
|
55
|
+
height: string | number;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* 公共构造器
|
|
59
|
+
* @param config
|
|
60
|
+
*/
|
|
61
|
+
constructor(config: string | HTMLDivElement | UserConfigType, data: {
|
|
62
|
+
width: string | number;
|
|
63
|
+
height: string | number;
|
|
64
|
+
});
|
|
65
|
+
/**
|
|
66
|
+
* 初始化组件大小/单位
|
|
67
|
+
*/
|
|
68
|
+
protected resize(): void;
|
|
69
|
+
/**
|
|
70
|
+
* 初始化方法
|
|
71
|
+
*/
|
|
72
|
+
protected initLucky(): void;
|
|
73
|
+
/**
|
|
74
|
+
* 鼠标点击事件
|
|
75
|
+
* @param e 事件参数
|
|
76
|
+
*/
|
|
77
|
+
protected handleClick(e: MouseEvent): void;
|
|
78
|
+
/**
|
|
79
|
+
* 根标签的字体大小
|
|
80
|
+
*/
|
|
81
|
+
protected setHTMLFontSize(): void;
|
|
82
|
+
clearCanvas(): void;
|
|
83
|
+
/**
|
|
84
|
+
* 设备像素比
|
|
85
|
+
* window 环境下自动获取, 其余环境手动传入
|
|
86
|
+
*/
|
|
87
|
+
protected setDpr(): void;
|
|
88
|
+
/**
|
|
89
|
+
* 重置盒子和canvas的宽高
|
|
90
|
+
*/
|
|
91
|
+
private resetWidthAndHeight;
|
|
92
|
+
/**
|
|
93
|
+
* 根据 dpr 缩放 canvas 并处理位移
|
|
94
|
+
*/
|
|
95
|
+
protected zoomCanvas(): void;
|
|
96
|
+
/**
|
|
97
|
+
* 从 window 对象上获取一些方法
|
|
98
|
+
*/
|
|
99
|
+
private initWindowFunction;
|
|
100
|
+
isWeb(): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* 异步加载图片并返回图片的几何信息
|
|
103
|
+
* @param src 图片路径
|
|
104
|
+
* @param info 图片信息
|
|
105
|
+
*/
|
|
106
|
+
protected loadImg(src: string, info: ImgItemType, resolveName?: string): Promise<ImgType>;
|
|
107
|
+
/**
|
|
108
|
+
* 公共绘制图片的方法
|
|
109
|
+
* @param imgObj 图片对象
|
|
110
|
+
* @param rectInfo: [x轴位置, y轴位置, 渲染宽度, 渲染高度]
|
|
111
|
+
*/
|
|
112
|
+
protected drawImage(ctx: CanvasRenderingContext2D, imgObj: ImgType, ...rectInfo: [...Tuple<number, 4>, ...Partial<Tuple<number, 4>>]): void;
|
|
113
|
+
/**
|
|
114
|
+
* 计算图片的渲染宽高
|
|
115
|
+
* @param imgObj 图片标签元素
|
|
116
|
+
* @param imgInfo 图片信息
|
|
117
|
+
* @param maxWidth 最大宽度
|
|
118
|
+
* @param maxHeight 最大高度
|
|
119
|
+
* @return [渲染宽度, 渲染高度]
|
|
120
|
+
*/
|
|
121
|
+
protected computedWidthAndHeight(imgObj: ImgType, imgInfo: ImgItemType, maxWidth: number, maxHeight: number): [number, number];
|
|
122
|
+
/**
|
|
123
|
+
* 转换单位
|
|
124
|
+
* @param { string } value 将要转换的值
|
|
125
|
+
* @param { number } denominator 分子
|
|
126
|
+
* @return { number } 返回新的字符串
|
|
127
|
+
*/
|
|
128
|
+
protected changeUnits(value: string, denominator?: number): number;
|
|
129
|
+
/**
|
|
130
|
+
* 获取长度
|
|
131
|
+
* @param length 将要转换的长度
|
|
132
|
+
* @param maxLength 最大长度
|
|
133
|
+
* @return 返回长度
|
|
134
|
+
*/
|
|
135
|
+
protected getLength(length: string | number | undefined, maxLength?: number): number;
|
|
136
|
+
/**
|
|
137
|
+
* 获取相对(居中)X坐标
|
|
138
|
+
* @param width
|
|
139
|
+
* @param col
|
|
140
|
+
*/
|
|
141
|
+
protected getOffsetX(width: number, maxWidth?: number): number;
|
|
142
|
+
protected getOffscreenCanvas(width: number, height: number): {
|
|
143
|
+
_offscreenCanvas: HTMLCanvasElement;
|
|
144
|
+
_ctx: CanvasRenderingContext2D;
|
|
145
|
+
} | void;
|
|
146
|
+
/**
|
|
147
|
+
* 添加一个新的响应式数据 (临时)
|
|
148
|
+
* @param data 数据
|
|
149
|
+
* @param key 属性
|
|
150
|
+
* @param value 新值
|
|
151
|
+
*/
|
|
152
|
+
$set(data: object, key: string | number, value: any): void;
|
|
153
|
+
/**
|
|
154
|
+
* 添加一个属性计算 (临时)
|
|
155
|
+
* @param data 源数据
|
|
156
|
+
* @param key 属性名
|
|
157
|
+
* @param callback 回调函数
|
|
158
|
+
*/
|
|
159
|
+
protected $computed(data: object, key: string, callback: Function): void;
|
|
160
|
+
/**
|
|
161
|
+
* 添加一个观察者 create user watcher
|
|
162
|
+
* @param expr 表达式
|
|
163
|
+
* @param handler 回调函数
|
|
164
|
+
* @param watchOpt 配置参数
|
|
165
|
+
* @return 卸载当前观察者的函数 (暂未返回)
|
|
166
|
+
*/
|
|
167
|
+
protected $watch(expr: string | Function, handler: Function | WatchOptType, watchOpt?: WatchOptType): Function;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
interface LuckyScratchConfig {
|
|
171
|
+
width?: string | number;
|
|
172
|
+
height?: string | number;
|
|
173
|
+
mask?: {
|
|
174
|
+
type?: 'color' | 'image';
|
|
175
|
+
color?: string;
|
|
176
|
+
src?: string;
|
|
177
|
+
};
|
|
178
|
+
scratch?: {
|
|
179
|
+
radius?: number;
|
|
180
|
+
percent?: number;
|
|
181
|
+
};
|
|
182
|
+
onceBeforeStart?: () => boolean | Promise<boolean>;
|
|
183
|
+
beforeStart?: () => boolean | Promise<boolean>;
|
|
184
|
+
start?: () => void;
|
|
185
|
+
end?: () => void;
|
|
186
|
+
success?: (progress: number) => void;
|
|
187
|
+
afterInit?: () => void;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
declare class LuckyScratch extends Lucky {
|
|
191
|
+
private mask;
|
|
192
|
+
private scratch;
|
|
193
|
+
private progress;
|
|
194
|
+
private isScratching;
|
|
195
|
+
private isCompleted;
|
|
196
|
+
disabled: boolean;
|
|
197
|
+
private isFirstScratch;
|
|
198
|
+
private eventsInitialized;
|
|
199
|
+
private onceBeforeStartCallback?;
|
|
200
|
+
private beforeStartCallback?;
|
|
201
|
+
private startCallback?;
|
|
202
|
+
private endCallback?;
|
|
203
|
+
private successCallback?;
|
|
204
|
+
private afterInitCallback?;
|
|
205
|
+
/**
|
|
206
|
+
* 刮刮卡构造器
|
|
207
|
+
* @param config 配置项
|
|
208
|
+
* @param data 抽奖数据
|
|
209
|
+
*/
|
|
210
|
+
constructor(config: UserConfigType, data: LuckyScratchConfig);
|
|
211
|
+
protected initData(data: LuckyScratchConfig): void;
|
|
212
|
+
/**
|
|
213
|
+
* 重写 resize 方法,在尺寸变化后重新绘制蒙层
|
|
214
|
+
*/
|
|
215
|
+
protected resize(): void;
|
|
216
|
+
init(): Promise<void>;
|
|
217
|
+
private draw;
|
|
218
|
+
private handleBindEvents;
|
|
219
|
+
private handleStart;
|
|
220
|
+
private handleMove;
|
|
221
|
+
private handleEnd;
|
|
222
|
+
private drawArc;
|
|
223
|
+
private checkProgress;
|
|
224
|
+
/**
|
|
225
|
+
* 动态设置禁用状态
|
|
226
|
+
* @param disabled 是否禁用
|
|
227
|
+
*/
|
|
228
|
+
setDisabled(disabled: boolean): void;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 切割圆角
|
|
233
|
+
* @param img 将要裁剪的图片对象
|
|
234
|
+
* @param radius 裁剪的圆角半径
|
|
235
|
+
* @returns 返回一个离屏 canvas 用于渲染
|
|
236
|
+
*/
|
|
237
|
+
declare const cutRound: (img: ImgType, radius: number) => ImgType;
|
|
238
|
+
/**
|
|
239
|
+
* 透明度
|
|
240
|
+
* @param img 将要处理的图片对象
|
|
241
|
+
* @param opacity 透明度
|
|
242
|
+
* @returns 返回一个离屏 canvas 用于渲染
|
|
243
|
+
*/
|
|
244
|
+
declare const opacity: (img: ImgType, opacity: number) => ImgType;
|
|
245
|
+
|
|
246
|
+
export { LuckyScratch, cutRound, opacity };
|