-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20181116.ts
More file actions
159 lines (144 loc) · 3.93 KB
/
20181116.ts
File metadata and controls
159 lines (144 loc) · 3.93 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
import Doodle from "./Doodle";
import { Point } from "./Point";
import { Colors } from "./Colors";
const screenSize = 128;
const frameCount = 15;
const spriteWidth = 11;
const spriteHeight = 8;
const pixelSize = 2;
export class DailyDoodle implements Doodle {
private frame: number;
private spriteToggle: boolean;
public init() {
this.frame = 0;
this.spriteToggle = false;
}
public update() {
this.frame++;
if (this.frame >= frameCount) {
this.frame = 0;
this.spriteToggle = !this.spriteToggle;
}
}
public draw(ctx: CanvasRenderingContext2D) {
ctx.clearRect(0, 0, screenSize, screenSize);
ctx.fillStyle = Colors.white;
const grid = Math.floor(
screenSize / (Math.max(spriteWidth, spriteHeight) * pixelSize)
);
for (let i = 0; i < grid * grid; i++) {
const c = i % grid;
const r = Math.floor(i / grid);
let isA = true;
if (r % 2 === 1) {
isA = !isA;
}
if (this.spriteToggle) {
isA = !isA;
}
let points: Point[] = [];
if (isA === true) {
points = this.spriteA(
new Point(
c * (pixelSize + spriteWidth),
r * (pixelSize + spriteWidth)
)
);
} else {
points = this.spriteB(
new Point(
c * (pixelSize + spriteWidth),
r * (pixelSize + spriteWidth)
)
);
}
for (const p of points) {
ctx.fillRect(p.x * pixelSize, p.y * pixelSize, pixelSize, pixelSize);
}
}
}
private spriteA(offset: Point): Point[] {
const points: Point[] = [];
const ox = offset.x;
const oy = offset.y;
// Row 1
points.push(new Point(ox + 2, oy + 0));
points.push(new Point(ox + 8, oy + 0));
// Row 2
points.push(new Point(ox + 3, oy + 1));
points.push(new Point(ox + 7, oy + 1));
// Row 3
for (let i = 2; i < 9; i++) {
points.push(new Point(ox + i, oy + 2));
}
// Row 4
for (let i = 1; i < 10; i++) {
if (i === 3 || i === 7) {
continue;
}
points.push(new Point(ox + i, oy + 3));
}
// Row 5
for (let i = 0; i < 11; i++) {
points.push(new Point(ox + i, oy + 4));
}
// Row 6
points.push(new Point(ox + 0, oy + 5));
points.push(new Point(ox + 10, oy + 5));
for (let i = 2; i < 9; i++) {
points.push(new Point(ox + i, oy + 5));
}
// Row 7
points.push(new Point(ox + 0, oy + 6));
points.push(new Point(ox + 2, oy + 6));
points.push(new Point(ox + 8, oy + 6));
points.push(new Point(ox + 10, oy + 6));
// Row 8
points.push(new Point(ox + 3, oy + 7));
points.push(new Point(ox + 4, oy + 7));
points.push(new Point(ox + 6, oy + 7));
points.push(new Point(ox + 7, oy + 7));
return points;
}
private spriteB(offset: Point): Point[] {
const points: Point[] = [];
const ox = offset.x;
const oy = offset.y;
// Row 1
points.push(new Point(ox + 2, oy + 0));
points.push(new Point(ox + 8, oy + 0));
// Row 2
points.push(new Point(ox + 0, oy + 1));
points.push(new Point(ox + 3, oy + 1));
points.push(new Point(ox + 7, oy + 1));
points.push(new Point(ox + 10, oy + 1));
// Row 3
points.push(new Point(ox + 0, oy + 2));
points.push(new Point(ox + 10, oy + 2));
for (let i = 2; i < 9; i++) {
points.push(new Point(ox + i, oy + 2));
}
// Row 4
for (let i = 0; i < 11; i++) {
if (i === 3 || i === 7) {
continue;
}
points.push(new Point(ox + i, oy + 3));
}
// Row 5
for (let i = 1; i < 10; i++) {
points.push(new Point(ox + i, oy + 4));
}
// Row 6
for (let i = 2; i < 9; i++) {
points.push(new Point(ox + i, oy + 5));
}
// Row 7
points.push(new Point(ox + 2, oy + 6));
points.push(new Point(ox + 8, oy + 6));
// Row 8
points.push(new Point(ox + 1, oy + 7));
points.push(new Point(ox + 9, oy + 7));
return points;
}
}