View
Theme
Font Style
7pt
8pt
9pt
10pt
11pt
Line Style
100%
110%
120%
130%
140%
Bold Keyword
Default
Inspector
Kkaefer
Eclipse
SQ Light
Lesser
Dark
Cobalt
Monokai
Rubyblue
Night
SQ Dark
Ambiance
Blackboard
Line Num.
Wrap Lines
Preview
Redraw
JS Tab
HTML Tab
CSS Tab
Live Tab
Prev. Tab
Next Tab
Browser
History…
Help
Edit
Settings
Auto Complete
Match Brackets
Match Highlight
Strip Whitespace
Auto Close Brackets
Auto Close Quotes
Show Print Margin
Undo
Redo
Delete
Select Line
Select All
Find & Replace
Find
Find in Repo.
Find Next
Find Previous
Replace Single
Replace All
Wrap Search
Revert
As Template
Diff Revision
Format
Compress
Text
Zen Coding
Indent
Tab Width
1
2
3
4
5
6
7
8
Indent Unit
1
2
3
4
5
6
7
8
Smart Indent
Use Tabs
Visible Tabs
Shift Left
Shift Right
Put Indent
Number
Increment by 1
Decrement by 1
Increment by 0.1
Decrement by 0.1
Increment by 10
Decrement by 10
Simple Math
Comment
Line
Move Up
Move Down
Copy Up
Copy Down
Go to Line…
Remove Line
Next Point
Prev. Point
Help
Share
Login
You can jump to the latest bin by adding
/latest
to your URL
×
z
Find
→
←
⟲
Replace
⊗
All
Replace
/* SWARMS (v1.7) * by Barry van Oudtshoorn (www.barryvan.com.au) * A re-implementation of http://www.barryvan.com.au/demos/swarms/swarms.html, * which in turn is a reimplementation of a Processing script that I wrote. :) * * Male and female particles move around a central point, gradually aging. * When they reach maturity, they may interact with a particle of the opposite * sex to spawn a new particle. As they grow old, they become larger and slower * until they eventually die. * * Revisions (really, these version numbers are all but meaningless!) * 1.0 (1023B) * - Initial code. * 1.5 (1017B) * - Moved to Closure compiler to reduce size * - Some slight tweaks to particle movement * - Canvas now positioned to get rid of annoying border (try fullscreen!) * 1.7 (1011B) * - The swarm centre now moves around (simple bouncing). This allows the * particles to pick up a bit more speed, and move in more interesting ways; * where before they tended to just 'bubble' in the middle of the screen, * the swarm, as an entity, now appears more fluid and organic. * - Bugfix to spawning: particles now actually have to be near to each other * in BOTH axes to spawn, and both particles have to be of suitable age. This * prevents billions of particles from spawning from a single one. * - Reduced the total number of particles for aesthetic reasons * - Did some more "by-hand" optimisations after running it through the closure * compiler, like removing global variable declarations when I'm not * instantiating the variables there and then and removing the leading 0 on * decimal values to conserve precious bytes. With each revision, despite * adding functionality each time, the size is going down! :D * * Copyright 2010 Barry van Oudtshoorn. */ var z = function(a, b, c) { // Normalise a to between b and c return (a / (c - b)); }, h = function(a, b, c) { // Limx a to between b and c return (a > b ? b : (a < c ? c : a)); }, // I strip out all of the uninitialised declarations after compilation; // they're globals anyway. I know it's not stylish, but it saves bytes! r,s, // Particles that we're currently interested in i,j, // Iterators u,d, // Working variables m = Math, // Alias v = m.random, // Alias p = [], // The particles e = document.getElementById('c'), // The canvas element t = e.getContext('2d'), // The context n, // The normalised age of a particle Q = window, // Alias X = (W = Q.innerWidth)/2, // The swarm's COG Y = (H = Q.innerHeight)/2, N = M = 1, // The components of the velocity of the swarm's COG q = function(a, b, c, f) { // Draw a blob t.fillStyle = a; t.beginPath(); t.arc(b, c, f, 0, m.PI*2, 0); t.closePath(); t.fill(); }, x = function() { // Global iterator //t.globalCompositeOperation = 'source-over'; // This is prettier, but "globalCompositeOperation" takes too many bytes! :( t.fillStyle = 'rgba(9,9,9,.2)'; t.fillRect(0,0,W,H); //t.globalCompositeOperation = 'lighter'; for (i = 40; i--;) { // Loop over all of the particles if (r = p[i]) { if (r.a > 1E3) { p[i] = 0; // Kill off old particles; use 0 because it's shorter than false, null, or using delete. } else { // Move the particle n = z(r.a, 0, 1E3); u = n * 7; r.X += v() * .1 * h(X - r.x, 1, -1); // Update the particle's velocity. r.Y += v() * .1 * h(Y - r.y, 1, -1); r.x += r.X; // Update the particle's coordinates based on its velocity. r.y += r.Y; r.a++; // Increment the particle's age // Draw the particle d = r.s ? 'rgba(49,153,255,' : 'rgba(255,49,166,'; // The colour of the particle u = 1 - n; // The opacity of the particle; n is the normalised age q(d + u + ')', r.x, r.y, 1 + n * 12); // Draw the primary particle blob q(d + (u/2) + ')', r.x, r.y, 1 + n * 16); // We draw a lighter, larger blob for a 'glow' effect r.f && r.f--; // If we've spawned, decrement the cooldown. if (r.f == 0 && r.a > 250 && r.a < 750 && v() > 0.9) { // If we're the right age, check if we can spawn! for (j = 40; j-- && !r.f;) { // We have to loop over the particles again, and see if we're close enough to spawn. if ((s = p[j]) && s != r && s.a >= r.a) { d = m.sqrt(r.x - s.x, r.y - s.y); // Two if statements to avoid this calculation if possible if (d > -5 && d < 5 && r.s != s.s) { // Collision detection? What's that? :P y(r.x, r.y); // Spawn a new particle! r.f = s.f = 10; // Enforce the spawning cooldown } } } } } } } // Bounce the swarm's COG around the canvas N = (X > W || X < 0 ? -N : N); M = (Y > H || Y < 0 ? -M : M); X += N; Y += M; }, // Spawn a new particle. If sx and sy are defined, they will be its initial // coordinates; otherwise, we just the COG. If sx and sy are defined, we also // give the particle a random velocity; this means that they're 'flung out' // from their parents; at startup, though, we want them all nicely bunched. y = function(sx,sy) { for (j = 40; j--;) { if (!p[j]) { p[j] = { x: sx || X, // The particle's coords y: sy || Y, X: sx ? v() * 4 - 2: 0, // The particles velocity Y: sy ? v() * 4 - 2 : 0, f: 1, // Cooldown after spawning a: 0, // The age of the particle s: v() > .5 // The sex of the particle } return; } } }; // Make the canvas fill the entire screen with (e.style) { position = 'fixed'; top = left = 0; } e.width = W; // Set the canvas width and height e.height = H; for (i = 40; i--;) p[i] = 0; // Populate initial particles for (i = 9; i--;) y(); // Spawn eight particles. A number < 10 saves a byte! :P Q.setInterval(x, 40); // Make it go.
JS1k, 1k demo submission
Pop out
Help
About
×
×