Click to randomize. Press “P” to toggle background painting. Press the spacebar to pause. Press “A” to toggle auto-randomizing. Press “C” to randomize colors. Press “S” to randomize the shape. Press “X” to randomize the number of axis. Press “+” and “-” to change the number of axis. Press “B” to clear the background. Press “D” to change the amount of decay when the background isn’t painting. Press “F” to show the frame rate.
|
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
PFont font; Part part; int time = 1; int r1, r2, r3, r4; int numAxis = 1; color bgColor; boolean showsFPS = false; boolean doesPaint = true; boolean autoplay = true; boolean isPaused = false; ColorPalette cp; float spread = 5; float osc1, osc2; void setup() { size(940, 540); colorMode(HSB); background(0); strokeWeight(0.5); font = createFont("Helvetica", 24); bgColor = color(0, 10); randomize(); part = new Part(0, 0); } void draw() { if (!isPaused) { if (!doesPaint) { fill(bgColor); noStroke(); rect(0, 0, width, height); } if (showsFPS) displayFPS(); if (autoplay) { time++; if (time > 700) randomize(); } translate(width/2, height/2); noFill(); osc1 += 0.0011; osc2 += 0.0013; r1 = abs(sin(osc1) * 0.009) + 0.001; r2 = abs(sin(osc2) * 0.009) + 0.001; part.draw(); } } void randomize() { time = 1; if (doesPaint) background(0); randomizeColor(); randomizeShape(); randomizeAxis(); } void randomizeShape() { if (doesPaint) background(0); int bottom = 0.001; int top = 0.01; r1 = random(bottom, top); r2 = random(bottom, top); r3 = random(bottom, top); r4 = random(bottom, top); spread = random(1, 20); } void randomizeAxis() { if (doesPaint) background(0); numAxis = round(random(2, 30)); } void randomizeColor() { cp = new ColorPalette(); } void mouseClicked() { randomize(); autoplay = false; } void keyPressed() { if (key == 'f') showsFPS = !showsFPS; if (key == 'd') { float a = alpha(bgColor); if (a == 10) a = 40; else a = 10; bgColor = color(0, a); } if (key == 'p') doesPaint = !doesPaint; if (key == 'a') autoplay = !autoplay; if (key == ' ') isPaused = !isPaused; if (key == 'c') randomizeColor(); if (key == 's') randomizeShape(); if (key == 'x') randomizeAxis(); if (key == 'b') background(0); if (key == '=') { numAxis++; if (doesPaint) background(0); } if (key == '-') { numAxis--; if (doesPaint) background(0); } if (numAxis < 1) numAxis = 1; if (numAxis > 30) numAxis = 30; } void displayFPS() { textFont(font, 18); fill(255); String output = "fps="; output += (int) frameRate; text(output, 10, 30); } class ColorPalette { PGraphics pg; float w = 300; color[] allColors; ColorPalette() { createPalette(); allColors = new color[w]; for (int i=0; i<w; i++) { allColors[i] = pg.get(i, 0); } } color getColorAtRatio(float _ratio) { int index = floor(w * _ratio); return allColors[index]; } void createPalette() { pg = createGraphics(w, 1); pg.colorMode(HSB); pg.beginDraw(); pg.noStroke(); color baseColor = color(random(255), random(255, 255), random(255, 255)); pg.background(baseColor); int numColors = 6; for (int i=0; i<numColors; i++) { addColor(); } pg.endDraw(); } void addColor() { color c = color(random(255), random(0, 255), random(150, 255)); float pos = random(w); float size = w/4; for (int i=0; i<w; i++) { float ratio = 0; float d = abs(i-pos); if (d < size) { ratio = 1.0 - (d/size); } pg.fill(hue(c), saturation(c), brightness(c), (ratio)*255); pg.rect(i, 0, 1, 1); } } void draw() { image(pg, 0, height/2, width, 10); } } class Part { PVector pos[]; int trailLength = 100; float speed = 0.03; float o1, o2, o3, o4; Part(float _x, float _y) { pos = new PVector[trailLength]; for (int i=0; i<trailLength; i++) { pos[i] = new PVector(0, 0); } } void draw() { float step = radians(360/numAxis); float x, y; for (int i=0; i<trailLength; i++) { o1 += r1*speed; o2 += r2*speed; o3 += r3*speed; o4 += r4*speed; x = sin(o1 + i*spread*r1) * 250; y = cos(o2 + i*spread*r2) * 100; x += sin(o3 + i*spread*r3) * 250; y += cos(o4 + i*spread*r4) * 100; pos[i].x += (x - pos[i].x) * 0.1; pos[i].y += (y - pos[i].y) * 0.1; } for (int i=1; i<trailLength; i++) { stroke(cp.getColorAtRatio(i/trailLength)); pushMatrix(); for (int j=0; j<numAxis; j++) { rotate(step); line(pos[i].x, pos[i].y, pos[i-1].x, pos[i-1].y); } popMatrix(); } } } |








