Skip to content

Commit 2ebec66

Browse files
committed
P_3_1_2: Code style improvements
1 parent 704366e commit 2ebec66

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

01_P/P_3_1_2_01/sketch.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// P_3_1_2_01.pde
1+
// P_3_1_2_01
22
//
33
// Generative Gestaltung, ISBN: 978-3-87439-759-9
44
// First Edition, Hermann Schmidt, Mainz, 2009
@@ -20,7 +20,7 @@
2020
* typewriter. uses input (text) as blueprint for a visual composition.
2121
*
2222
* MOUSE
23-
* drag : move canvas
23+
* click + drag : move canvas
2424
*
2525
* KEYS
2626
* a-z : text input (keyboard)
@@ -82,12 +82,14 @@ function setup() {
8282
offsetX = 0;
8383
offsetY = 0;
8484
zoom = 0.75;
85+
8586
actRandomSeed = 6;
8687

8788
cursor(HAND);
8889
textFont(font, 25);
8990
textAlign(LEFT, BASELINE);
90-
imageMode(CORNER);
91+
noStroke();
92+
fill(0);
9193
}
9294

9395
function windowResized() {
@@ -96,7 +98,6 @@ function windowResized() {
9698

9799
function draw() {
98100
background(255);
99-
noStroke();
100101

101102
if (mouseIsPressed && mouseButton == LEFT) {
102103
centerX = mouseX - offsetX;
@@ -161,15 +162,13 @@ function draw() {
161162
break;
162163

163164
default: // all others
164-
fill(0);
165165
text(letter, 0, 0);
166166
translate(letterWidth, 0);
167167
}
168168
}
169169

170170
// blink cursor after text
171-
fill(0);
172-
if (int(frameCount / 6) % 2 == 0) rect(0, 0, 15, 2);
171+
if (frameCount / 6 % 2 == 0) rect(0, 0, 15, 2);
173172
}
174173

175174

@@ -192,9 +191,6 @@ function keyPressed() {
192191
textTyped = textTyped.substring(0, max(0, textTyped.length - 1));
193192
print(textTyped);
194193
break;
195-
case TAB:
196-
case ESCAPE:
197-
break;
198194
case ENTER:
199195
case RETURN:
200196
// enable linebreaks
@@ -212,5 +208,6 @@ function keyPressed() {
212208
function keyTyped() {
213209
if (keyCode >= 32) {
214210
textTyped += key;
211+
print(textTyped);
215212
}
216213
}

01_P/P_3_1_2_02/sketch.js

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// P_3_1_2_02.pde
1+
// P_3_1_2_02
22
//
33
// Generative Gestaltung, ISBN: 978-3-87439-759-9
44
// First Edition, Hermann Schmidt, Mainz, 2009
@@ -24,42 +24,43 @@
2424
*
2525
* KEYS
2626
* a-z : text input (keyboard)
27-
* space : random straight / small curve
2827
* ,.!? : curves
28+
* space : random straight / small curve
2929
* :+-xz : icons
3030
* o : station with the last 7 typed letters as name
3131
* a u : stop
3232
* del, backspace : remove last letter
3333
* arrow up : zoom canvas +
3434
* arrow down : zoom canvas -
35+
* alt : new random layout
3536
* ctrl : save png
3637
*/
3738
'use strict';
3839

40+
var textTyped = 'Was hier folgt ist Tet! So asnt, und mag. Ich mag Tet sehr.';
3941
var font;
40-
var textTyped = "Was hier folgt ist Tet! So asnt, und mag. Ich mag Tet sehr.";
4142

4243
var shapeSpace;
4344
var shapeSpace2;
4445
var shapePeriod;
4546
var shapeComma;
46-
var shapeExclamationmark;
47-
4847
var shapeQuestionmark;
48+
var shapeExclamationmark;
4949
var shapeReturn;
5050
var icon1;
5151
var icon2;
5252
var icon3;
5353
var icon4;
5454
var icon5;
5555

56-
var centerX = 0;
57-
var centerY = 0;
58-
var offsetX = 0;
59-
var offsetY = 0;
60-
var zoom = 0.75;
56+
var centerX;
57+
var centerY;
58+
var offsetX;
59+
var offsetY;
60+
var zoom;
61+
62+
var actRandomSeed;
6163

62-
//new RGBColour(0, 130, 164);
6364
var palette = [
6465
[253, 195, 0],
6566
[0, 0, 0],
@@ -92,17 +93,21 @@ function preload() {
9293
function setup() {
9394
createCanvas(windowWidth, windowHeight);
9495

95-
textFont(font, 25);
96+
centerX = width / 2;
97+
centerY = height / 2;
98+
offsetX = 0;
99+
offsetY = 0;
100+
zoom = 0.75;
101+
actRandomSeed = 6;
102+
96103
cursor(HAND);
104+
textFont(font, 25);
105+
textAlign(LEFT, BASELINE);
97106
noStroke();
98107
fill(0);
99-
100-
centerX = width / 2;
101-
centerY = height / 2;
102108
}
103109

104110
function windowResized() {
105-
// resize canvas when window is resized
106111
resizeCanvas(windowWidth, windowHeight);
107112
}
108113

@@ -114,27 +119,23 @@ function draw() {
114119
centerY = mouseY - offsetY;
115120
}
116121

122+
// allways produce the same sequence of random numbers
123+
randomSeed(actRandomSeed);
124+
117125
translate(centerX, centerY);
118126
scale(zoom);
119127

120-
push();
121-
122-
randomSeed(0);
123-
124128
actColorIndex = 0;
125129
fill(palette[actColorIndex][0], palette[actColorIndex][1], palette[actColorIndex][2]);
126130
rect(0, -25, 10, 35);
127131

128132
for (var i = 0; i < textTyped.length; i++) {
129-
var fontSize = 25;
130-
textFont(font, fontSize);
131133
var letter = textTyped.charAt(i);
132134
var letterWidth = textWidth(letter);
133135

134136
// ------ letter rule table ------
135137
switch (letter) {
136138
case ' ': //space
137-
//60% notrun, 20% left, 20% right
138139
var dir = floor(random(5));
139140
if (dir == 0) {
140141
image(shapeSpace, 0, -15);
@@ -147,26 +148,31 @@ function draw() {
147148
rotate(-QUARTER_PI);
148149
}
149150
break;
151+
150152
case ',':
151153
image(shapeComma, 0, -15);
152154
translate(33, 15);
153155
rotate(QUARTER_PI);
154156
break;
157+
155158
case '.':
156159
image(shapePeriod, 0, -56);
157160
translate(56, -56);
158161
rotate(-HALF_PI);
159162
break;
163+
160164
case '!':
161165
image(shapeExclamationmark, 0, -30);
162166
translate(43, -18);
163167
rotate(-QUARTER_PI);
164168
break;
169+
165170
case '?':
166171
image(shapeQuestionmark, 0, -30);
167172
translate(43, -18);
168173
rotate(-QUARTER_PI);
169174
break;
175+
170176
case '\n':
171177
// start a new line at a random position near the center
172178
rect(0, -25, 10, 35);
@@ -178,6 +184,7 @@ function draw() {
178184
fill(palette[actColorIndex][0], palette[actColorIndex][1], palette[actColorIndex][2]);
179185
rect(0, -25, 10, 35);
180186
break;
187+
181188
case 'o': // Station big
182189
rect(0, -15, letterWidth + 1, 15);
183190
push();
@@ -193,31 +200,39 @@ function draw() {
193200
pop();
194201
translate(letterWidth, 0);
195202
break;
203+
196204
case 'a': // Station small left
197205
rect(0, 0 - 15, letterWidth + 1, 25);
198206
rect(0, 0 - 15, letterWidth + 1, 15);
199207
translate(letterWidth, 0);
200208
break;
209+
201210
case 'u': // Station small right
202211
rect(0, 0 - 25, letterWidth + 1, 25);
203212
rect(0, 0 - 15, letterWidth + 1, 15);
204213
translate(letterWidth, 0);
205214
break;
215+
206216
case ':': // icon
207217
image(icon1, 0, -60, 30, 30);
208218
break;
219+
209220
case '+': // icon
210221
image(icon2, 0, -60, 35, 30);
211222
break;
223+
212224
case '-': // icon
213225
image(icon3, 0, -60, 30, 30);
214226
break;
227+
215228
case 'x': // icon
216229
image(icon4, 0, -60, 30, 30);
217230
break;
231+
218232
case 'z': // icon
219233
image(icon5, 0, -60, 30, 30);
220234
break;
235+
221236
default: // all others
222237
rect(0, -15, letterWidth + 1, 15);
223238
translate(letterWidth, 0);
@@ -227,8 +242,6 @@ function draw() {
227242
// blink cursor after text
228243
fill(200, 30, 40);
229244
if (frameCount / 6 % 2 == 0) rect(0, 0, 15, 2);
230-
231-
pop();
232245
}
233246

234247
function mousePressed() {
@@ -238,6 +251,7 @@ function mousePressed() {
238251

239252
function keyReleased() {
240253
if (keyCode == CONTROL) saveCanvas(gd.timestamp(), 'png');
254+
if (keyCode == ALT) actRandomSeed++;
241255
}
242256

243257
function keyPressed() {

0 commit comments

Comments
 (0)