forked from heartfly/algorithm-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
68 lines (67 loc) · 1.63 KB
/
draw.js
File metadata and controls
68 lines (67 loc) · 1.63 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
import config from "./config";
var width, height,
r = config.radius;
function Draw (canvas) {
this.cache = [];
this.init(canvas);
}
Draw.prototype.init = function (canvas) {
this.ctx = canvas.getContext("2d");
this.ctx.font = config.font;
this.ctx.textAlign = "center";
height = canvas.height;
width = canvas.width;
this.startX = width / 2;
this.startY = r;
};
Draw.prototype.round = function (x, y, value, c) {
this.ctx.beginPath();
this.ctx.arc(x, y, r, 0, 2 * Math.PI, 0);
this.ctx.fillStyle = c || "#27A28D";
this.ctx.fill();
this.font(x, y, value);
};
Draw.prototype.font = function (x, y, value) {
this.ctx.fillStyle = config.color;
this.ctx.fillText(value, x, y + config.dev);
};
Draw.prototype.draw = function (root) {
this.ctx.clearRect(0, 0, width, height);
this.PreOrder(root, this.startX, this.startY, 500);
this.line();
};
Draw.prototype.PreOrder = function (root, x, y, w) {
if (root == null) return void 0;
var lx = x - w / 2,
rx = x + w / 2,
lry = y + 50,
c = root.isRed === void 0
? undefined
: root.isRed ? "red" : "#111";
this.round(x, y, root.value, c);
this.PreOrder(root.left, lx, lry, w / 2);
this.PreOrder(root.right, rx, lry, w / 2);
if (root.left) {
this.cache.push({
start: [x, y + r],
end: [lx, lry - r]
});
}
if (root.right) {
this.cache.push({
start: [x, y + r],
end: [rx, lry - r]
});
}
};
Draw.prototype.line = function () {
this.cache.forEach((val) => {
this.ctx.beginPath();
this.ctx.strokeStyle = "#aaa";
this.ctx.moveTo.apply(this.ctx, val.end);
this.ctx.lineTo.apply(this.ctx, val.start);
this.ctx.stroke();
});
this.cache = [];
};
export default Draw;