Skip to content

Instantly share code, notes, and snippets.

@not-scripter
Created July 22, 2023 14:45
Show Gist options
  • Save not-scripter/7fba8bab39829adb3173c991377f67d1 to your computer and use it in GitHub Desktop.
Save not-scripter/7fba8bab39829adb3173c991377f67d1 to your computer and use it in GitHub Desktop.
Particle Swarm [Magnetic Field recreation]
<!--
Click and drag to attract
Right click to repulse
Mouse-wheel click to create a time dilation field
Use the Controls to decrease or increase
the particle count to tweak performance.
-->
<canvas id="swarm"></canvas>

Particle Swarm [Magnetic Field recreation]

Click and drag to attract, right-click to repulse, middle-mouse-click to create a time-dilation field. Check out the options for colors and particle count (lower count might improve performance on FF).

Inspired by thepheer's magnetic field pen. I wanted to recreate this pen completely by programming a perlin simplex noise generator, using my own implementation of SmallPRNG for seeding the noise generator, a simple Vector3D class and main code.

All the functional code (but the SmallPRNG code) is in this pen, including vector and perlin noise stuff. In this implementation, the particles wrap the screen. This means that when they fall off one side, they re-appear on the other side.

Once again, thanks to @tmrDevelops for taking the time to help me with some issues.

A Pen by Bas Groothedde on CodePen.

License.

