-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
232 lines (207 loc) · 7.18 KB
/
index.tsx
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import * as React from "react";
import { render } from "react-dom";
import { HashRouter, Link, Route, RouteComponentProps, withRouter } from "react-router-dom";
import "./index.scss";
import { Annulus } from "./sketches/annulus";
import { Astroid } from "./sketches/astroid";
import { Blankets } from "./sketches/blankets";
import { BloodySpiderWeb } from "./sketches/bloody-spider-web";
import { ChristmasSpiralTree } from "./sketches/spiral-christmas-tree";
import { CliffordAttractors } from "./sketches/clifford-attractors";
import { CubicDisarray } from "./sketches/cubic-disarray";
import { Cuts } from "./sketches/cuts";
import { GalaxyMap } from "./sketches/galaxy-map";
import { ISketch } from "./sketches/sketch";
import { Isolines } from "./sketches/isolines";
import { LightInACave } from "./sketches/light-in-a-cave";
import { NeonLines } from "./sketches/neon-lines";
import { Nucleus } from "./sketches/nucleus";
import { Print10 } from "./sketches/print10";
import { RoughBalls } from "./sketches/rough-balls";
import { SpaceFillingCurves } from "./sketches/space-filling-curves";
import { StaticContext } from "react-router";
import { SuperPermutations } from "./sketches/super-permutations";
import { Walls } from "./sketches/walls";
import { Roses } from "./sketches/roses";
import { CairoTiling } from "./sketches/cairo-tiling";
import { PenroseTiling } from "./sketches/penrose-tiling";
import { TruchetTiles } from "./sketches/truchet-tiles";
import { TriangularMaze } from "./sketches/triangular-maze";
import { CircularMaze } from "./sketches/circular-maze";
import { Dla } from "./sketches/dla";
import { ParallelBands } from "./sketches/parallel-bands";
import { GooglyEyes } from "./sketches/eyes";
import { Scribbles } from "./sketches/scribbles";
import { FocusEye } from "./sketches/focus-eye";
import { Sorting } from "./sketches/sorting";
import { BlackWhiteRain } from "./sketches/bw-rain";
import { Rots } from "./sketches/rots";
import { NoiseQuads } from "./sketches/noise-quads";
import { Voronoi } from "./sketches/vornoi";
import { NoiseSymmetry } from "./sketches/symmetry";
import { SpiralNoise } from "./sketches/spiral-noise";
// from older to most recent
export const SKETCHES = [
new Print10(),
new CubicDisarray(),
new BloodySpiderWeb(),
new Annulus(),
new Astroid(),
new NeonLines(),
new Nucleus(),
new RoughBalls(),
new Blankets(),
new ChristmasSpiralTree(),
new LightInACave(),
new Cuts(),
new Walls(),
new SuperPermutations(),
new SpaceFillingCurves(),
new GalaxyMap(),
new CliffordAttractors(),
new Isolines(),
new Roses(),
new CairoTiling(),
new PenroseTiling(),
new TruchetTiles(),
new TriangularMaze(),
new CircularMaze(),
new Dla(),
new ParallelBands(),
new GooglyEyes(),
new Scribbles(),
new FocusEye(),
new Sorting(),
new BlackWhiteRain(),
new Rots(),
new NoiseQuads(),
new Voronoi(),
new NoiseSymmetry(),
new SpiralNoise(),
];
const sketchesMap = new Map(SKETCHES.map(s => [s.name, s] as [string, ISketch]));
class SketchSelector extends React.PureComponent<RouteComponentProps<any, StaticContext, any>> {
public render() {
const sketches = [];
for (const sketchName of sketchesMap.keys()) {
sketches.push(
<li className="menu-item" key={sketchName}>
<Link to={`sketch/${sketchName}`}>{sketchName}</Link>
</li>,
);
}
// show from latest to older
sketches.reverse();
return (
<div className="container">
<h1 className="text-center">
<ul className="breadcrumb">
<li className="breadcrumb-item">Matto</li>
</ul>
</h1>
<div className="columns">
<div className={"column col-4 col-sm-8 col-mx-auto"}>
<p>
Matto is a generative art playground built on top of Typescript and
Rust. It also uses the p5js library.
</p>
<ul className="menu">{sketches}</ul>
</div>
</div>
</div>
);
}
}
interface ISketchIProps {
match: {
params: {
sketchId: string;
};
};
}
class Sketch extends React.Component<ISketchIProps, {}> {
public static readonly CANVAS_ID = "piece-canvas-container";
public render() {
return (
<div className="container">
<h1 className="text-center">
<ul className="breadcrumb">
<li className="breadcrumb-item">
<Link to="/">Matto</Link>
</li>
<li className="breadcrumb-item">{this.props.match.params.sketchId}</li>
</ul>
</h1>
<div className="columns">
<div className={"column col-10 col-sm-12 col-mx-auto"}>
<div>
<div id={Sketch.CANVAS_ID} />
</div>
</div>
</div>
</div>
);
}
public componentDidMount() {
this.runSketch(this.props.match.params.sketchId);
}
private runSketch(sketchName: string) {
const sketch = sketchesMap.get(sketchName);
if (sketch === undefined) {
throw new Error(`da fuck bro? ${sketchName} is not a valid sketch`);
}
return new (p5 as any)((p: p5) => {
p.setup = () => {
p.createCanvas(sketch.width, sketch.height);
sketch.reset(p);
};
p.draw = () => {
p.push();
sketch.draw(p);
if (!sketch.loop) {
p.noLoop();
}
p.pop();
};
p.mouseClicked = () => {
if (p.mouseButton !== p.LEFT) {
return false;
}
sketch.reset(p);
p.draw();
return false;
};
p.keyPressed = () => {
if (p.key === " ") {
sketch.reset(p);
p.draw();
} else if (p.key === "s") {
p.noLoop();
} else if (p.key === "p" && sketch.loop) {
p.loop();
} else if (p.key === "d") {
if (sketch.loop) {
p.noLoop();
}
p.save(`${sketch.name}.png`);
if (sketch.loop) {
p.loop();
}
} else {
return;
}
return false;
};
}, Sketch.CANVAS_ID);
}
}
const mountNode = document.getElementById("app");
render(
<HashRouter>
<div>
<Route path="/sketch/:sketchId" component={Sketch} />
<Route exact path="/" component={withRouter(SketchSelector)} />
</div>
</HashRouter>,
mountNode,
);