can-draw
Version:
Draw canvas conveniently.
83 lines (82 loc) • 2.2 kB
TypeScript
import CustomShape from './Shapes/CustomShape';
import Circle from './Shapes/Circle';
import Text from './Shapes/Text';
import Ring from './Shapes/Ring';
import Line from './Shapes/Line';
import { IDrawConfig } from './types';
import { Shape } from './Shapes';
/**
* CanDraw
*
* 注意:祖先元素必须是有具体宽度/高度的,在使用 `flex` 或 `grid` 布局时,必须设定 `width/height` 为绝对值或百分比值,
* 否则会造成canvas容器宽度/高度计算错误
*
* NOTE: The width/height of parent element must be exact, or the width/height of canvas will be wrong.
* Especially when use `flex` or `grid` display, set `width/height` to exact value or percentage value.
* @constructor
* @author winjey-song@163.com
*/
declare class CanDraw {
static Shape: typeof Shape;
static CustomShape: typeof CustomShape;
static Circle: typeof Circle;
static Line: typeof Line;
static Text: typeof Text;
static Ring: typeof Ring;
CONFIG: IDrawConfig;
DPR: number;
private _willDraw;
private _size;
private _containerEle;
private _canvasEle;
private _canvasCtx;
constructor(config?: IDrawConfig);
/**
* 设置容器
* @param {HTMLElement} container
* @returns {CanDraw}
*/
setContainer(container: HTMLElement): this;
/**
* 获取是否绘制的状态
* @returns {boolean}
*/
getWillDraw(): boolean;
/**
* 获取容器宽度
* @returns {number}
*/
getWidth(): number;
/**
* 获取容器高度
* @returns {number}
*/
getHeight(): number;
/**
* 获取canvas上下文
* @returns {CanvasRenderingContext2D | null}
*/
getCanvasCtx(): CanvasRenderingContext2D;
/**
* 初始化容器与canvas画布
* @returns {CanDraw}
*/
init(): this;
/**
* 窗口resize时调用
* @returns {CanDraw}
*/
resize(): this;
/**
* 清空画布
* @returns {CanDraw}
*/
clear(shape?: Shape): this;
/**
* 将形状添加并绘制到画布
* @param shape
* @returns {CanDraw}
*/
add(shape: Shape): this;
}
export default CanDraw;