-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20181115.ts
More file actions
67 lines (57 loc) · 1.53 KB
/
20181115.ts
File metadata and controls
67 lines (57 loc) · 1.53 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
import Doodle from "./Doodle";
import { Point } from "./Point";
import { RandomColor } from "./Colors";
const screenSize = 128;
const gridCount = 16;
const gridSize = screenSize / gridCount;
const frameCount = 1;
export class DailyDoodle implements Doodle {
private cells: Point[];
private index: number;
private frame: number;
private colors: string[];
public init() {
this.cells = [];
this.colors = [];
this.index = 0;
this.frame = 0;
const doneCells: string[] = [];
for (let i = 0; i < gridCount * gridCount; i++) {
const p = new Point(
Math.floor(gridCount * Math.random()),
Math.floor(gridCount * Math.random())
);
while (doneCells.indexOf(`${p.x},${p.y}`) !== -1) {
p.x = Math.floor(gridCount * Math.random());
p.y = Math.floor(gridCount * Math.random());
}
doneCells.push(`${p.x},${p.y}`);
this.cells.push(p);
this.colors.push(RandomColor());
}
}
public update() {
this.frame++;
if (this.frame >= frameCount) {
this.index++;
if (this.index >= gridCount * gridCount) {
this.index = 0;
}
this.frame = 0;
}
}
public draw(ctx: CanvasRenderingContext2D) {
ctx.clearRect(0, 0, screenSize, screenSize);
for (let i = 0; i < this.index; i++) {
ctx.fillStyle = this.colors[i];
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
ctx.fillRect(
this.cells[i].x * gridSize,
this.cells[i].y * gridSize,
gridSize,
gridSize
);
}
}
}