-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20181202.ts
More file actions
62 lines (52 loc) · 1.46 KB
/
20181202.ts
File metadata and controls
62 lines (52 loc) · 1.46 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
import Doodle from "./Doodle";
import { RandomColor } from "./Colors";
import { Point } from "./Point";
import { TreeNode } from "./TreeNode";
const screenSize = 128;
export class DailyDoodle implements Doodle {
private tree: TreeNode;
public init() {
this.tree = this.createTreeNode(new Point(64, 80), 12);
}
public update() {
// nop
}
public draw(ctx: CanvasRenderingContext2D) {
ctx.clearRect(0, 0, screenSize, screenSize);
ctx.beginPath();
ctx.strokeStyle = RandomColor();
ctx.lineWidth = 2;
ctx.moveTo(64, 128);
this.lineToNode(ctx, this.tree);
ctx.stroke();
}
private createTreeNode(p: Point, branchLength: number): TreeNode {
const n = new TreeNode(new Point(p.x, p.y));
if (branchLength <= 0) {
return n;
}
if (p.x - branchLength >= 0) {
n.leftChild = this.createTreeNode(
new Point(p.x - branchLength, p.y - branchLength),
branchLength - 2
);
}
if (p.x + branchLength <= screenSize) {
n.rightChild = this.createTreeNode(
new Point(p.x + branchLength, p.y - branchLength),
branchLength - 2
);
}
return n;
}
private lineToNode(ctx: CanvasRenderingContext2D, node: TreeNode) {
ctx.lineTo(node.point.x, node.point.y);
if (node.leftChild) {
this.lineToNode(ctx, node.leftChild);
}
ctx.moveTo(node.point.x, node.point.y);
if (node.rightChild) {
this.lineToNode(ctx, node.rightChild);
}
}
}