/*
Click and drag to attract
Right click to repulse
Mouse-wheel click to create a time dilation field
Use the Controls to decrease or increase
the particle count to tweak performance.
*/
+(function(root) {
'use strict';
var Vector3D = function Vector3D(x, y, z) {
this.set(x, y, z);
}, v3dp = Vector3D.prototype;
v3dp.dot2d = function(x, y) {
return ((this.x * x) + (this.y * y));
};
v3dp.dot3d = function(x, y, z) {
return ((this.x * x) + (this.y * y) + (this.z * z));
};
v3dp.set = function(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
return this;
};
v3dp.add = function(other) {
if(typeof other === "number") {
this.x += other, this.y += other, this.z += other;
return this;
}
this.x += other.x, this.y += other.y, this.z += other.z;
return this;
};
v3dp.sub = function(other) {
if(typeof other === "number") {
this.x -= other, this.y -= other, this.z -= other;
return this;
}
this.x -= other.x, this.y -= other.y, this.z -= other.z;
return this;
};
v3dp.mul = function(other) {
if(typeof other === "number") {
this.x *= other, this.y *= other, this.z *= other;
return this;
}
this.x *= other.x, this.y *= other.y, this.z *= other.z;
return this;
};
v3dp.div = function(other) {
if(typeof other === "number") {
this.x /= other, this.y /= other, this.z /= other;
return this;
}
this.x /= other.x, this.y /= other.y, this.z /= other.z;
return this;
};
v3dp.move = function(dest) {
if(dest instanceof Vector3D) {
dest.x = this.x, dest.y = this.y, dest.z = this.z;
}
return this;
};
v3dp.within2d = function(bounds) {
return (this.x >= 0 && this.x < bounds.x && this.y >= 0 && this.y < bounds.y);
};
v3dp.wrap2d = function(bounds) {
if(this.x > bounds.x) {
this.x = 0;
return true;
}
if(this.x < 0) {
this.x = bounds.x;
return true;
}
if(this.y > bounds.y) {
this.y = 0;
return true;
}
if(this.y < 0) {
this.y = bounds.y;
return true;
}
};
v3dp.eq = function(other) {
return (other instanceof Vector3D) && this.x === other.x && this.y === other.y && this.z === other.z;
};
v3dp.distance = function(other) {
var dx = (this.x - other.x),
dy = (this.y - other.y);
return Math.sqrt(dx * dx + dy * dy);
};
v3dp.clone = function() {
return new Vector3D(this.x, this.y, this.z);
};
root.Vector3D = Vector3D;
}(window));
+(function(root) {
'use strict';
// a simple non-optimized Perlin Simplex Noise. I wrote this
// to understand Simplex Noise a bit more.
// fully self-contained state, so you can influence the outcome
// of each simplex noise state
var Perlin = function Perlin() {
this.grad3 = [
new Vector3D(1,1,0), new Vector3D(-1,1,0), new Vector3D(1,-1,0), new Vector3D(-1,-1,0),
new Vector3D(1,0,1), new Vector3D(-1,0,1), new Vector3D(1,0,-1), new Vector3D(-1,0,-1),
new Vector3D(0,1,1), new Vector3D(0,-1,1), new Vector3D(0,1,-1), new Vector3D(0,-1,-1)
];
this.p = [
0x97, 0xa0, 0x89, 0x5b, 0x5a, 0x0f, 0x83, 0x0d, 0xc9, 0x5f, 0x60, 0x35, 0xc2, 0xe9, 0x07, 0xe1,
0x8c, 0x24, 0x67, 0x1e, 0x45, 0x8e, 0x08, 0x63, 0x25, 0xf0, 0x15, 0x0a, 0x17, 0xbe, 0x06, 0x94,
0xf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c0, 0xd6, 0x1f, 0xb5, 0xc7, 0x6a, 0x9d,
0xb8, 0x54, 0xcc, 0xb0, 0x73, 0x79, 0x32, 0x2d, 0x7f, 0x04, 0x96, 0xfe, 0x8a, 0xec, 0xcd, 0x5d,
0xde, 0x72, 0x43, 0x1d, 0x18, 0x48, 0xf3, 0x8d, 0x80, 0xc3, 0x4e, 0x42, 0xd7, 0x3d, 0x9c, 0xb4
];
this.permutation = new Array(512);
this.gradP = new Array(512);
// skew and unskew factors for 2D or 3D, can be modified per state!
this.F2 = (0.5 * (Math.sqrt(3) - 1));
this.G2 = ((3 - Math.sqrt(3)) / 6);
this.F3 = (1 / 3);
this.G3 = (1 / 6);
}, pp = Perlin.prototype;
pp.init = function(prng) {
if(typeof prng !== "function") {
throw new TypeError("prng needs to be a function returning an int between 0 and 255");
}
for(var i = 0; i < 256; i += 1) {
var randval = (this.p[i] ^ prng());
this.permutation[i] = this.permutation[i + 256] = randval;
this.gradP[i] = this.gradP[i + 256] = this.grad3[randval % this.grad3.length];
}
};
// I removed the pp.simplex2d function, because I don't need it in this project
// pp.simplex2d = function(x, y) {};
pp.simplex3d = function(x, y, z) {
var n0, n1, n2, n3, i1, j1, k1, i2, j2, k2,
x1, y1, z1, x2, y2, z2, x3, y3, z3,
gi0, gi1, gi2, gi3, t0, t1, t2, t3,
s = ((x + y + z) * this.F3),
i = Math.floor(x + s), j = Math.floor(y + s), k = Math.floor(z + s),
t = ((i + j + k) * this.G3),
x0 = (x - i + t), y0 = (y - j + t), z0 = (z - k + t);
if(x0 >= y0) {
if(y0 >= z0) {
i1=1; j1=0; k1=0; i2=1; j2=1; k2=0;
} else if(x0 >= z0) {
i1=1; j1=0; k1=0; i2=1; j2=0; k2=1;
} else {
i1=0; j1=0; k1=1; i2=1; j2=0; k2=1;
}
} else {
if(y0 < z0) {
i1=0; j1=0; k1=1; i2=0; j2=1; k2=1;
} else if(x0 < z0) {
i1=0; j1=1; k1=0; i2=0; j2=1; k2=1;
} else {
i1=0; j1=1; k1=0; i2=1; j2=1; k2=0;
}
}
x1 = (x0 - i1 + this.G3), y1 = (y0 - j1 + this.G3), z1 = (z0 - k1 + this.G3);
x2 = (x0 - i2 + 2 * this.G3), y2 = (y0 - j2 + 2 * this.G3), z2 = (z0 - k2 + 2 * this.G3);
x3 = (x0 - 1 + 3 * this.G3), y3 = (y0 - 1 + 3 * this.G3), z3 = (z0 - 1 + 3 * this.G3);
i &= 255, j &= 255, k &= 255;
gi0 = this.gradP[i + this.permutation[j + this.permutation[k]]];
gi1 = this.gradP[i + i1 + this.permutation[j + j1 + this.permutation[k + k1]]];
gi2 = this.gradP[i + i2 + this.permutation[j + j2 + this.permutation[k + k2]]];
gi3 = this.gradP[i + 1 + this.permutation[j + 1 + this.permutation[k + 1]]];
t0 = (0.6 - x0 * x0 - y0 * y0 - z0 * z0);
t1 = (0.6 - x1 * x1 - y1 * y1 - z1 * z1);
t2 = (0.6 - x2 * x2 - y2 * y2 - z2 * z2);
t3 = (0.6 - x3 * x3 - y3 * y3 - z3 * z3);
n0 = (t0 < 0 ? 0 : (t0 *= t0, t0 * t0 * gi0.dot3d(x0, y0, z0)));
n1 = (t1 < 0 ? 0 : (t1 *= t1, t1 * t1 * gi1.dot3d(x1, y1, z1)));
n2 = (t2 < 0 ? 0 : (t2 *= t2, t2 * t2 * gi2.dot3d(x2, y2, z2)));
n3 = (t3 < 0 ? 0 : (t3 *= t3, t3 * t3 * gi3.dot3d(x3, y3, z3)));
return (32 * (n0 + n1 + n2 + n3));
};
root.Perlin = Perlin;
}(window));
;(function(root) {
'use strict';
var MouseMonitor = function(element) {
this.position = new Vector3D(0, 0, 0);
this.state = {left: false, middle: false, right: false};
this.element = element;
var that = this;
element.addEventListener('mousemove', function(event) {
var dot, eventDoc, doc, body, pageX, pageY;
event = event || window.event;
if (event.pageX == null && event.clientX != null) {
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0 );
}
that.position.x = event.pageX;
that.position.y = event.pageY;
});
element.addEventListener('contextmenu', function(event) {
return event.preventDefault();
});
element.addEventListener('mousedown', function(event) {
if(event.which === 1) that.state.left = true;
if(event.which === 2) that.state.middle = true;
if(event.which === 3) that.state.right = true;
return event.preventDefault();
});
element.addEventListener('mouseup', function(event) {
that.state.left = that.state.middle = that.state.right = false;
return event.preventDefault();
});
};
root.MouseMonitor = MouseMonitor;
}(window));
+(function(root) {
'use strict';
var Particle = function Particle(generator, bounds, rctx, mon) {
this.p = new Vector3D(); // position
this.t = new Vector3D(); // trail to
this.v = new Vector3D(); // velocity
this.g = generator; // simplex noise generator
this.b = bounds; // window bounds for wrapping
this.r = rctx; // random context
this.m = mon; // mouse position monitor
this.reset();
}, pp = Particle.prototype;
pp.reset = function() {
// new random position
this.p.x = this.t.x = Math.floor(this.r.random() * this.b.x);
this.p.y = this.t.y = Math.floor(this.r.random() * this.b.y);
// reset velocity
this.v.set(1, 1, 0);
// iteration and life
this.i = 0;
this.l = this.r.random(1000, 10000); // life time before particle respawns
};
pp.step = function() {
if(this.i++ > this.l) {
this.reset();
}
var xx = (this.p.x / 200),
yy = (this.p.y / 200),
zz = (Date.now() / 5000),
a = (this.r.random() * Math.Tau),
rnd= (this.r.random() / 4);
// calculate the new velocity based on the noise
// random velocity in a random direction
this.v.x += (rnd * Math.sin(a) + this.g.simplex3d(xx, yy, -zz)); // sin or cos, no matter
this.v.y += (rnd * Math.cos(a) + this.g.simplex3d(xx, yy, zz)); // opposite zz's matters
if(this.m.state.left) {
// add a difference between mouse pos and particle pos (a fraction of it) to the velocity.
this.v.add(this.m.position.clone().sub(this.p).mul(.00085));
}
// repulse the particles if the right mouse button is down and the distance between
// the mouse and particle is below an arbitrary value between 200 and 250.
if(this.m.state.right && this.p.distance(this.m.position) < this.r.random(200, 250)) {
this.v.add(this.p.clone().sub(this.m.position).mul(.02));
}
// time dilation field, stuff moves at 10% here, depending on distance
if(this.m.state.middle) {
var d = this.p.distance(this.m.position),
l = this.r.random(200, 250);
if(d < l) {
this.v.mul(d / l);
}
}
// keep a copy of the current position, for a nice line between then and now and add velocity
this.p.move(this.t).add(this.v.mul(.94)); // slow down the velocity slightly
// wrap around the edges
if(this.p.wrap2d(this.b)) {
this.p.move(this.t);
}
};
// plot the line, but do not stroke yet.
pp.render = function(context) {
context.moveTo(this.t.x, this.t.y);
context.lineTo(this.p.x, this.p.y);
};
root.Particle = Particle;
}(window));
window.addEventListener('load', function() {
var rctx = new SmallPRNG(+new Date()), // random generator, see ref
p = new Perlin(), // simplex noise generator
canvas = document.getElementById("swarm"),
context = canvas.getContext("2d"),
stats = new Stats(),
monitor = new MouseMonitor(canvas),
gui = new dat.GUI(),
hue = 0, particles = [], resize,
width, height, bounds = new Vector3D(0, 0, 0),
settings = {
particleNum: 5000,
fadeOverlay: true,
rotateColor: true,
staticColor: {r: 0, g: 75, b: 50},
staticColorString: 'rgba(0, 75, 50, 0.55)'
};
stats.setMode(0); // Start off with FPS mode
// Place the statistics at the bottom right.
stats.domElement.style.position = 'absolute';
stats.domElement.style.right = '5px';
stats.domElement.style.bottom = '5px';
document.body.appendChild(stats.domElement);
// dat.gui stuff, 2 folders with a few properties
var f1 = gui.addFolder('Particles'),
f2 = gui.addFolder('Colors');
f1.add(settings, 'particleNum', 1000, 15000).step(10).name("Particles").onChange(function() {
if(settings.particleNum < particles.length) {
var toDelete = (particles.length - settings.particleNum);
particles.splice(particles.length - toDelete, toDelete);
} else {
for(var i = particles.length; i < settings.particleNum; i += 1) {
particles.push(new Particle(p, bounds, rctx, monitor));
}
}
});
f2.add(settings, 'fadeOverlay').name("Fade Clear").onChange(function() {
if(settings.fadeOverlay) {
resize();
}
});
f2.add(settings, 'rotateColor').name("Rotate Color");
f2.addColor(settings, 'staticColor').name("Static Color").onChange(function() {
settings.staticColorString = 'rgba(' +
Math.floor(settings.staticColor.r) + ', ' +
Math.floor(settings.staticColor.g) + ', ' +
Math.floor(settings.staticColor.b) + ', ' + .55 + ')';
});
f1.open();
f2.open();
gui.close();
// seed perlin with random bytes from SmallPRNG
p.init(function() {
// called for each permutation (256 times)
return rctx.random(0, 255);
});
resize = function() {
// resize the canvas
canvas.width = width = bounds.x = window.innerWidth;
canvas.height = height = bounds.y = window.innerHeight;
// remove this and see weird gorgeous stuffs, the history of particles.
context.fillStyle = '#ffffff';
context.fillRect(0, 0, width, height);
}; resize();
window.addEventListener('resize', resize);
// generate a few particles
for(var i = 0; i < settings.particleNum; i += 1) {
particles.push(new Particle(p, bounds, rctx, monitor));
}
+(function render() {
requestAnimFrame(render);
stats.begin();
context.beginPath();
// render each particle and trail
for(var i = 0; i < particles.length; i += 1) {
particles[i].step(), particles[i].render(context);
}
context.globalCompositeOperation = 'source-over';
if(settings.fadeOverlay) {
context.fillStyle = 'rgba(0, 0, 0, .085)';
} else {
context.fillStyle = 'rgba(0, 0, 0, 1)';
}
context.fillRect(0, 0, width, height);
context.globalCompositeOperation = 'lighter';
if(settings.rotateColor) {
context.strokeStyle = 'hsla(' + hue + ', 75%, 50%, .55)';
} else {
context.strokeStyle = settings.staticColorString;
}
context.stroke();
context.closePath();
stats.end();
hue = ((hue + .5) % 360);
}());
});
<script src="//codepen.io/ImagineProgramming/pen/bcmyD.js"></script>
<script src="//s3-us-west-2.amazonaws.com/s.cdpn.io/188512/codepen-utilities.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5.1/dat.gui.min.js"></script>
/*
Click and drag to attract
Right click to repulse
Mouse-wheel click to create a time dilation field
Use the Controls to decrease or increase
the particle count to tweak performance.
*/
body, html {
margin: 0;
padding: 0;
}
body {
canvas {
display: block;
cursor: crosshair;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment