-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual-array.ts
More file actions
243 lines (237 loc) · 6.82 KB
/
visual-array.ts
File metadata and controls
243 lines (237 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
export type tBottomIndex = "head tail" | "all";
export type tArrayAction = "unshift" | "push" | "shift" | "pop"
export interface iTopText {
show: boolean,
data: Object
}
export interface iInitial {
containerId: string,
array: Array<number>,
cw?: number,
ch?: number,
bottomIndex?: tBottomIndex,
topText?: iTopText,
customStyle?: any,
}
class VisualArray {
array: Array<number>
readonly cw?: number
readonly ch?: number
readonly bottomIndex?: tBottomIndex
readonly topText?: iTopText
readonly customStyle?: any
length: number
readonly numberFont: number
canvas: HTMLCanvasElement
ctx: CanvasRenderingContext2D
constructor(props: iInitial) {
const { containerId,
array,
cw = 30,
ch = 30,
bottomIndex = "head tail",
topText,
customStyle = { font: "30px math", strokeStyle: "black", lineWidth: 1 } } = props
this.array = array;
this.cw = cw;
this.ch = cw;
this.bottomIndex = bottomIndex;
this.topText = topText || {
show: true,
data: { 0: "头", [array.length - 1]: "尾" },
};
this.customStyle = customStyle;
this.length = array.length;
this.numberFont = parseInt(customStyle.font.replace("px", ""));
// 若已经存在则不重新创建canvas
this.canvas =
<HTMLCanvasElement>document.getElementById(containerId).children[0] ||
document.createElement("canvas");
this._calculateCanvas(
this.length,
cw,
ch,
this.bottomIndex,
this.topText,
this.numberFont,
this.customStyle
);
const isCanvasExist = this._checkCanvasExist(containerId);
if (!isCanvasExist) {
document.body.appendChild(this.canvas);
}
this._configContext();
this._generateArray(array, cw, ch, this.topText, customStyle);
this._generateText(this.topText, cw);
this._generateIndex(cw, ch, this.bottomIndex, this.topText);
if (!isCanvasExist) {
this._appendToContainer(containerId);
}
}
/**
* 计算canvas宽度和高度
*/
_calculateCanvas(
length: number,
cw: number,
ch: number,
bottomIndex: tBottomIndex,
topText: iTopText,
numberFont: number,
customStyle: any
) {
const width = cw * length;
let canvasHeight = ch;
if (bottomIndex) {
canvasHeight += numberFont;
}
if (topText.show) {
canvasHeight += numberFont;
}
const height = canvasHeight + 2 * customStyle.lineWidth;
this.canvas.width = width;
this.canvas.height = height;
return { width, height };
}
/**
* 检查是否已经添加
*/
_checkCanvasExist(id: string) {
const container = document.getElementById(id);
return container.children.length >= 1;
}
/**
* 生成数组
*/
_generateArray(array: Array<number>, cw: number, ch: number, topText: iTopText, customStyle: any) {
const { strokeStyle } = customStyle;
this.ctx.strokeStyle = strokeStyle;
let y = 0;
if (topText.show) {
y += this.numberFont + 2 * customStyle.lineWidth;
}
for (let i = 0; i < this.length; i++) {
this.ctx.strokeRect(i * cw, y, cw, ch);
this.ctx.fillText(
`${array[i]}`,
(i + 0.5) * cw,
ch / 2 + y + this.numberFont / 2
);
}
}
/**
* 生成顶部文字
*/
_generateText(topText: iTopText, cw: number) {
if (!topText.show) return;
let keys = Object.keys(topText.data);
keys.forEach((key) => {
this.ctx.fillText(
topText.data[key],
(parseInt(key) + 0.5) * cw,
this.numberFont
);
});
}
/**
* 生成底部索引
*/
_generateIndex(cw: number, ch: number, bottomIndex: tBottomIndex, topText: iTopText) {
let y = ch;
if (bottomIndex) {
y += this.numberFont;
}
if (topText.show) {
y += this.numberFont;
}
if (bottomIndex === "head tail") {
this.ctx.fillText('0', (0 + 0.5) * cw, y);
this.ctx.fillText(`${this.length - 1}`, (this.length - 1 + 0.5) * cw, y);
} else {
bottomIndex === "all";
for (let i = 0; i < this.length; i++) {
this.ctx.fillText(`${i}`, (i + 0.5) * cw, y);
}
}
}
/**
* 添加到容器
*/
_appendToContainer(id: string) {
const container = document.getElementById(id);
container.appendChild(this.canvas);
}
/**
* 配置context
*/
_configContext() {
this.ctx = this.canvas.getContext("2d");
this.ctx.font = this.customStyle.font;
this.ctx.textAlign = "center";
}
/**
* 重新绘制
*/
_rerender() {
this._generateArray(
this.array,
this.cw,
this.ch,
this.topText,
this.customStyle
);
this._generateText(this.topText, this.cw);
this._generateIndex(this.cw, this.ch, this.bottomIndex, this.topText);
}
/**
* 数组操作unshift、push、shift、pop
*/
mutateArray(action: tArrayAction, payload?: number) {
this.clearCanvas();
if (action === "unshift" || action === "push") {
this.array[action](payload);
} else {
this.array[action]();
}
this.length = this.array.length;
this._calculateCanvas(
this.length,
this.cw,
this.ch,
this.bottomIndex,
this.topText,
this.numberFont,
this.customStyle
);
this._configContext();
let tailIndex = this.array.length;
if (action === "unshift" || action === "push") {
tailIndex -= 2;
}
this.topText.data = {
0: this.topText.data[0],
[this.array.length - 1]: this.topText.data[tailIndex],
};
this._rerender();
}
/**
* 导出图片
*/
exportImage() {
this.canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.setAttribute("href", url);
a.setAttribute("download", `visual-array-${+new Date()}.jpg`);
a.click();
URL.revokeObjectURL(url);
});
}
/**
* 清空画布
*/
clearCanvas() {
this.ctx?.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
export default VisualArray;