-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20181129.ts
More file actions
84 lines (74 loc) · 2.34 KB
/
20181129.ts
File metadata and controls
84 lines (74 loc) · 2.34 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
import Doodle from "./Doodle";
import { Colors } from "./Colors";
import { Point } from "./Point";
const screenSize = 128;
const frameDuration = 500;
const turnCount = 3;
const separation = 5;
export class DailyDoodle implements Doodle {
private t: number = 0;
private frame: number = 0;
private points: Point[] = [];
private color: string;
public init() {
this.points = [];
let spiralSize = 5;
let bottomLeft = new Point(
Math.floor(screenSize / 2),
Math.floor(screenSize / 2) + spiralSize
);
this.points.push(bottomLeft);
for (let i = 0; i < turnCount * 4; i++) {
const upperLeft = new Point(bottomLeft.x, bottomLeft.y - spiralSize);
const upperRight = new Point(upperLeft.x + spiralSize, upperLeft.y);
spiralSize += separation;
const bottomRight = new Point(upperRight.x, upperRight.y + spiralSize);
bottomLeft = new Point(bottomRight.x - spiralSize, bottomRight.y);
spiralSize += separation;
this.points.push(upperLeft);
this.points.push(upperRight);
this.points.push(bottomRight);
this.points.push(bottomLeft);
}
this.color = Colors.green;
}
public update() {
this.frame++;
if (this.frame > frameDuration) {
this.frame = 0;
this.init();
}
this.t = this.frame / frameDuration;
}
public draw(ctx: CanvasRenderingContext2D) {
ctx.clearRect(0, 0, screenSize, screenSize);
const drawPoints: Point[] = [this.points[0]];
const percentage = this.points.length * this.t;
const index = Math.floor(percentage);
const diff = percentage - index;
for (let i = 1; i < index; i++) {
drawPoints.push(this.points[i]);
}
if (diff > 0) {
if (index < this.points.length && index > 0) {
const lastPoint = this.points[index - 1];
const nextPoint = this.points[index];
const newPoint = new Point(
lastPoint.x + Math.floor((nextPoint.x - lastPoint.x) * diff),
lastPoint.y + Math.floor((nextPoint.y - lastPoint.y) * diff)
);
drawPoints.push(newPoint);
}
}
ctx.beginPath();
ctx.strokeStyle = this.color;
for (let i = 0; i < drawPoints.length; i++) {
if (i === 0) {
ctx.moveTo(drawPoints[0].x, drawPoints[0].y);
} else {
ctx.lineTo(drawPoints[i].x, drawPoints[i].y);
}
ctx.stroke();
}
}
}