-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandom.ts
More file actions
141 lines (117 loc) · 3.45 KB
/
random.ts
File metadata and controls
141 lines (117 loc) · 3.45 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
function random(max: number) {
return Math.floor(Math.random() * max);
}
class CanvasPainting {
viewport: HTMLCanvasElement;
ctx: CanvasRenderingContext2D;
width: number;
height: number;
constructor(viewport: HTMLCanvasElement) {
this.viewport = viewport;
this.width = viewport.width = window.innerWidth || document.body.clientWidth;
this.height = viewport.height = window.innerHeight || document.body.clientHeight;
const ctx = viewport.getContext('2d');
if (!ctx) {
throw new Error("Failed to getContext from canvas element");
}
this.ctx = ctx;
}
square(startX: number, startY: number) {
this.ctx.fillRect(startX, startY, 50, 50);
}
triangle(startX: number, startY: number) {
this.ctx.beginPath();
this.ctx.moveTo(startX, startY);
this.ctx.lineTo(startX + 50, startY);
this.ctx.lineTo(startX + 25, startY - 50);
this.ctx.fill();
}
circle(startX: number, startY: number) {
this.ctx.beginPath();
this.ctx.arc(startX, startY, 25, 0, Math.PI * 2, true);
this.ctx.fill();
}
}
class Lines extends CanvasPainting {
render() {
this.ctx.lineWidth = 3;
// Between 5 and 10 lines
const lineCount = 5 + random(5);
const colors = ['deeppink', 'darkorange', 'lime'];
for (let i = 0; i < lineCount; i++) {
const color = colors[random(colors.length)];
const startX = random(this.width);
const startY = random(this.height / 2);
const endX = random(this.width);
const endY = random(this.height / 2) + (this.height / 2);
this.ctx.strokeStyle = color;
this.ctx.beginPath();
this.ctx.moveTo(startX, startY);
this.ctx.lineTo(endX, endY);
this.ctx.stroke();
}
}
}
class Shapes extends CanvasPainting {
[key: string]: any;
render() {
this.ctx.lineWidth = 3;
const shapes = ['square', 'triangle', 'circle'];
const colors = [
'rgba(255, 0, 0, 0.8)', // Red
'rgba(255, 255, 0, 0.8)', // Yellow
'rgba(0, 0, 255, 0.8)' // Blue
];
const shapeCount = 18;
for (let i = 0; i < shapeCount; i++) {
const color = colors[random(colors.length)];
const shape = shapes[random(shapes.length)];
const startX = random(this.width);
const startY = random(this.height);
this.ctx.fillStyle = color;
this[shape](startX, startY);
}
}
}
class Combo extends CanvasPainting {
render() {
randomBackgroundColor();
Lines.prototype.render.call(this);
Shapes.prototype.render.call(this);
}
}
function randomBackgroundColor() {
const rgb = [
random(255),
random(255),
random(255)
].join(",");
document.body.style.cssText = `background-color: rgb(${rgb})`;
}
function changingRandomBackgroundColor() {
return setInterval(randomBackgroundColor, 500);
}
function renderShapes() {
const viewport = <HTMLCanvasElement | null>document.getElementById('viewport');
if (!viewport) {
return;
}
const shapes = new Shapes(viewport);
shapes.render();
}
function renderLines() {
const viewport = <HTMLCanvasElement | null>document.getElementById('viewport');
if (!viewport) {
throw new Error("Couldn't find element with id 'viewport'");
}
const lines = new Lines(viewport);
lines.render();
}
function renderCombo() {
const viewport = <HTMLCanvasElement | null>document.getElementById('viewport');
if (!viewport) {
throw new Error("Couldn't find element with id 'viewport'");
}
const combo = new Combo(viewport);
combo.render();
}