import * as Export from './export'; type Listener = (key: Export.StyleKey) => void; const systemInfo = wx.getSystemInfoSync(); // /** rpx到px的转换率 */ // const rpx = 750 / systemInfo.screenWidth; // /** 绘制单位 */ // const dpr = wx.getSystemInfoSync().pixelRatio; class Style { constructor (canvas: HTMLCanvasElement, scale: number) { this.canvas = canvas; this.scale = 375 / systemInfo.screenWidth / scale / (systemInfo.pixelRatio / 2); } private canvas: HTMLCanvasElement; private scale: number; private listenerSet: Listener[] = []; /** 重载样式的写入方法 */ public overloadSeter: (key: Export.StyleKey, value: any) => any = (key, value) => value; /** flex子项在flex容器的当前行的侧轴(纵轴)方向上的对齐方式 */ // private _alignItems: Export.AlignItems = 'flex-start'; // public set alignItems(value: Export.AlignItems) { // this._alignItems = value; // this.trigger('alignItems'); // } // public get alignItems() { // return this._alignItems; // } private _backgroundColor = ''; public set backgroundColor(value: string) { const key = 'backgroundColor'; this._backgroundColor = this.overloadSeter(key, value); this.trigger(key); } public get backgroundColor() { return this._backgroundColor; } /** 边框 */ private _border = ''; public set border(value: string) { const key = 'border'; this._border = this.overloadSeter(key, value); this.trigger(key); } public get border() { return this._border; } /** * 圆角 * @description 为数组时顺序为左上角、右上角、右下角、左下角 */ private _borderRadius: Export.Size | [number, number, number, number] = ''; public set borderRadius(value: Export.Size | [Export.Size, Export.Size, Export.Size, Export.Size]) { const key = 'borderRadius'; this._borderRadius = this.overloadSeter(key, value); this.trigger(key); } public get borderRadius() { return this._borderRadius; } /** 字体颜色 @default #000 */ private _color = '#000'; public set color(value: string) { const key = 'color'; this._color = this.overloadSeter(key, value); this.trigger(key); } public get color() { return this._color; } /** 规定元素应该生成的框的类型 */ // private _display: Export.Display = 'block'; // public set display(value: Export.Display) { // const key = 'display'; // this._display = this.overloadSeter(key, value); // this.trigger(key); // } // public get display() { // return this._display; // } /** 灵活项目的方向 */ // private _flexDirection: Export.FlexDirection = 'row'; // public set flexDirection(value: Export.FlexDirection) { // const key = 'flexDirection'; // this._flexDirection = this.overloadSeter(key, value); // this.trigger(key); // } // public get flexDirection() { // return this._flexDirection; // } /** 字体系列 */ private _fontFamily = 'arial,sans-serif'; public set fontFamily(value: string) { const key = 'fontFamily'; this._fontFamily = this.overloadSeter(key, value); this.trigger(key); } public get fontFamily() { return this._fontFamily; } /** 字体尺寸 @default 14 */ private _fontSize = 14; public set fontSize(value: number) { const key = 'fontSize'; this._fontSize = Math.ceil(this.overloadSeter(key, value)); this.trigger(key); } public get fontSize() { return this._fontSize; } /** 字体风格 */ private _fontStyle: Export.FontStyle = 'normal'; public set fontStyle(value: Export.FontStyle) { const key = 'fontStyle'; this._fontStyle = this.overloadSeter(key, value); this.trigger(key); } public get fontStyle() { return this._fontStyle; } /** 字体异体 */ private _fontVariant: Export.FontVariant = 'normal'; public set fontVariant(value: Export.FontVariant) { const key = 'fontVariant'; this._fontVariant = this.overloadSeter(key, value); this.trigger(key); } public get fontVariant() { return this._fontVariant; } /** 字体粗细 */ private _fontWeight: Export.FontWeight = 'normal'; public set fontWeight(value: Export.FontWeight) { const key = 'fontWeight'; this._fontWeight = this.overloadSeter(key, value); this.trigger(key); } public get fontWeight() { return this._fontWeight; } /** 高度 @description 支持百分比 */ private _height: Export.Size = 0; public set height(value: Export.Size) { const key = 'height'; this._height = this.overloadSeter(key, value); this.trigger(key); } public get height() { return this._height; } /** 设置行间的距离(行高) * @description 只支持倍数,不支持具体的值 * @default 1 */ private _lineHeight = 1; public set lineHeight(value: number) { const key = 'lineHeight'; this._lineHeight = this.overloadSeter(key, value); this.trigger(key); } public get lineHeight() { return this._lineHeight; } /** 设置行间的距离(行高) * @description 只支持倍数,不支持具体的值 * @default 1 */ private _maxWidth: Export.Size = 0; public set maxWidth(value: Export.Size) { const key = 'maxWidth'; this._maxWidth = this.overloadSeter(key, value); this.trigger(key); } public get maxWidth() { return this._maxWidth; } /** 设置行间的距离(行高) * @description 只支持倍数,不支持具体的值 * @default 1 */ private _textAlign: Export.TextAlign = 'left'; public set textAlign(value: Export.TextAlign) { const key = 'textAlign'; this._textAlign = this.overloadSeter(key, value); this.trigger(key); } public get textAlign() { return this._textAlign; } /** 它返回当前元素相对于其 offsetParent 元素的顶部内边距的距离。 */ // private _offsetTop = 0; // public set offsetTop(value: number) { // const key = 'offsetTop'; // this._offsetTop = this.overloadSeter(key, value); // this.trigger(key); // } // public get offsetTop() { // return this._offsetTop; // } // /** 返回当前元素左上角相对于 offsetParent 节点的左边界偏移的像素值 */ // private _offsetLeft = 0; // public set offsetLeft(value: number) { // const key = 'offsetLeft'; // this._offsetLeft = this.overloadSeter(key, value); // this.trigger(key); // } // public get offsetLeft() { // return this._offsetLeft; // } /** 内边距 */ // private _paddingTop = 0; // public set paddingTop(value: number) { // const key = 'paddingTop'; // this._paddingTop = this.overloadSeter(key, value); // this.trigger(key); // } // public get paddingTop() { // return this._paddingTop; // } /** 内边距 */ // private _paddingRight = 0; // public set paddingRight(value: number) { // const key = 'paddingRight'; // this._paddingRight = this.overloadSeter(key, value); // this.trigger(key); // } // public get paddingRight() { // return this._paddingRight; // } /** 内边距 */ // private _paddingBottom = 0; // public set paddingBottom(value: number) { // const key = 'paddingBottom'; // this._paddingBottom = this.overloadSeter(key, value); // this.trigger(key); // } // public get paddingBottom() { // return this._paddingBottom; // } /** 内边距 */ // private _paddingLeft = 0; // public set paddingLeft(value: number) { // const key = 'paddingLeft'; // this._paddingLeft = this.overloadSeter(key, value); // this.trigger(key); // } // public get paddingLeft() { // return this._paddingLeft; // } /** 宽度 @description 支持百分比 */ private _width: Export.Size = 0; public set width(value: Export.Size) { const key = 'width'; this._width = this.overloadSeter(key, value); this.trigger(key); } public get width() { return this._width; } /** 元素坐标位置 */ private _x = 0; public set x(value: number) { const key = 'x'; this._x = this.overloadSeter(key, value); this.trigger(key); } public get x() { return this._x; } /** 元素坐标位置 */ private _y: Export.Size = 0; public set y(value: Export.Size) { const key = 'y'; this._y = this.overloadSeter(key, value); this.trigger(key); } public get y() { return this._y; } /** 计算尺寸,如果是百分比单位则根据父元素计算 */ public calc(value: Export.Size, base: number) { if (typeof value === 'string') { if (value[value.length - 1] === '%') { return base * this.scale * parseFloat(value) / 100; } } else { return value; } return 0; } /** * 计算样式的数值 * @param auto | 使用实例的自带size,比如图片自带的宽高 */ public getComputedStyle(key: 'width' | 'maxWidth' | 'height' | 'x' | 'y'): number { switch (key) { case 'width': // this._width = this.calc(this.width, this.element.parent?.style.getComputedStyle('width') || this.canvas.width); this._width = this.calc(this.width, this.canvas.width); return this._width; case 'maxWidth': this._maxWidth = this.calc(this.maxWidth, this.canvas.width); return this._maxWidth; case 'height': // this._height = this.calc(this.height, this.element.parent?.style.getComputedStyle('height') || this.canvas.height); this._height = this.calc(this.height, this.canvas.height); return this._height; case 'x': this._x = this.calc(this.x, this.canvas.width); return this._x; case 'y': this._y = this.calc(this.y, this.canvas.height); return this._y; } } /** 监听触发 */ private trigger(styleKey: Export.StyleKey) { for (let i = 0; i < this.listenerSet.length; i++) { this.listenerSet[i](styleKey); } } /** 属性更新时触发 */ public addListener(callback: Listener) { this.listenerSet.push(callback); } /** 卸载监听 @deprecated 需要时才使用,不必和addListener绑定使用 */ public removeListener(callback: Listener) { const index = this.listenerSet.findIndex((item) => item === callback); this.listenerSet.splice(index, 1); } /** 绘制圆角 */ public drawBorderRadius(ctx: CanvasRenderingContext2D) { const x = this.x; const y = this.getComputedStyle('y'); const width = this.width as number; const height = this.height as number; ctx.beginPath(); if (Array.isArray(this.borderRadius)) { const r = this.borderRadius as number[]; for (let i = 0; i < r.length; i++) { if (typeof r[i] === 'string') { r[i] = parseFloat(r[i] as any as string) / 100 * width; } } // ctx.translate(x, y); // ctx.arc(x + width / 2, y + height / 2, width / 2, 0, 2 * Math.PI); // ctx.arc(x + width / 2, y + width / 2, width / 2, 50, 2 * Math.PI) ctx.arc(x + width / 2, y + height / 2, width / 2, 0, 2 * Math.PI); // //从右下角顺时针绘制,弧度从0到1/2PI ctx.arc(x + width - r[2],y + height - r[2], r[2], 0, Math.PI / 2); // 矩形下边线 ctx.lineTo(x + r[2], y + height); //左下角圆弧,弧度从1/2PI到PI ctx.arc(x + r[3], y + height - r[3], r[3], Math.PI / 2, Math.PI); //矩形左边线 ctx.lineTo(x + 0, y + r[3]); //左上角圆弧,弧度从PI到3/2PI ctx.arc(x + r[0], y + r[0], r[0], Math.PI, Math.PI * 3 / 2); //上边线 ctx.lineTo(x + width - r[0], y + 0); //右上角圆弧 ctx.arc(x + width - r[1], y + r[1], r[1], Math.PI * 3 / 2, Math.PI * 2); //右边线 ctx.lineTo(x + width, y + height - r[1]); } else { let value = 0; if (typeof this.borderRadius === 'string') { value = 360 * (parseFloat(this.borderRadius) / 100) / 90; } else { value = this.borderRadius / 90; } ctx.arc(x + width / value, y + height / value, Math.ceil(width / value), 0, value * Math.PI); } ctx.clip(); ctx.closePath(); } } export default Style;