import Element, { Position } from './base'; class View extends Element { constructor(canvas: HTMLCanvasElement, scale: number) { super(canvas, 'view', scale); } private readonly _childrens: Element[] = []; /** 渲染偏移量 */ // private offsetLeft = 0; // /** 渲染偏移量 */ // private offsetTop = 0; /** 子元素 */ // public get childrens() { // return this._childrens; // } /** 当前节点的最后面插入其它节点内容(作为子节点) */ // public append(element: Element) { // element.parent = this; // this._childrens.push(element); // } // /** 获取子元素渲染的方向 */ // private getDirection(): 'horizontal' | 'vertical' | undefined { // if (this.style.display === 'flex' && this.style.flexDirection === 'row') { // return 'horizontal'; // } else if (this.style.display === 'block') { // return 'vertical'; // } else { // console.error('View样式设置有误,渲染方向获取失败', this.style); // } // } // public setSize() { // const childrens = this.childrens; // const direction = this.getDirection(); // let [maxWidth, maxHeight, width, height] = [0, 0, 0, 0]; // for (let i = 0; i < childrens.length; i++) { // const child = childrens[i]; // child.setSize(); // const childHeight = child.style.getComputedStyle('height')!; // const childWidth = child.style.getComputedStyle('width')!; // width += childWidth; // height += childHeight; // if (maxHeight < childHeight) { // maxHeight = childHeight; // } // if (maxWidth < childWidth) { // maxWidth = childWidth; // } // } // if (direction === 'horizontal') { // this.style.height = maxHeight; // this.style.width = width; // } else { // this.style.height = height; // this.style.width = maxWidth; // } // this.style.height += this.style.paddingTop + this.style.paddingBottom; // this.style.width += this.style.paddingLeft + this.style.paddingRight; // } public render(ctx: CanvasRenderingContext2D) { // const childrens = this.childrens; // const direction = this.getDirection(); // const alignItems = this.style.alignItems; // const height = this.style.height as number; // this.offsetLeft = posiotn.x + this.style.paddingLeft - this.style.paddingRight; // this.offsetTop = posiotn.y + this.style.paddingTop - this.style.paddingBottom; // for (let i = 0; i < childrens.length; i++) { // const child = childrens[i]; // const prevChild = childrens[i - 1]; // const childHeight = child.style.getComputedStyle('height')!; // const childWidth = child.style.getComputedStyle('width')!; // let [x, y] = [0, 0]; // if (direction === 'horizontal') { // x = prevChild?.style.getComputedStyle('width') || 0; // if (alignItems === 'flex-start') { // y = this.offsetTop; // } else if (alignItems === 'center') { // const offsetTop = (height - childHeight) / 2; // y = offsetTop > 0 ? offsetTop : 0; // console.log('childHeight', childHeight, y, this.offsetTop); // } // } // child.style.x = x + this.offsetLeft; // child.style.y = y + this.offsetTop; // if (direction === 'horizontal') { // child.render(ctx, { x: this.offsetLeft, y: child.style.y }); // this.offsetLeft += childWidth; // } else { // child.render(ctx, { x: child.style.x, y: this.offsetTop }); // this.offsetTop += childHeight; // // console.log('child.style.offsetTop', child.style.offsetTop, this.style.offsetTop); // } // } ctx.save(); if (this.style.borderRadius) { this.style.drawBorderRadius(ctx); } if (this.style.backgroundColor) { ctx.fillStyle = this.style.backgroundColor; ctx.fillRect( this.style.x, this.style.getComputedStyle('y'), this.style.getComputedStyle('width'), this.style.getComputedStyle('height') ); } if (this.style.border) { const params = this.style.border.split(' '); ctx.lineWidth = parseFloat(params[0]); ctx.strokeStyle = params[2]; ctx.strokeRect( this.style.x + ctx.lineWidth / 2, this.style.getComputedStyle('y') + ctx.lineWidth / 2, this.style.getComputedStyle('width') - ctx.lineWidth, this.style.getComputedStyle('height') - ctx.lineWidth ); } ctx.restore(); } } export default View;