@vnpn/lucky-canvas-core 1.7.26

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,4 @@
1
+ export { default as LuckyWheel } from './lib/wheel';
2
+ export { default as LuckyGrid } from './lib/grid';
3
+ export { default as SlotMachine } from './lib/slot';
4
+ export { cutRound, opacity } from './utils/image';
@@ -0,0 +1,130 @@
1
+ import Lucky from './lucky';
2
+ import { UserConfigType } from '../types/index';
3
+ import LuckyGridConfig from '../types/grid';
4
+ export default class LuckyGrid extends Lucky {
5
+ private rows;
6
+ private cols;
7
+ private blocks;
8
+ private prizes;
9
+ private buttons;
10
+ private button?;
11
+ private defaultConfig;
12
+ private defaultStyle;
13
+ private activeStyle;
14
+ private _defaultConfig;
15
+ private _defaultStyle;
16
+ private _activeStyle;
17
+ private startCallback?;
18
+ private endCallback?;
19
+ private cellWidth;
20
+ private cellHeight;
21
+ private startTime;
22
+ private endTime;
23
+ private currIndex;
24
+ private stopIndex;
25
+ private endIndex;
26
+ private demo;
27
+ private timer;
28
+ private FPS;
29
+ /**
30
+ * 游戏当前的阶段
31
+ * step = 0 时, 游戏尚未开始
32
+ * step = 1 时, 此时处于加速阶段
33
+ * step = 2 时, 此时处于匀速阶段
34
+ * step = 3 时, 此时处于减速阶段
35
+ */
36
+ private step;
37
+ /**
38
+ * 中奖索引
39
+ * prizeFlag = undefined 时, 处于开始抽奖阶段, 正常旋转
40
+ * prizeFlag >= 0 时, 说明stop方法被调用, 并且传入了中奖索引
41
+ * prizeFlag === -1 时, 说明stop方法被调用, 并且传入了负值, 本次抽奖无效
42
+ */
43
+ private prizeFlag;
44
+ private cells;
45
+ private prizeArea;
46
+ private ImageCache;
47
+ /**
48
+ * 九宫格构造器
49
+ * @param config 配置项
50
+ * @param data 抽奖数据
51
+ */
52
+ constructor(config: UserConfigType, data: LuckyGridConfig);
53
+ protected resize(): void;
54
+ protected initLucky(): void;
55
+ /**
56
+ * 初始化数据
57
+ * @param data
58
+ */
59
+ private initData;
60
+ /**
61
+ * 初始化属性计算
62
+ */
63
+ private initComputed;
64
+ /**
65
+ * 初始化观察者
66
+ */
67
+ private initWatch;
68
+ /**
69
+ * 初始化 canvas 抽奖
70
+ */
71
+ init(): Promise<void>;
72
+ private initImageCache;
73
+ /**
74
+ * canvas点击事件
75
+ * @param e 事件参数
76
+ */
77
+ protected handleClick(e: MouseEvent): void;
78
+ /**
79
+ * 根据索引单独加载指定图片并缓存
80
+ * @param cellName 模块名称
81
+ * @param cellIndex 模块索引
82
+ * @param imgName 模块对应的图片缓存
83
+ * @param imgIndex 图片索引
84
+ */
85
+ private loadAndCacheImg;
86
+ /**
87
+ * 绘制九宫格抽奖
88
+ */
89
+ protected draw(): void;
90
+ /**
91
+ * 处理背景色
92
+ * @param x
93
+ * @param y
94
+ * @param width
95
+ * @param height
96
+ * @param background
97
+ * @param isActive
98
+ */
99
+ private handleBackground;
100
+ /**
101
+ * 刻舟求剑
102
+ */
103
+ private carveOnGunwaleOfAMovingBoat;
104
+ /**
105
+ * 对外暴露: 开始抽奖方法
106
+ */
107
+ play(): void;
108
+ /**
109
+ * 对外暴露: 缓慢停止方法
110
+ * @param index 中奖索引
111
+ */
112
+ stop(index?: number): void;
113
+ /**
114
+ * 实际开始执行方法
115
+ * @param num 记录帧动画执行多少次
116
+ */
117
+ private run;
118
+ /**
119
+ * 计算奖品格子的几何属性
120
+ * @param { array } [...矩阵坐标, col, row]
121
+ * @return { array } [...真实坐标, width, height]
122
+ */
123
+ private getGeometricProperty;
124
+ /**
125
+ * 换算渲染坐标
126
+ * @param x
127
+ * @param y
128
+ */
129
+ protected conversionAxis(x: number, y: number): [number, number];
130
+ }
@@ -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,109 @@
1
+ import Lucky from './lucky';
2
+ import { UserConfigType } from '../types/index';
3
+ import SlotMachineConfig from '../types/slot';
4
+ export default class SlotMachine extends Lucky {
5
+ private blocks;
6
+ private prizes;
7
+ private slots;
8
+ private defaultConfig;
9
+ private _defaultConfig;
10
+ private defaultStyle;
11
+ private _defaultStyle;
12
+ private endCallback;
13
+ private _offscreenCanvas?;
14
+ private cellWidth;
15
+ private cellHeight;
16
+ private cellAndSpacing;
17
+ private widthAndSpacing;
18
+ private heightAndSpacing;
19
+ private FPS;
20
+ private scroll;
21
+ private stopScroll;
22
+ private endScroll;
23
+ private startTime;
24
+ private endTime;
25
+ private slotStep;
26
+ private defaultOrder;
27
+ /**
28
+ * 游戏当前的整体阶段
29
+ * step = 0 时, 游戏尚未开始
30
+ * step = 1 时, 此时处于加速阶段
31
+ * step = 2 时, 此时处于运行阶段
32
+ */
33
+ private step;
34
+ /**
35
+ * 中奖索引
36
+ * prizeFlag = undefined 时, 处于开始抽奖阶段, 正常旋转
37
+ * prizeFlag >= 0 时, 说明stop方法被调用, 并且传入了中奖索引
38
+ * prizeFlag === -1 时, 说明stop方法被调用, 并且传入了负值, 本次抽奖无效
39
+ */
40
+ private prizeFlag;
41
+ private prizeArea?;
42
+ private ImageCache;
43
+ /**
44
+ * 老虎机构造器
45
+ * @param config 配置项
46
+ * @param data 抽奖数据
47
+ */
48
+ constructor(config: UserConfigType, data: SlotMachineConfig);
49
+ protected resize(): void;
50
+ protected initLucky(): void;
51
+ /**
52
+ * 初始化数据
53
+ * @param data
54
+ */
55
+ private initData;
56
+ /**
57
+ * 初始化属性计算
58
+ */
59
+ private initComputed;
60
+ /**
61
+ * 初始化观察者
62
+ */
63
+ private initWatch;
64
+ /**
65
+ * 初始化 canvas 抽奖
66
+ */
67
+ init(): Promise<void>;
68
+ private redrawOffscreenCanvas;
69
+ private initImageCache;
70
+ /**
71
+ * 根据索引单独加载指定图片并缓存
72
+ * @param cellName 模块名称
73
+ * @param cellIndex 模块索引
74
+ * @param imgName 模块对应的图片缓存
75
+ * @param imgIndex 图片索引
76
+ */
77
+ private loadAndCacheImg;
78
+ /**
79
+ * 绘制离屏canvas
80
+ */
81
+ protected drawOffscreenCanvas(): void;
82
+ /**
83
+ * 绘制背景区域
84
+ */
85
+ protected drawBlocks(): SlotMachine['prizeArea'];
86
+ /**
87
+ * 绘制老虎机抽奖
88
+ */
89
+ protected draw(): void;
90
+ /**
91
+ * 刻舟求剑
92
+ */
93
+ private carveOnGunwaleOfAMovingBoat;
94
+ private hasAllPrizeFlag;
95
+ private isAllSlotStopped;
96
+ private getEndPrize;
97
+ /**
98
+ * 对外暴露: 开始抽奖方法
99
+ */
100
+ play(): void;
101
+ stop(index: number | number[], slotIndex?: number): void;
102
+ /**
103
+ * 让游戏动起来
104
+ * @param num 记录帧动画执行多少次
105
+ */
106
+ private run;
107
+ private displacement;
108
+ private displacementWidthOrHeight;
109
+ }
@@ -0,0 +1,109 @@
1
+ import Lucky from './lucky';
2
+ import { UserConfigType } from '../types/index';
3
+ import LuckyWheelConfig from '../types/wheel';
4
+ export default class LuckyWheel extends Lucky {
5
+ private blocks;
6
+ private prizes;
7
+ private buttons;
8
+ private defaultConfig;
9
+ private defaultStyle;
10
+ private _defaultConfig;
11
+ private _defaultStyle;
12
+ private startCallback?;
13
+ private endCallback?;
14
+ private Radius;
15
+ private prizeRadius;
16
+ private prizeDeg;
17
+ private prizeAng;
18
+ private rotateDeg;
19
+ private maxBtnRadius;
20
+ private startTime;
21
+ private endTime;
22
+ private stopDeg;
23
+ private endDeg;
24
+ private FPS;
25
+ /**
26
+ * 游戏当前的阶段
27
+ * step = 0 时, 游戏尚未开始
28
+ * step = 1 时, 此时处于加速阶段
29
+ * step = 2 时, 此时处于匀速阶段
30
+ * step = 3 时, 此时处于减速阶段
31
+ */
32
+ private step;
33
+ /**
34
+ * 中奖索引
35
+ * prizeFlag = undefined 时, 处于开始抽奖阶段, 正常旋转
36
+ * prizeFlag >= 0 时, 说明stop方法被调用, 并且传入了中奖索引
37
+ * prizeFlag === -1 时, 说明stop方法被调用, 并且传入了负值, 本次抽奖无效
38
+ */
39
+ private prizeFlag;
40
+ private ImageCache;
41
+ /**
42
+ * 大转盘构造器
43
+ * @param config 配置项
44
+ * @param data 抽奖数据
45
+ */
46
+ constructor(config: UserConfigType, data: LuckyWheelConfig);
47
+ protected resize(): void;
48
+ protected initLucky(): void;
49
+ /**
50
+ * 初始化数据
51
+ * @param data
52
+ */
53
+ private initData;
54
+ /**
55
+ * 初始化属性计算
56
+ */
57
+ private initComputed;
58
+ /**
59
+ * 初始化观察者
60
+ */
61
+ private initWatch;
62
+ /**
63
+ * 初始化 canvas 抽奖
64
+ */
65
+ init(): Promise<void>;
66
+ private initImageCache;
67
+ /**
68
+ * canvas点击事件
69
+ * @param e 事件参数
70
+ */
71
+ protected handleClick(e: MouseEvent): void;
72
+ /**
73
+ * 根据索引单独加载指定图片并缓存
74
+ * @param cellName 模块名称
75
+ * @param cellIndex 模块索引
76
+ * @param imgName 模块对应的图片缓存
77
+ * @param imgIndex 图片索引
78
+ */
79
+ private loadAndCacheImg;
80
+ private drawBlock;
81
+ /**
82
+ * 开始绘制
83
+ */
84
+ protected draw(): void;
85
+ /**
86
+ * 刻舟求剑
87
+ */
88
+ private carveOnGunwaleOfAMovingBoat;
89
+ /**
90
+ * 对外暴露: 开始抽奖方法
91
+ */
92
+ play(): void;
93
+ /**
94
+ * 对外暴露: 缓慢停止方法
95
+ * @param index 中奖索引
96
+ */
97
+ stop(index?: number): void;
98
+ /**
99
+ * 实际开始执行方法
100
+ * @param num 记录帧动画执行多少次
101
+ */
102
+ private run;
103
+ /**
104
+ * 换算渲染坐标
105
+ * @param x
106
+ * @param y
107
+ */
108
+ protected conversionAxis(x: number, y: number): [number, number];
109
+ }
@@ -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,85 @@
1
+ import { FontItemType, ImgItemType, BorderRadiusType, BackgroundType, ShadowType, FontExtendType } from './index';
2
+ export type PrizeFontType = FontItemType & FontExtendType;
3
+ export type ButtonFontType = FontItemType & FontExtendType;
4
+ export type CellFontType = PrizeFontType | ButtonFontType;
5
+ export type BlockImgType = ImgItemType & {};
6
+ export type PrizeImgType = ImgItemType & {
7
+ activeSrc?: string;
8
+ };
9
+ export type ButtonImgType = ImgItemType & {};
10
+ export type CellImgType = PrizeImgType | ButtonImgType;
11
+ export type BlockType = {
12
+ borderRadius?: BorderRadiusType;
13
+ background?: BackgroundType;
14
+ padding?: string;
15
+ paddingTop?: string | number;
16
+ paddingRight?: string | number;
17
+ paddingBottom?: string | number;
18
+ paddingLeft?: string | number;
19
+ imgs?: Array<BlockImgType>;
20
+ };
21
+ export type CellType<T, U> = {
22
+ x: number;
23
+ y: number;
24
+ col?: number;
25
+ row?: number;
26
+ borderRadius?: BorderRadiusType;
27
+ background?: BackgroundType;
28
+ shadow?: ShadowType;
29
+ fonts?: Array<T>;
30
+ imgs?: Array<U>;
31
+ };
32
+ export type PrizeType = CellType<PrizeFontType, PrizeImgType> & {
33
+ range?: number;
34
+ disabled?: boolean;
35
+ };
36
+ export type ButtonType = CellType<ButtonFontType, ButtonImgType> & {
37
+ callback?: Function;
38
+ };
39
+ export type DefaultConfigType = {
40
+ gutter?: number;
41
+ speed?: number;
42
+ accelerationTime?: number;
43
+ decelerationTime?: number;
44
+ };
45
+ export type DefaultStyleType = {
46
+ borderRadius?: BorderRadiusType;
47
+ background?: BackgroundType;
48
+ shadow?: ShadowType;
49
+ fontColor?: PrizeFontType['fontColor'];
50
+ fontSize?: PrizeFontType['fontSize'];
51
+ fontStyle?: PrizeFontType['fontStyle'];
52
+ fontWeight?: PrizeFontType['fontWeight'];
53
+ lineHeight?: PrizeFontType['lineHeight'];
54
+ wordWrap?: PrizeFontType['wordWrap'];
55
+ lengthLimit?: PrizeFontType['lengthLimit'];
56
+ lineClamp?: PrizeFontType['lineClamp'];
57
+ };
58
+ export type ActiveStyleType = {
59
+ background?: BackgroundType;
60
+ shadow?: ShadowType;
61
+ fontColor?: PrizeFontType['fontColor'];
62
+ fontSize?: PrizeFontType['fontSize'];
63
+ fontStyle?: PrizeFontType['fontStyle'];
64
+ fontWeight?: PrizeFontType['fontWeight'];
65
+ lineHeight?: PrizeFontType['lineHeight'];
66
+ };
67
+ export type RowsType = number;
68
+ export type ColsType = number;
69
+ export type StartCallbackType = (e: MouseEvent, button?: ButtonType) => void;
70
+ export type EndCallbackType = (prize: object) => void;
71
+ export default interface LuckyGridConfig {
72
+ width: string | number;
73
+ height: string | number;
74
+ rows?: RowsType;
75
+ cols?: ColsType;
76
+ blocks?: Array<BlockType>;
77
+ prizes?: Array<PrizeType>;
78
+ buttons?: Array<ButtonType>;
79
+ button?: ButtonType;
80
+ defaultConfig?: DefaultConfigType;
81
+ defaultStyle?: DefaultStyleType;
82
+ activeStyle?: ActiveStyleType;
83
+ start?: StartCallbackType;
84
+ end?: EndCallbackType;
85
+ }