lucky-scratch 1.1.1 → 1.1.2

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.
@@ -0,0 +1,2 @@
1
+ export { default as LuckyScratch } from './lib/scratch';
2
+ export { cutRound, opacity } from './utils/image';
@@ -0,0 +1,128 @@
1
+ import '../utils/polyfill';
2
+ import { ConfigType, UserConfigType, ImgItemType, ImgType, Tuple } from '../types/index';
3
+ import { WatchOptType } from '../observer/watcher';
4
+ export default class Lucky {
5
+ static version: string;
6
+ protected readonly version: string;
7
+ protected readonly config: ConfigType;
8
+ protected readonly ctx: CanvasRenderingContext2D;
9
+ protected htmlFontSize: number;
10
+ protected rAF: Function;
11
+ protected boxWidth: number;
12
+ protected boxHeight: number;
13
+ protected data: {
14
+ width: string | number;
15
+ height: string | number;
16
+ };
17
+ /**
18
+ * 公共构造器
19
+ * @param config
20
+ */
21
+ constructor(config: string | HTMLDivElement | UserConfigType, data: {
22
+ width: string | number;
23
+ height: string | number;
24
+ });
25
+ /**
26
+ * 初始化组件大小/单位
27
+ */
28
+ protected resize(): void;
29
+ /**
30
+ * 初始化方法
31
+ */
32
+ protected initLucky(): void;
33
+ /**
34
+ * 鼠标点击事件
35
+ * @param e 事件参数
36
+ */
37
+ protected handleClick(e: MouseEvent): void;
38
+ /**
39
+ * 根标签的字体大小
40
+ */
41
+ protected setHTMLFontSize(): void;
42
+ clearCanvas(): void;
43
+ /**
44
+ * 设备像素比
45
+ * window 环境下自动获取, 其余环境手动传入
46
+ */
47
+ protected setDpr(): void;
48
+ /**
49
+ * 重置盒子和canvas的宽高
50
+ */
51
+ private resetWidthAndHeight;
52
+ /**
53
+ * 根据 dpr 缩放 canvas 并处理位移
54
+ */
55
+ protected zoomCanvas(): void;
56
+ /**
57
+ * 从 window 对象上获取一些方法
58
+ */
59
+ private initWindowFunction;
60
+ isWeb(): boolean;
61
+ /**
62
+ * 异步加载图片并返回图片的几何信息
63
+ * @param src 图片路径
64
+ * @param info 图片信息
65
+ */
66
+ protected loadImg(src: string, info: ImgItemType, resolveName?: string): Promise<ImgType>;
67
+ /**
68
+ * 公共绘制图片的方法
69
+ * @param imgObj 图片对象
70
+ * @param rectInfo: [x轴位置, y轴位置, 渲染宽度, 渲染高度]
71
+ */
72
+ protected drawImage(ctx: CanvasRenderingContext2D, imgObj: ImgType, ...rectInfo: [...Tuple<number, 4>, ...Partial<Tuple<number, 4>>]): void;
73
+ /**
74
+ * 计算图片的渲染宽高
75
+ * @param imgObj 图片标签元素
76
+ * @param imgInfo 图片信息
77
+ * @param maxWidth 最大宽度
78
+ * @param maxHeight 最大高度
79
+ * @return [渲染宽度, 渲染高度]
80
+ */
81
+ protected computedWidthAndHeight(imgObj: ImgType, imgInfo: ImgItemType, maxWidth: number, maxHeight: number): [number, number];
82
+ /**
83
+ * 转换单位
84
+ * @param { string } value 将要转换的值
85
+ * @param { number } denominator 分子
86
+ * @return { number } 返回新的字符串
87
+ */
88
+ protected changeUnits(value: string, denominator?: number): number;
89
+ /**
90
+ * 获取长度
91
+ * @param length 将要转换的长度
92
+ * @param maxLength 最大长度
93
+ * @return 返回长度
94
+ */
95
+ protected getLength(length: string | number | undefined, maxLength?: number): number;
96
+ /**
97
+ * 获取相对(居中)X坐标
98
+ * @param width
99
+ * @param col
100
+ */
101
+ protected getOffsetX(width: number, maxWidth?: number): number;
102
+ protected getOffscreenCanvas(width: number, height: number): {
103
+ _offscreenCanvas: HTMLCanvasElement;
104
+ _ctx: CanvasRenderingContext2D;
105
+ } | void;
106
+ /**
107
+ * 添加一个新的响应式数据 (临时)
108
+ * @param data 数据
109
+ * @param key 属性
110
+ * @param value 新值
111
+ */
112
+ $set(data: object, key: string | number, value: any): void;
113
+ /**
114
+ * 添加一个属性计算 (临时)
115
+ * @param data 源数据
116
+ * @param key 属性名
117
+ * @param callback 回调函数
118
+ */
119
+ protected $computed(data: object, key: string, callback: Function): void;
120
+ /**
121
+ * 添加一个观察者 create user watcher
122
+ * @param expr 表达式
123
+ * @param handler 回调函数
124
+ * @param watchOpt 配置参数
125
+ * @return 卸载当前观察者的函数 (暂未返回)
126
+ */
127
+ protected $watch(expr: string | Function, handler: Function | WatchOptType, watchOpt?: WatchOptType): Function;
128
+ }
@@ -0,0 +1,68 @@
1
+ import Lucky from './lucky';
2
+ import LuckyScratchConfig from '../types/scratch';
3
+ import { UserConfigType } from '../types/index';
4
+ export default class LuckyScratch extends Lucky {
5
+ private mask;
6
+ private scratch;
7
+ private progress;
8
+ private isScratching;
9
+ private isCompleted;
10
+ disabled: boolean;
11
+ private isFirstScratch;
12
+ private eventsInitialized;
13
+ private onceBeforeStartCallback?;
14
+ private beforeStartCallback?;
15
+ private startCallback?;
16
+ private endCallback?;
17
+ private successCallback?;
18
+ private afterInitCallback?;
19
+ /**
20
+ * 刮刮卡构造器
21
+ * @param config 配置项
22
+ * @param data 抽奖数据
23
+ */
24
+ constructor(config: UserConfigType, data: LuckyScratchConfig);
25
+ protected initData(data: LuckyScratchConfig): void;
26
+ /**
27
+ * 重写 resize 方法,在尺寸变化后重新绘制蒙层
28
+ */
29
+ protected resize(): void;
30
+ init(): Promise<void>;
31
+ private draw;
32
+ private handleBindEvents;
33
+ private handleStart;
34
+ private handleMove;
35
+ private handleEnd;
36
+ private drawArc;
37
+ /**
38
+ * 在指定坐标绘制刮痕
39
+ * @param x 相对于 canvas 的 x 坐标(物理像素,已乘以 dpr)
40
+ * @param y 相对于 canvas 的 y 坐标(物理像素,已乘以 dpr)
41
+ */
42
+ private drawArcAt;
43
+ private checkProgress;
44
+ /**
45
+ * 动态设置禁用状态
46
+ * @param disabled 是否禁用
47
+ */
48
+ setDisabled(disabled: boolean): void;
49
+ /**
50
+ * 公共方法:处理触摸/鼠标开始事件
51
+ * 供小程序、uni-app 等非 DOM 环境使用
52
+ * @param x 相对于 canvas 的 x 坐标(逻辑像素)
53
+ * @param y 相对于 canvas 的 y 坐标(逻辑像素)
54
+ */
55
+ handleTouchStart(x: number, y: number): Promise<void>;
56
+ /**
57
+ * 公共方法:处理触摸/鼠标移动事件
58
+ * 供小程序、uni-app 等非 DOM 环境使用
59
+ * @param x 相对于 canvas 的 x 坐标(逻辑像素)
60
+ * @param y 相对于 canvas 的 y 坐标(逻辑像素)
61
+ */
62
+ handleTouchMove(x: number, y: number): void;
63
+ /**
64
+ * 公共方法:处理触摸/鼠标结束事件
65
+ * 供小程序、uni-app 等非 DOM 环境使用
66
+ */
67
+ handleTouchEnd(): void;
68
+ }
@@ -0,0 +1,2 @@
1
+ declare const newArrayProto: any;
2
+ export { newArrayProto };
@@ -0,0 +1,18 @@
1
+ import Watcher from './watcher';
2
+ export default class Dep {
3
+ static target: Watcher | null;
4
+ private subs;
5
+ /**
6
+ * 订阅中心构造器
7
+ */
8
+ constructor();
9
+ /**
10
+ * 收集依赖
11
+ * @param {*} sub
12
+ */
13
+ addSub(sub: Watcher): void;
14
+ /**
15
+ * 派发更新
16
+ */
17
+ notify(): void;
18
+ }
@@ -0,0 +1,23 @@
1
+ import Dep from './dep';
2
+ export default class Observer {
3
+ value: any;
4
+ dep: Dep;
5
+ /**
6
+ * 观察者构造器
7
+ * @param value
8
+ */
9
+ constructor(value: any);
10
+ walk(data: object | any[]): void;
11
+ }
12
+ /**
13
+ * 处理响应式
14
+ * @param { Object | Array } data
15
+ */
16
+ export declare function observe(data: any): Observer | void;
17
+ /**
18
+ * 重写 setter / getter
19
+ * @param {*} data
20
+ * @param {*} key
21
+ * @param {*} val
22
+ */
23
+ export declare function defineReactive(data: any, key: string | number, val: any): void;
@@ -0,0 +1,4 @@
1
+ export declare const hasProto: boolean;
2
+ export declare function def(obj: object, key: string | number, val: any, enumerable?: boolean): void;
3
+ export declare function parsePath(path: string): (data: object | any[]) => object | any[];
4
+ export declare function traverse(value: any): void;
@@ -0,0 +1,30 @@
1
+ import Lucky from '../lib/lucky';
2
+ export interface WatchOptType {
3
+ handler?: () => Function;
4
+ immediate?: boolean;
5
+ deep?: boolean;
6
+ }
7
+ export default class Watcher {
8
+ id: number;
9
+ $lucky: Lucky;
10
+ expr: string | Function;
11
+ cb: Function;
12
+ deep: boolean;
13
+ getter: Function;
14
+ value: any;
15
+ /**
16
+ * 观察者构造器
17
+ * @param {*} $lucky
18
+ * @param {*} expr
19
+ * @param {*} cb
20
+ */
21
+ constructor($lucky: Lucky, expr: string | Function, cb: Function, options?: WatchOptType);
22
+ /**
23
+ * 根据表达式获取新值
24
+ */
25
+ get(): any;
26
+ /**
27
+ * 触发 watcher 更新
28
+ */
29
+ update(): void;
30
+ }
@@ -0,0 +1,59 @@
1
+ export type FontItemType = {
2
+ text: string;
3
+ top?: string | number;
4
+ left?: string | number;
5
+ fontColor?: string;
6
+ fontSize?: string;
7
+ fontStyle?: string;
8
+ fontWeight?: string;
9
+ lineHeight?: string;
10
+ };
11
+ export type FontExtendType = {
12
+ wordWrap?: boolean;
13
+ lengthLimit?: string | number;
14
+ lineClamp?: number;
15
+ };
16
+ export type ImgType = HTMLImageElement | HTMLCanvasElement;
17
+ export type ImgItemType = {
18
+ src: string;
19
+ top?: string | number;
20
+ left?: string | number;
21
+ width?: string;
22
+ height?: string;
23
+ formatter?: (img: ImgType) => ImgType;
24
+ $resolve?: Function;
25
+ $reject?: Function;
26
+ };
27
+ export type BorderRadiusType = string | number;
28
+ export type BackgroundType = string;
29
+ export type ShadowType = string;
30
+ export type ConfigType = {
31
+ nodeType?: number;
32
+ flag: 'WEB' | 'MP-WX' | 'UNI-H5' | 'UNI-MP' | 'TARO-H5' | 'TARO-MP';
33
+ el?: string;
34
+ divElement?: HTMLDivElement;
35
+ canvasElement?: HTMLCanvasElement;
36
+ ctx: CanvasRenderingContext2D;
37
+ dpr: number;
38
+ handleCssUnit?: (num: number, unit: string) => number;
39
+ rAF?: Function;
40
+ setTimeout: Function;
41
+ setInterval: Function;
42
+ clearTimeout: Function;
43
+ clearInterval: Function;
44
+ beforeCreate?: Function;
45
+ beforeResize?: Function;
46
+ afterResize?: Function;
47
+ beforeInit?: Function;
48
+ afterInit?: Function;
49
+ beforeDraw?: Function;
50
+ afterDraw?: Function;
51
+ afterStart?: Function;
52
+ };
53
+ export type UserConfigType = Partial<ConfigType>;
54
+ export type UniImageType = {
55
+ path: string;
56
+ width: number;
57
+ height: number;
58
+ };
59
+ export type Tuple<T, Len extends number, Res extends T[] = []> = Res['length'] extends Len ? Res : Tuple<T, Len, [...Res, T]>;
@@ -0,0 +1,19 @@
1
+ export default interface LuckyScratchConfig {
2
+ width?: string | number;
3
+ height?: string | number;
4
+ mask?: {
5
+ type?: 'color' | 'image';
6
+ color?: string;
7
+ src?: string;
8
+ };
9
+ scratch?: {
10
+ radius?: number;
11
+ percent?: number;
12
+ };
13
+ onceBeforeStart?: () => boolean | Promise<boolean>;
14
+ beforeStart?: () => boolean | Promise<boolean>;
15
+ start?: () => void;
16
+ end?: () => void;
17
+ success?: (progress: number) => void;
18
+ afterInit?: () => void;
19
+ }
@@ -0,0 +1,29 @@
1
+ import { ImgType } from '../types/index';
2
+ /**
3
+ * 根据路径获取图片对象
4
+ * @param { string } src 图片路径
5
+ * @returns { Promise<HTMLImageElement> } 图片标签
6
+ */
7
+ export declare const getImage: (src: string) => Promise<ImgType>;
8
+ /**
9
+ * 切割圆角
10
+ * @param img 将要裁剪的图片对象
11
+ * @param radius 裁剪的圆角半径
12
+ * @returns 返回一个离屏 canvas 用于渲染
13
+ */
14
+ export declare const cutRound: (img: ImgType, radius: number) => ImgType;
15
+ /**
16
+ * 透明度
17
+ * @param img 将要处理的图片对象
18
+ * @param opacity 透明度
19
+ * @returns 返回一个离屏 canvas 用于渲染
20
+ */
21
+ export declare const opacity: (img: ImgType, opacity: number) => ImgType;
22
+ /**
23
+ * 高斯模糊
24
+ * @param img 将要处理的图片对象
25
+ * @param radius 模糊半径
26
+ * @returns 返回一个离屏 canvas 用于渲染
27
+ */
28
+ export declare const blur: (img: ImgType, radius: number) => ImgType;
29
+ export declare const getBase64Image: () => void;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * 判断是否是期望的类型
3
+ * @param { unknown } param 将要判断的变量
4
+ * @param { ...string } types 期望的类型
5
+ * @return { boolean } 返回期望是否正确
6
+ */
7
+ export declare const isExpectType: (param: unknown, ...types: string[]) => boolean;
8
+ export declare const get: (data: object, strKeys: string) => any;
9
+ export declare const has: (data: object, key: string | number) => boolean;
10
+ /**
11
+ * 移除\n
12
+ * @param { string } str 将要处理的字符串
13
+ * @return { string } 返回新的字符串
14
+ */
15
+ export declare const removeEnter: (str: string) => string;
16
+ /**
17
+ * 把任何数据类型转成数字
18
+ * @param num
19
+ */
20
+ export declare const getNumber: (num: unknown) => number;
21
+ /**
22
+ * 判断颜色是否有效 (透明色 === 无效)
23
+ * @param color 颜色
24
+ */
25
+ export declare const hasBackground: (color: string | undefined | null) => boolean;
26
+ /**
27
+ * 通过padding计算
28
+ * @return { object } block 边框信息
29
+ */
30
+ export declare const computePadding: (block: {
31
+ padding?: string | undefined;
32
+ }, getLength: Function) => [number, number, number, number];
33
+ /**
34
+ * 节流函数
35
+ * @param fn 将要处理的函数
36
+ * @param wait 时间, 单位为毫秒
37
+ * @returns 包装好的节流函数
38
+ */
39
+ export declare const throttle: (fn: Function, wait?: number) => (this: any, ...args: any[]) => void;
40
+ /**
41
+ * 通过概率计算出一个奖品索引
42
+ * @param { Array<number | undefined> } rangeArr 概率
43
+ * @returns { number } 中奖索引
44
+ */
45
+ export declare const computeRange: (rangeArr: Array<number | undefined>) => number;
46
+ /**
47
+ * 根据宽度分割字符串, 来达到换行的效果
48
+ * @param text
49
+ * @param maxWidth
50
+ * @returns
51
+ */
52
+ export declare const splitText: (ctx: CanvasRenderingContext2D, text: string, getWidth: (lines: string[]) => number, lineClamp?: number) => string[];
53
+ export declare const getSortedArrayByIndex: <T>(arr: T[], order: number[]) => T[];
@@ -0,0 +1,26 @@
1
+ /**
2
+ * 转换为运算角度
3
+ * @param { number } deg 数学角度
4
+ * @return { number } 运算角度
5
+ */
6
+ export declare const getAngle: (deg: number) => number;
7
+ /**
8
+ * 根据角度计算圆上的点
9
+ * @param { number } deg 运算角度
10
+ * @param { number } r 半径
11
+ * @return { Array<number> } 坐标[x, y]
12
+ */
13
+ export declare const getArcPointerByDeg: (deg: number, r: number) => [number, number];
14
+ /**
15
+ * 根据点计算切线方程
16
+ * @param { number } x 横坐标
17
+ * @param { number } y 纵坐标
18
+ * @return { Array<number> } [斜率, 常数]
19
+ */
20
+ export declare const getTangentByPointer: (x: number, y: number) => Array<number>;
21
+ export declare const fanShapedByArc: (ctx: CanvasRenderingContext2D, minRadius: number, maxRadius: number, start: number, end: number, gutter: number) => void;
22
+ export declare const roundRectByArc: (ctx: CanvasRenderingContext2D, ...[x, y, w, h, r]: number[]) => void;
23
+ /**
24
+ * 创建线性渐变色
25
+ */
26
+ export declare const getLinearGradient: (ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, background: string) => any;
File without changes
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 缓动函数
3
+ * t: current time(当前时间)
4
+ * b: beginning value(初始值)
5
+ * c: change in value(变化量)
6
+ * d: duration(持续时间)
7
+ *
8
+ * 感谢张鑫旭大佬 https://github.com/zhangxinxu/Tween
9
+ */
10
+ interface SpeedType {
11
+ easeIn: (...arr: number[]) => number;
12
+ easeOut: (...arr: number[]) => number;
13
+ }
14
+ export declare const quad: SpeedType;
15
+ export declare const cubic: SpeedType;
16
+ export declare const quart: SpeedType;
17
+ export declare const quint: SpeedType;
18
+ export declare const sine: SpeedType;
19
+ export declare const expo: SpeedType;
20
+ export declare const circ: SpeedType;
21
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lucky-scratch",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "一个基于原生 js 的刮刮卡抽奖插件",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",