-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (63 loc) · 1.71 KB
/
Copy pathindex.js
File metadata and controls
72 lines (63 loc) · 1.71 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
const WIDTH = 800;
const HEIGHT = 320;
const MAX_LENGTH = Math.floor(WIDTH / 22);
class RectPaint {
constructor() {
this.ctx = document.querySelector('canvas').getContext('2d');
this.sortArr = [];
this.renderQueue = [];
}
draw(array, colors = {}) {
this.ctx.clearRect(0, 0, WIDTH, HEIGHT);
array = array.slice(0, MAX_LENGTH);
const offset = Math.floor((WIDTH - array.length * 22) / 2);
for (let i = 0; i < array.length; i++) {
this.ctx.fillStyle = '#888';
this.ctx.fillText(array[i], i * 22 + offset + 2, HEIGHT - array[i] - 5)
this.ctx.fillStyle = colors[i] || '#48908f';
this.ctx.fillRect(i * 22 + offset, HEIGHT - array[i], 20, array[i]);
}
}
geratorRandomArr() {
const arr = this._geratorRandomArr(Math.floor(800 / 22));
this.sortArr = arr;
this.stopAnimate();
this.draw(arr);
}
sort(sortFn, speed) {
if (this.timer) return;
sortFn(this.sortArr, this);
this.beginAnimate(speed)
}
beginAnimate(speed) {
this.timer = setInterval(() => {
if (!this.renderQueue.length) {
this.stopAnimate();
return;
}
const { data, colors } = this.renderQueue.shift();
this.draw(data, colors);
}, speed)
}
stopAnimate() {
clearInterval(this.timer);
this.renderQueue = [];
this.timer = null;
this.draw(this.sortArr);
}
clearCanvas() {
this.ctx.clearRect(0, 0, WIDTH, HEIGHT);
}
addAnimateData(data) {
this.renderQueue.push(data)
}
// 生成随机高度的柱形数组
_geratorRandomArr(len) {
let arr = [];
for (let i = 0; i < len; i++) {
const height = Math.floor(Math.random() * 29 + 1) * 10;
arr.push(height)
}
return arr;
}
}