208
2p
2d-particle-experiements / files
2d-particle-experiements / js / script.js
8.0 KB
1/*jslint browser:true, plusplus:true, devel:true */ 2/*global $:false */ 3(function (win, doc) { 4 'use strict'; 5 6 var body = doc.querySelector('body'), 7 canvas = doc.createElement('canvas'), 8 c = canvas.getContext('2d'), 9 radians = Math.PI / 180; 10 11 body.appendChild(canvas); 12 canvas.width = win.innerWidth - 20; 13 canvas.height = win.innerHeight - 20 - 40; 14 15 /* tool */ 16 function rrange(min, max, noFloor) { 17 var result = Math.random() * (max - min) + min; 18 return noFloor ? result : Math.floor(result); 19 } 20 21 function stop() { 22 if (win.interval) { 23 clearInterval(win.interval); 24 } 25 26 return "Stopped"; 27 } 28 29 function clean() { 30 canvas.width = canvas.width; 31 canvas.height = canvas.height; 32 } 33 34 function Particle(x, y, size, color) { 35 this.x = x; 36 this.y = y; 37 this.size = size; 38 this.color = color || rrange(1, 359); 39 } 40 41 Particle.prototype = { 42 draw: function () { 43 c.save(); 44 c.fillStyle = "hsl(" + this.color + ", 100%, 50%)"; 45 c.beginPath(); 46 c.arc(this.x, this.y, this.size, 0, Math.PI * 2, true); 47 c.fill(); 48 c.restore(); 49 } 50 }; 51 52 function tiles() { 53 var w = canvas.width, 54 h = canvas.height, 55 r = 20, 56 rows = Math.floor(w / r), 57 cols = Math.floor(h / r), 58 color = 0, 59 n, 60 m; 61 62 for (n = 0; n < cols; ++n) { 63 for (m = 0; m < rows; ++m) { 64 65 c.strokeStyle = "hsl(" + 66 (color === 360 ? color = 0 : color += 0.2) + 67 ", 100%, 50%)"; 68 69 c.beginPath(); 70 71 c.arc(r + n * (r * 2), 72 r + m * (r * 2), 73 Math.floor(r / 4) * 3, 74 0, 75 Math.PI * 2, true); 76 c.stroke(); 77 78 } 79 } 80 81 } 82 83 function grid() { 84 var w = canvas.width, 85 h = canvas.height, 86 mod = 100, 87 size = Math.floor(w / mod), 88 i; 89 90 canvas.width = size * mod + 2; 91 canvas.style.width = size * mod + 2; 92 93 c.save(); 94 c.moveTo(0, 0); 95 i = mod; 96 while (i) { 97 c.moveTo(size * i, 0); 98 c.lineTo(size * i, h); 99 --i; 100 } 101 102 c.moveTo(0, 0); 103 i = Math.floor(h / size); 104 while (i) { 105 c.moveTo(0, size * i); 106 c.lineTo(w, size * i); 107 --i; 108 } 109 110 c.stroke(); 111 c.restore(); 112 } 113 114 function spiral() { 115 var particle, 116 particles = [], 117 maxParticles = 6000, 118 angle = 0, 119 size = 5, 120 x = 0, 121 y = 0, 122 xyModifier = 1.1, 123 color = 180; 124 125 clean(); 126 127 c.save(); 128 c.translate(canvas.width / 2, canvas.height / 2); 129 130 while (--maxParticles > 0) { 131 angle += 2; 132 y += xyModifier; 133 x += xyModifier; 134 color = color >= 360 ? 180 : color + 1; 135 136 if (!(x >= canvas.width || y >= canvas.height || size <= 0)) { 137 c.save(); 138 c.rotate(angle * radians); 139 140 particle = new Particle(x, y, size, color); 141 particles.push(particle); 142 particle.draw(); 143 144 c.restore(); 145 } 146 } 147 148 c.restore(); 149 150 return "Look at the pretty dots!"; 151 } 152 153 /* recursion play */ 154 function fractal() { 155 var gens = 0, 156 max = 5, 157 size = 4, 158 startColor = rrange(260, 360); 159 160 clean(); 161 162 c.save(); 163 c.translate(canvas.width / 2, canvas.height / 2); 164 new Particle(0, 0, size, startColor).draw(); 165 166 (function init(angle, color, distance, scale) { 167 gens++; 168 angle = angle || rrange(1, 90); 169 170 c.save(); 171 c.rotate(angle * radians); 172 c.beginPath(); 173 c.moveTo(0, 0); 174 c.lineTo(distance, 0); 175 176 c.strokeStyle = 'hsl(' + color + ', 100%, 50%)'; 177 c.stroke(); 178 179 c.translate(distance, 0); 180 c.scale(scale, scale); 181 182 if (gens < max) { 183 new Particle(0, 0, size, color).draw(); 184 185 color = color >= 360 ? 1 : color + 20 * gens; 186 187 init(rrange(1, 270), color, rrange(200, 300), 188 rrange(0.75, 1, true)); 189 190 init(rrange(1, 270), color, rrange(200, 300), 191 rrange(0.75, 1, true)); 192 } 193 194 if (gens === max) { 195 new Particle(0, 0, size, color).draw(); 196 } 197 c.restore(); 198 gens--; 199 }(0, startColor, 0, 1)); 200 201 c.restore(); 202 203 return "Wicked man..."; 204 } 205 206 function rain() { 207 var dash, 208 dashes = [], 209 max = 4000, 210 startColor = 180, 211 endColor = 200, 212 startLeap = 0.5, 213 endLeap = 3; 214 215 clean(); 216 217 if (win.interval) { 218 clearInterval(win.interval); 219 } 220 221 win.interval = setInterval(function () { 222 var i = 0; 223 224 c.save(); 225 c.fillStyle = 'hsla(0, 0%, 0%, 0.2)'; 226 c.fillRect(0, 0, canvas.width, canvas.height); 227 c.restore(); 228 229 if (dashes.length <= max) { 230 dash = new Particle(rrange(1, canvas.width, true), 231 rrange(1, canvas.height, true), 232 rrange(startLeap, endLeap, true), 233 rrange(startColor, endColor)); 234 235 dashes.push(dash); 236 } 237 238 for (i; i < dashes.length; i++) { 239 dash = dashes[i]; 240 dash.y = dash.y >= canvas.height ? 241 rrange(1, canvas.height / 2) : 242 dash.y + rrange(1, 6); 243 244 dash.draw(); 245 } 246 }, 1000 / 30); 247 248 return "It's raining..."; 249 } 250 251 function circle(x, y, soft) { 252 var particle, 253 max = 12, 254 angleMod = 360 / max, 255 radius = 90; 256 257 x = x || canvas.width / 2; 258 y = y || canvas.height / 2; 259 260 261 if (soft) { 262 c.save(); 263 c.fillStyle = 'rgba(0, 0, 0, 0.3)'; 264 c.fillRect(0, 0, canvas.width, canvas.height); 265 c.restore(); 266 } else { 267 clean(); 268 } 269 270 c.save(); 271 c.translate(x, y); 272 273 while (max) { 274 275 particle = new Particle(radius, radius, 8, angleMod * max); 276 particle.draw(); 277 278 c.rotate(angleMod * radians); 279 280 max -= 1; 281 } 282 283 c.restore(); 284 285 return "It's magic"; 286 } 287 288 function trackingCircle() { 289 var x = 0, 290 y = 0, 291 angle = 1; 292 293 canvas.addEventListener('mousemove', function (e) { 294 x = e.clientX; 295 y = e.clientY; 296 }, false); 297 298 win.interval = setInterval(function () { 299 circle(x, y, true); 300 }, 1000 / 60); 301 302 return "tracking..."; 303 304 } 305 306 win.spiral = spiral; 307 win.fractal = fractal; 308 win.rain = rain; 309 win.stop = stop; 310 win.clean = clean; 311 win.circle = circle; 312 win.tracking = trackingCircle; 313 314 win.help = function () { 315 console.log([ 316 'Welcome to particle playground!', 317 '', 318 '(Warning: you might lock up your browser)', 319 '', 320 'There are 3 functions for you to use:', 321 ' spiral();', 322 ' fractal();', 323 ' rain(); and stop(); (stop calls off the rain)', 324 ' circle();', 325 ' tracking();', 326 'No arguments (comming soon)', 327 '', 328 'You can see this prompt by calling help();' 329 ].join('\n')); 330 }; 331 332 $('.fn').click(function (e) { 333 var fn = $(e.target).data('fn'); 334 win[fn](); 335 }); 336 337 $(function () { 338 win.help(); 339 //grid(); 340 tiles(); 341 }); 342 343}(window, document));