import Element, { Position } from './base'; class Text extends Element { constructor(canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D, scale: number) { super(canvas, 'text', scale); this.ctx = ctx; // this.style.overloadSeter = (key, value) => { // if (key === 'fontSize' || key === 'lineHeight') { // this.setSize(); // } // return value; // }; } private ctx: CanvasRenderingContext2D; private _innerText = ''; /** 待插入的文本 */ public set innerText(value: string) { this._innerText = value; // this.setSize(); } public get innerText() { return this._innerText; } /** 文本的最大显示行数 */ public line = 1; private setFont(ctx: CanvasRenderingContext2D) { const fontSize = this.style.fontSize; const fontStyle = this.style.fontStyle; const fontWeight = this.style.fontWeight; const fontVariant = this.style.fontVariant; const fontFamily = this.style.fontFamily; ctx.font = `${fontStyle} ${fontVariant} ${fontWeight} ${fontSize}px ${fontFamily}`; ctx.fillStyle = this.style.color; } // public setSize(ctx?: CanvasRenderingContext2D) { // this.setFont(ctx || this.ctx); // const textMetrics = this.ctx.measureText(this.innerText); // const fontSize = this.style.fontSize; // // const paddingScale = this.style.paddingScale(); // this.style.height = fontSize * this.style.lineHeight; // if (this.style.width) // this.style.width = textMetrics.width; // this.lienHeight = this.style.height - fontSize; // } private ellipsis(context?: CanvasRenderingContext2D) { const ctx = context || this.ctx; /** 省略号宽度 */ const ellipsisWidth = ctx.measureText('...').width; const fontSize = this.style.fontSize; let x = this.style.x; const y = this.style.getComputedStyle('y'); const chars = this.innerText.split(''); const height = fontSize * this.style.lineHeight; let line = 1; let newText = chars[0]; let width = 0; /** 判断文本是否已经都渲染完 */ let isEnd = false; for (let i = 1; i < chars.length; i++) { newText = newText + chars[i]; const newTextWidth = ctx.measureText(newText).width + newText.length; isEnd = false; if (newTextWidth + ellipsisWidth >= this.style.getComputedStyle('maxWidth')) { ctx.fillText(newText, x, y + height * line); width = newTextWidth + ellipsisWidth; newText = ''; line++; isEnd = true; } } if (isEnd === false) { if (this.style.textAlign === 'center') { const newTextWidth = ctx.measureText(newText).width; x += (this.style.getComputedStyle('maxWidth') - newTextWidth) / 2; } ctx.fillText(newText, x, y + height * line); } return { width, height }; } public render(ctx: CanvasRenderingContext2D) { this.setFont(ctx || this.ctx); const textMetrics = this.ctx.measureText(this.innerText); const fontSize = this.style.fontSize; /** 不计算文字大小的行高 */ const x = this.style.x; if (this.line <= 1) { const width = textMetrics.width; const height = fontSize * this.style.lineHeight; const lienHeight = height - fontSize; const y = this.style.getComputedStyle('y') + height + lienHeight / 2; this.style.width = width; this.style.height = fontSize * this.style.lineHeight; ctx.fillText(this.innerText, x, y); } else { const { width, height } = this.ellipsis(ctx); this.style.width = width; this.style.height = height; } } } export default Text;