lucky-canvas-z
Version:
一个基于原生 js 的(大转盘 / 九宫格 / 老虎机)抽奖插件
129 lines (128 loc) • 4.24 kB
TypeScript
import "../utils/polyfill";
import { ConfigType, UserConfigType, ImgItemType, ImgType, Tuple } from "../types/index";
import { WatchOptType } from "../observer/watcher";
export default class Lucky {
static version: string;
protected readonly version: string;
protected readonly config: ConfigType;
protected readonly ctx: CanvasRenderingContext2D;
protected htmlFontSize: number;
protected rAF: Function;
protected boxWidth: number;
protected boxHeight: number;
protected data: {
width: string | number;
height: string | number;
};
/**
* 公共构造器
* @param config
*/
constructor(config: string | HTMLDivElement | UserConfigType, data: {
width: string | number;
height: string | number;
});
/**
* 初始化组件大小/单位
*/
protected resize(): void;
/**
* 初始化方法
*/
protected initLucky(): void;
/**
* 鼠标点击事件
* @param e 事件参数
*/
protected handleClick(e: MouseEvent): void;
/**
* 根标签的字体大小
*/
protected setHTMLFontSize(): void;
clearCanvas(): void;
/**
* 设备像素比
* window 环境下自动获取, 其余环境手动传入
*/
protected setDpr(): void;
/**
* 重置盒子和canvas的宽高
*/
private resetWidthAndHeight;
/**
* 根据 dpr 缩放 canvas 并处理位移
*/
protected zoomCanvas(): void;
/**
* 从 window 对象上获取一些方法
*/
private initWindowFunction;
isWeb(): boolean;
/**
* 异步加载图片并返回图片的几何信息
* @param src 图片路径
* @param info 图片信息
*/
protected loadImg(src: string, info: ImgItemType, resolveName?: string): Promise<ImgType>;
/**
* 公共绘制图片的方法
* @param imgObj 图片对象
* @param rectInfo: [x轴位置, y轴位置, 渲染宽度, 渲染高度]
*/
protected drawImage(ctx: CanvasRenderingContext2D, imgObj: ImgType, ...rectInfo: [...Tuple<number, 4>, ...Partial<Tuple<number, 4>>]): void;
/**
* 计算图片的渲染宽高
* @param imgObj 图片标签元素
* @param imgInfo 图片信息
* @param maxWidth 最大宽度
* @param maxHeight 最大高度
* @return [渲染宽度, 渲染高度]
*/
protected computedWidthAndHeight(imgObj: ImgType, imgInfo: ImgItemType, maxWidth: number, maxHeight: number): [number, number];
/**
* 转换单位
* @param { string } value 将要转换的值
* @param { number } denominator 分子
* @return { number } 返回新的字符串
*/
protected changeUnits(value: string, denominator?: number): number;
/**
* 获取长度
* @param length 将要转换的长度
* @param maxLength 最大长度
* @return 返回长度
*/
protected getLength(length: string | number | undefined, maxLength?: number): number;
/**
* 获取相对(居中)X坐标
* @param width
* @param col
*/
protected getOffsetX(width: number, maxWidth?: number): number;
protected getOffscreenCanvas(width: number, height: number): {
_offscreenCanvas: HTMLCanvasElement;
_ctx: CanvasRenderingContext2D;
} | void;
/**
* 添加一个新的响应式数据 (临时)
* @param data 数据
* @param key 属性
* @param value 新值
*/
$set(data: object, key: string | number, value: any): void;
/**
* 添加一个属性计算 (临时)
* @param data 源数据
* @param key 属性名
* @param callback 回调函数
*/
protected $computed(data: object, key: string, callback: Function): void;
/**
* 添加一个观察者 create user watcher
* @param expr 表达式
* @param handler 回调函数
* @param watchOpt 配置参数
* @return 卸载当前观察者的函数 (暂未返回)
*/
protected $watch(expr: string | Function, handler: Function | WatchOptType, watchOpt?: WatchOptType): Function;
}