-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
197 lines (157 loc) · 3.75 KB
/
sketch.js
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
/**
* p5.js sketch.
*/
// colors definition
const BACKGROUND_COLOR = '#fbe6c2';
const ENTRY_COLOR = '#c15050';
const INFO_COLOR = '#d97642'
const TURTLE_COLOR = '#a3d2ca'
// fonts definition
const LARGE = 45;
const MEDIUM = 30;
let chango;
// define L-system state
let iteration = 1;
let currentGrammar = 0;
let lsystem;
/**
* p5.js pre-setup function.
*/
function preload()
{
// load 'Chango' font
chango = loadFont('assets/Chango-Regular.ttf');
}
/**
* p5.js setup function.
*/
function setup()
{
// create drawing canvas
createCanvas(windowWidth, windowHeight);
// stop drawing loop
noLoop();
// draw initial grammar
loadGrammar();
// clean background
background(BACKGROUND_COLOR);
}
/**
* p5.js draw function.
*/
function draw()
{
// clean background
background(BACKGROUND_COLOR);
// draw current L-system state
turtle.draw(lsystem.state);
// display grammar info
displayInfo();
}
/**
* p5.js mouse clicked callback.
*/
function mouseClicked()
{
if (iteration === GRAMMARS[currentGrammar].maxDerivations)
{
iteration = 1;
nextScene();
}
else
{
iteration += 1;
nextDerivation();
}
}
/**
* Grammar setup.
*/
function loadGrammar()
{
// load grammar parameters
grammar = GRAMMARS[currentGrammar];
// restart L-system
lsystem = new LSystem(grammar.axiom, grammar.ruleset);
// restart turtle drawer
turtle = new Turtle(
grammar.x,
grammar.y,
grammar.distance,
radians(grammar.angle),
grammar.decay
);
}
/**
* Forward sketch to next grammar.
*/
function nextScene()
{
// get next grammar index
currentGrammar + 1 === GRAMMARS.length ? currentGrammar = 0 : currentGrammar++;
// setup next grammar
loadGrammar();
// call p5.js drawing function
redraw();
}
/**
* Get L-system next derivation.
*/
function nextDerivation()
{
// derivate next L-system state
lsystem.derive();
// call p5.js drawing function
redraw();
}
/**
* Displays info about current sketch state.
*/
function displayInfo()
{
fill(ENTRY_COLOR);
stroke(ENTRY_COLOR);
textFont(chango, LARGE);
text('System:', 50, (windowHeight * 0.15))
fill(INFO_COLOR);
stroke(INFO_COLOR);
textSize(MEDIUM);
text(`(${currentGrammar + 1}/${GRAMMARS.length})`, 50, (windowHeight * 0.2))
fill(ENTRY_COLOR);
stroke(ENTRY_COLOR);
textFont(chango, LARGE);
text('Axiom:', 50, (windowHeight * 0.3))
fill(INFO_COLOR);
stroke(INFO_COLOR);
textSize(MEDIUM);
text(`${GRAMMARS[currentGrammar].axiom}`, 50, (windowHeight * 0.35))
fill(ENTRY_COLOR);
stroke(ENTRY_COLOR);
textSize(LARGE);
text('Iteration:', 50, (windowHeight * 0.45))
fill(INFO_COLOR);
stroke(INFO_COLOR);
textSize(MEDIUM);
text(`(${iteration}/${GRAMMARS[currentGrammar].maxDerivations})`, 50, (windowHeight * 0.5))
fill(ENTRY_COLOR);
stroke(ENTRY_COLOR);
textSize(LARGE);
text('Instructions:', 50, (windowHeight * 0.6))
fill(INFO_COLOR);
stroke(INFO_COLOR);
textSize(MEDIUM);
text(`${lsystem.state.length}`, 50, (windowHeight * 0.65))
fill(ENTRY_COLOR);
stroke(ENTRY_COLOR);
textSize(LARGE);
text(`Ruleset:`, 50, (windowHeight * 0.75))
let i = 0.8;
for (let rule in GRAMMARS[currentGrammar].ruleset)
{
fill(INFO_COLOR);
stroke(INFO_COLOR);
textSize(MEDIUM);
text(`${rule}: ${GRAMMARS[currentGrammar].ruleset[rule]}`, 50, (windowHeight * i));
i += 0.05;
}
}