Skip to content

Instantly share code, notes, and snippets.

@yohannawaliya
Created February 4, 2019 21:31
Show Gist options
  • Save yohannawaliya/c5f2adaf20f345f297189cc7f602e8fa to your computer and use it in GitHub Desktop.
Save yohannawaliya/c5f2adaf20f345f297189cc7f602e8fa to your computer and use it in GitHub Desktop.
Galactic Orbitals

Galactic Orbitals

Galaxy clusters orbiting galaxy clusters, orbiting galaxy clusters...After a short while of orbiting, smaller clusters travel to the centers of larger galaxies && connections are formed
Extension of Foo Particles to Foo Bar Particles. Foo holds our Math equations for motion while Bar specifies x,y positions && velocity. Fairly flexible, very short. Will likely do a variation or two..

A Pen by Tiffany Rayside on CodePen.

License.

<canvas id='canv'></canvas>
var c = document.getElementById('canv');
var $ = c.getContext('2d');
var w = c.width = window.innerWidth;
var h = c.height = window.innerHeight;
window.addEventListener('resize', function(){
c.width = w = window.innerWidth;
c.height = h = window.innerHeight;
}, false);
var num = w * h / 810;
var arr = [];
var i = 0;
while (arr.length < num) {
arr.push({
x: (Math.random() * w) | 0,
y: (Math.random() * h) | 0,
vx: 0,
vy: 0
});
}
function _X(foo) {
return Math.sin(foo.y / 45 ) / 0.3;
}
function _Y(foo) {
return Math.sin(foo.x / 45 ) / 0.3;
}
function upd(bar) {
var n = arr[i];
n.x += n.vx;
n.y += n.vy;
if (n.x < 0) {
n.x = w + n.x;
} else if (n.x >= w) {
n.x -= w;
}
if (n.y < 0) {
n.y = h + n.y;
} else if (n.y >= h) {
n.y -= h;
}
n.vy = _Y(n);
n.vx = _X(n);
}
function draw(bar) {
var n = arr[i];
var col = 'hsla(' + i + ',90%, 60%, 1)';
//outer rings
$.beginPath();
$.fillStyle = col;
$.globalAlpha = .1;
$.arc(n.x, n.y, 15 / Math.max((n.vx * n.vx + n.vy * n.vy), 0.5), 0, 2 * Math.PI, 0);
$.closePath();
$.fill();
//inner rings
$.beginPath();
$.globalAlpha = 1;
$.fillStyle = col;
$.arc(n.x, n.y, 8 / Math.max((n.vx * n.vx + n.vy * n.vy), 0.8), 0, 2 * Math.PI, 0);
$.closePath();
$.fill();
}
function go() {
$.fillStyle = 'hsla(0,0%,0%,1)';
$.fillRect(0, 0, w, h);
for (i = 0; i < num; i++) {
upd(i);
draw(i);
}
};
run();
function run() {
window.requestAnimationFrame(run);
go();
}
body{
width:100%;
margin:0;
overflow:hidden;
background:hsla(0,0%,0%,1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment