lyb-pixi-js 1.12.73 → 1.12.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
/** @description 表格绘制并填入数字 */
|
|
2
1
|
import { Container } from "pixi.js";
|
|
3
2
|
export interface LibPixiTableParams {
|
|
4
3
|
/** 表格数据 */
|
|
5
4
|
data: (number | string)[][];
|
|
6
5
|
/** 是否需要表格外框 */
|
|
7
6
|
outsideBorder?: boolean;
|
|
8
|
-
/**
|
|
7
|
+
/** 单元格最小宽度 */
|
|
9
8
|
cellWidth?: number;
|
|
10
|
-
/**
|
|
9
|
+
/** 单元格最小高度 */
|
|
11
10
|
cellHeight?: number;
|
|
12
11
|
/** 字体大小 */
|
|
13
12
|
fontSize?: number;
|
|
@@ -15,50 +14,18 @@ export interface LibPixiTableParams {
|
|
|
15
14
|
fontColor?: string;
|
|
16
15
|
/** 表格第一列字体颜色 */
|
|
17
16
|
firstColumnFontColor?: string;
|
|
18
|
-
/**
|
|
17
|
+
/** 表格第一行字体颜色 */
|
|
18
|
+
firstRowFontColor?: string;
|
|
19
|
+
/** 是否字体需要加粗 */
|
|
19
20
|
fontBold?: boolean;
|
|
21
|
+
/** 格子内部内边距 */
|
|
22
|
+
cellPadding?: number;
|
|
20
23
|
/** 线条厚度 */
|
|
21
24
|
lineWidth?: number;
|
|
22
25
|
/** 线条颜色 */
|
|
23
26
|
lineColor?: string;
|
|
24
27
|
}
|
|
25
|
-
/** @description
|
|
26
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-pixi-js#LibPixiTable-数字表格
|
|
27
|
-
*/
|
|
28
|
+
/** @description 绘制表格 */
|
|
28
29
|
export declare class LibPixiTable extends Container {
|
|
29
|
-
/** 行数 */
|
|
30
|
-
private _rows;
|
|
31
|
-
/** 列数 */
|
|
32
|
-
private _cols;
|
|
33
|
-
/** 单元格宽度 */
|
|
34
|
-
private _cellWidth;
|
|
35
|
-
/** 单元格高度 */
|
|
36
|
-
private _cellHeight;
|
|
37
|
-
/** 字体大小 */
|
|
38
|
-
private _fontSize;
|
|
39
|
-
/** 线条宽度 */
|
|
40
|
-
private _lineWidth;
|
|
41
|
-
/** 字体颜色 */
|
|
42
|
-
private _fontColor;
|
|
43
|
-
/** 表格第一列字体颜色 */
|
|
44
|
-
private _firstColumnFontColor;
|
|
45
|
-
/** 线条颜色 */
|
|
46
|
-
private _lineColor;
|
|
47
|
-
/** 是否需要表格外框 */
|
|
48
|
-
private _outsideBorder;
|
|
49
|
-
/** 是否需要加粗 */
|
|
50
|
-
private _fontBold;
|
|
51
|
-
/** 二维数字数组 */
|
|
52
|
-
private _data;
|
|
53
30
|
constructor(params: LibPixiTableParams);
|
|
54
|
-
/** @description 绘制表格 */
|
|
55
|
-
private _drawTable;
|
|
56
|
-
/** @description 填充数字 */
|
|
57
|
-
private fillNumbers;
|
|
58
|
-
/** @description 创建数字文本
|
|
59
|
-
* @param number 数字
|
|
60
|
-
* @param col 列索引
|
|
61
|
-
* @param row 行索引
|
|
62
|
-
*/
|
|
63
|
-
private _createNumberText;
|
|
64
31
|
}
|
|
@@ -1,76 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { libPixiScaleContainer } from '../../Utils/LibPixiScaleContainer';
|
|
4
|
-
/** @description 绘制表格并填充数字
|
|
5
|
-
* @link 使用方法:https://www.npmjs.com/package/lyb-pixi-js#LibPixiTable-数字表格
|
|
6
|
-
*/
|
|
1
|
+
import { Container, Graphics, Text, TextStyle } from "pixi.js";
|
|
2
|
+
/** @description 绘制表格 */
|
|
7
3
|
export class LibPixiTable extends Container {
|
|
8
4
|
constructor(params) {
|
|
5
|
+
var _a, _b;
|
|
9
6
|
super();
|
|
10
|
-
const { data, cellWidth =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
graphics.drawRect(0, 0, tableWidth, tableHeight);
|
|
7
|
+
const { data, outsideBorder = true, cellWidth = 80, cellHeight = 30, fontSize = 14, fontColor = "#000000", firstColumnFontColor, firstRowFontColor, fontBold = true, cellPadding = 6, lineWidth = 1, lineColor = "#cccccc", } = params;
|
|
8
|
+
if (!data || !data.length)
|
|
9
|
+
return;
|
|
10
|
+
// 计算列数(取最长行)
|
|
11
|
+
const cols = data.reduce((max, row) => Math.max(max, row.length), 0);
|
|
12
|
+
// 文本样式缓存
|
|
13
|
+
const baseStyle = new TextStyle({
|
|
14
|
+
fontSize,
|
|
15
|
+
fontWeight: fontBold ? "bold" : "normal",
|
|
16
|
+
});
|
|
17
|
+
// 先测量所有单元格尺寸,得到每列宽度、每行高度
|
|
18
|
+
const colWidths = Array(cols).fill(cellWidth);
|
|
19
|
+
const rowHeights = Array(data.length).fill(cellHeight);
|
|
20
|
+
for (let r = 0; r < data.length; r += 1) {
|
|
21
|
+
for (let c = 0; c < cols; c += 1) {
|
|
22
|
+
const raw = (_a = (data[r] && data[r][c])) !== null && _a !== void 0 ? _a : "";
|
|
23
|
+
const txt = new Text(String(raw), baseStyle);
|
|
24
|
+
const w = txt.width + cellPadding * 2;
|
|
25
|
+
const h = txt.height + cellPadding * 2;
|
|
26
|
+
if (w > colWidths[c])
|
|
27
|
+
colWidths[c] = w;
|
|
28
|
+
if (h > rowHeights[r])
|
|
29
|
+
rowHeights[r] = h;
|
|
30
|
+
}
|
|
35
31
|
}
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
// 计算每个单元格左上坐标
|
|
33
|
+
const colX = [];
|
|
34
|
+
let accX = 0;
|
|
35
|
+
for (let c = 0; c < cols; c += 1) {
|
|
36
|
+
colX.push(accX);
|
|
37
|
+
accX += colWidths[c];
|
|
40
38
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
const rowY = [];
|
|
40
|
+
let accY = 0;
|
|
41
|
+
for (let r = 0; r < rowHeights.length; r += 1) {
|
|
42
|
+
rowY.push(accY);
|
|
43
|
+
accY += rowHeights[r];
|
|
45
44
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
45
|
+
// 绘制网格线
|
|
46
|
+
const g = new Graphics();
|
|
47
|
+
const lineHex = parseInt(lineColor.replace("#", ""), 16);
|
|
48
|
+
g.lineStyle(lineWidth, lineHex);
|
|
49
|
+
const totalW = accX;
|
|
50
|
+
const totalH = accY;
|
|
51
|
+
// 可选外框
|
|
52
|
+
if (outsideBorder) {
|
|
53
|
+
g.drawRect(0, 0, totalW, totalH);
|
|
54
|
+
}
|
|
55
|
+
// 画竖线
|
|
56
|
+
let xPos = 0;
|
|
57
|
+
for (let c = 1; c < cols; c += 1) {
|
|
58
|
+
xPos += colWidths[c - 1];
|
|
59
|
+
g.moveTo(xPos + lineWidth / 2, 0);
|
|
60
|
+
g.lineTo(xPos + lineWidth / 2, totalH);
|
|
61
|
+
}
|
|
62
|
+
// 画横线
|
|
63
|
+
let yPos = 0;
|
|
64
|
+
for (let r = 1; r < rowHeights.length; r += 1) {
|
|
65
|
+
yPos += rowHeights[r - 1];
|
|
66
|
+
g.moveTo(0, yPos + lineWidth / 2);
|
|
67
|
+
g.lineTo(totalW, yPos + lineWidth / 2);
|
|
68
|
+
}
|
|
69
|
+
this.addChild(g);
|
|
70
|
+
// 绘制文本(居中)
|
|
71
|
+
for (let r = 0; r < data.length; r += 1) {
|
|
72
|
+
for (let c = 0; c < cols; c += 1) {
|
|
73
|
+
const raw = (_b = (data[r] && data[r][c])) !== null && _b !== void 0 ? _b : "";
|
|
74
|
+
const color = (r === 0 && firstRowFontColor) || (c === 0 && firstColumnFontColor) || fontColor;
|
|
75
|
+
const style = new TextStyle({
|
|
76
|
+
fontSize,
|
|
77
|
+
fontWeight: fontBold ? "bold" : "normal",
|
|
78
|
+
fill: color,
|
|
79
|
+
});
|
|
80
|
+
const txt = new Text(String(raw), style);
|
|
81
|
+
const cx = colX[c];
|
|
82
|
+
const cy = rowY[r];
|
|
83
|
+
// 居中放置
|
|
84
|
+
const tx = cx + (colWidths[c] - txt.width) / 2;
|
|
85
|
+
const ty = cy + (rowHeights[r] - txt.height) / 2;
|
|
86
|
+
txt.x = Math.round(tx);
|
|
87
|
+
txt.y = Math.round(ty);
|
|
88
|
+
this.addChild(txt);
|
|
54
89
|
}
|
|
55
90
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
*/
|
|
62
|
-
_createNumberText(number, col, row) {
|
|
63
|
-
const text = new Text(number.toString(), {
|
|
64
|
-
_fontSize: this._fontSize,
|
|
65
|
-
fill: col === 0 ? this._firstColumnFontColor : this._fontColor,
|
|
66
|
-
fontWeight: this._fontBold ? "bold" : "normal",
|
|
67
|
-
});
|
|
68
|
-
// 计算文本的居中位置
|
|
69
|
-
const x = col * this._cellWidth + this._cellWidth / 2;
|
|
70
|
-
const y = row * this._cellHeight + this._cellHeight / 2;
|
|
71
|
-
this.addChild(text);
|
|
72
|
-
text.anchor.set(0.5);
|
|
73
|
-
text.position.set(x, y);
|
|
74
|
-
libPixiScaleContainer(text, this._cellWidth * 0.9);
|
|
91
|
+
// 设置容器大小属性(便于外部读取)
|
|
92
|
+
// @ts-ignore 想让外部能通过宽高读取
|
|
93
|
+
this.width = totalW;
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
this.height = totalH;
|
|
75
96
|
}
|
|
76
97
|
}
|