import Event from '../../../typings/event'; import Style from '../Style'; // import View from './View'; export type TagName = 'image' | 'text' | 'view'; export type EventType = 'tap' | 'pressMove' | 'pinch'; export interface Position { x: number; y: number; } type EventCallback = (event: Event.Touch) => void; abstract class Element { constructor(canvas: HTMLCanvasElement, tagName: TagName, scale: number) { this.canvas = canvas; this.tagName = tagName; this.scale = scale; this.style = new Style(this.canvas, scale); } protected scale: number; protected readonly canvas: HTMLCanvasElement; /** 父元素 */ // public parent: View | undefined; /** 标签名 */ public readonly tagName: string; /** 样式 */ public readonly style: Style; /** 绑定的事件 */ public _events: Record = { tap: [], pressMove: [], pinch: [] }; /** * 渲染函数的具体实现 * @param offset | 画板绘制到此元素时的offset */ public abstract render(ctx: CanvasRenderingContext2D): void; /** 添加事件绑定 */ public addEventListener(type: EventType, callback: EventCallback) { this._events[type].push(callback); } } export default Element;