One of the best tools in the motion design bag o’ tricks is the spring. Here we have a bunch of springs linked together – 500 to be exact. The lead particle moves by itself, or it will follow the mouse if it’s moving. To get the fading and blurring effects, we’re using a pair of PGraphics objects to render the scene, distort it a bit, and then render it back into the main scene. This is sometimes referred to as “rendering to a texture”, but it’s much simpler to accomplish in processing than when working with OpenGL directly.
|
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 |
PGraphics canvas; PGraphics auxCanvas; float fadeSpeed = 2.0; Chain chain; float startX; float startY; float osc = 0.0; float oscSpeed = 0.005; boolean isPaused = false; boolean chainIsActive = true; void setup() { size(940, 540); canvas = createGraphics(width, height); auxCanvas = createGraphics(width, height); startX = random(width); startY = random(height); chain = new Chain(); canvas.beginDraw(); canvas.background(0); canvas.endDraw(); auxCanvas.beginDraw(); auxCanvas.background(0); auxCanvas.endDraw(); canvas.colorMode(HSB, 255); auxCanvas.colorMode(HSB, 255); colorMode(HSB, 255); smooth(); canvas.smooth(); auxCanvas.smooth(); } void draw() { if (!isPaused) { osc += oscSpeed; float ratio = sin(osc); float invRatio = cos(osc); float offsetX = cos(osc) * 0.1; float offsetY = sin(osc) * 0.1; canvas.beginDraw(); canvas.image(auxCanvas, 0, 0, width, height); if (chainIsActive) chain.update(); canvas.endDraw(); image(canvas, 0, 0); auxCanvas.beginDraw(); auxCanvas.image(canvas, -fadeSpeed*0.5, -fadeSpeed*0.5, width+fadeSpeed, height+fadeSpeed); auxCanvas.endDraw(); } } boolean sketchFullScreen() { return false; } void keyPressed() { if (key == 'p') isPaused = !isPaused; if (key == 'c') chainIsActive = !chainIsActive; } class Chain { int numParts = 500; Part[] parts; float hue = random(TWO_PI); float saturation = 125; float brightness = 125; float osc = random(TWO_PI); float x, y, vx, vy; Chain() { x = startX; y = startY; vx = 0; vy = 0; createParts(); } void update() { canvas.noFill(); updateHue(); if (mouseX == pmouseX && mouseY == pmouseY) { vx += random(-1.0, 1.0) * 1.0; vy += random(-1.0, 1.0) * 1.0; vx *= 0.99; vy *= 0.99; } else { vx += (mouseX - x) * 0.01; vy += (mouseY - y) * 0.01; vx *= 0.9; vy *= 0.9; } x += vx; y += vy; if (x > width) { x = width; vx *= -1; } if (x < 0) { x = 0; vx *= -1; } if (y > height) { y = height; vy *= -1; } if (y < 0) { y = 0; vy *= -1; } updateParts(); } void updateParts() { for (int i=0; i<numParts; i++) { parts[i].update(); } } void createParts() { parts = new Part[numParts]; for (int i=0; i<numParts; i++) { Part part = new Part(); if (i > 0) part.leader = parts[i-1]; part.hueOffset = i * (255/6/numParts); parts[i] = part; } } void updateHue() { osc += 0.003; hue += 0.1; hue = hue % 255; saturation = (sin(osc)/2 + 0.5) * 255; brightness = (sin(osc*0.3)/2 + 0.5) * 255; } } class Part { float x, y, vx, vy; Part leader = null; float spring = 0.2; float friction = 0.56; float hueOffset = 0.0; float hue = 0.0; Part() { x = startX; y = startY; vx = 0.0; vy = 0.0; } void update() { if (leader == null) { vx += (chain.x - x) * spring; vy += (chain.y - y) * spring; } else { vx += (leader.x - x) * spring; vy += (leader.y - y) * spring; } vx *= friction; vy *= friction; x += vx; y += vy; if (leader != null) { hue = (chain.hue + hueOffset) % 255; canvas.stroke(hue, chain.saturation, chain.brightness, 255); canvas.line(x, y, leader.x, leader.y); } } } |




