Constellations
How do I make an constellations?
A port to p5.js of a small project I wrote in Java over the summer. Particles with random velocities draw lines between themselves if they are if their euclidean distance is small enough. These lines get colored based on the distance between the points.. What is a constellations? How do you make a constellations? This script and codes were developed by Ben Matthews on 10 September 2022, Saturday.
Constellations - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Constellations</title> <link rel='stylesheet prefetch' href='https://www.unm.edu/~bmatthews1/hosted/fullSizeCanvas.css'>
</head>
<body> <script src='https://github.com/processing/p5.js/releases/download/0.5.4/p5.min.js'></script>
<script src='https://www.unm.edu/~bmatthews1/hosted/dat.gui.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Constellations - Script Codes JS Codes
window.addEventListener("resize", resize);
var gui;
function Params(){ this.maxSpeed = 1.5; this.maxAttaches = 5; this.attachRadius = 74; this.numParticles = 300; this.pointRadius = 0; this.hueBase = 200; this.hueVariance = 60; this.lineFade = .5; this.backgroundFade = .7; this.lineWidth = 1; this.useMouse = false; this.mouseRadius = 200; this.mouseSpeedModifier = 4;
}
var attachParticleRatio = (50)/500;
var lastParticleAmount = 500;
var paramSet = new Params();
var particles = [];
var maxParticles = 1000;
var width = 0;
var height = 0;
function Particle(x, y, xSpeed, ySpeed){ this.x = x; this.y = y; this.xSpeed = xSpeed; this.ySpeed = ySpeed; this.mouseModX = 0; this.mouseModY = 0; this.step = function(){ this.mouseModX = 0; this.mouseModY = 0; if (mouseIsPressed && paramSet.useMouse){ var xDiff = this.x - mouseX; var yDiff = this.y - mouseY; var distance = Math.sqrt(xDiff*xDiff + yDiff*yDiff); if (distance < paramSet.mouseRadius){ this.mouseModX = (xDiff/distance)*paramSet.mouseSpeedModifier; this.mouseModY = (yDiff/distance)*paramSet.mouseSpeedModifier; } } this.x += this.xSpeed*paramSet.maxSpeed + this.mouseModX; this.y += this.ySpeed*paramSet.maxSpeed + this.mouseModY; if (this.x < 0) this.x = 0; if (this.x > width) this.x = width; if (this.y < 0) this.y = 0; if (this.y > height) this.y = height; if (this.x <= 0 && this.xSpeed < 0) this.xSpeed *= -1; if (this.x >= width && this.xSpeed > 0) this.xSpeed *= -1; if (this.y <= 0 && this.ySpeed < 0) this.ySpeed *= -1; if (this.y >= height && this.ySpeed > 0) this.ySpeed *= -1; }
}
function createParticle(){ var x = Math.random()*window.innerWidth; var y = Math.random()*window.innerHeight; var xSpeed = Math.random()*2 - 1; var ySpeed = Math.random()*2 - 1; // console.log(x + ", " + y + ", " + xSpeed + ", " + ySpeed); return (new Particle(x, y, xSpeed, ySpeed));
}
for (var i = 0; i < maxParticles; i++){ particles.push(createParticle());
}
function setup()
{ createCanvas(window.innerWidth, window.innerHeight); resize(); background(0); colorMode(HSB, 360, 100, 100, 100); gui = new dat.GUI({ height : 5 * 32 - 1 }); gui.remember(paramSet); var particles = gui.addFolder("particles"); particles.add(paramSet, "numParticles", 0, 1000, 1); particles.add(paramSet, "maxSpeed", 0, 5); particles.add(paramSet, "pointRadius", 0, 5); particles.open(); var connections = gui.addFolder("connecitons"); connections.add(paramSet, "attachRadius", 0, 200, 1).listen(); connections.add(paramSet, "maxAttaches", 1, 10, 1); connections.open(); var visuals = gui.addFolder("visuals"); visuals.add(paramSet, "hueBase", 0, 360); visuals.add(paramSet, "hueVariance", 0, 360); visuals.add(paramSet, "lineWidth", 1, 30); visuals.add(paramSet, "lineFade", 0, 1, .01); visuals.add(paramSet, "backgroundFade", 0, 1, .01); var mouse = gui.addFolder("mouse"); mouse.add(paramSet, "useMouse"); mouse.add(paramSet, "mouseRadius", 0, 500); mouse.add(paramSet, "mouseSpeedModifier", -10, 10);
}
function draw()
{ var bgAlpha = 1 - paramSet.backgroundFade; background(1, 1, 1,bgAlpha*bgAlpha*100); noStroke(); for (var i = 0; i < paramSet.numParticles; i++){ var p = particles[i]; fill(0, 100, 100); ellipse(p.x, p.y, paramSet.pointRadius, paramSet.pointRadius); p.step(); } for (var i = 0; i < paramSet.numParticles; i++){ var p = particles[i]; drawClosest(p); }
}
function drawClosest(p){ strokeWeight(paramSet.lineWidth); var attaches = 0; var attachRadius = paramSet.attachRadius; for(var i = 0; i < paramSet.numParticles; i++){ var p2 = particles[i]; var xDiff = p2.x-p.x; var yDiff = p2.y-p.y; var distance = xDiff*xDiff+yDiff*yDiff; if (distance < attachRadius*attachRadius){ setStrokeColor(distance); line(p.x, p.y, p2.x, p2.y); attaches++; } if (attaches >= paramSet.maxAttaches) i = paramSet.numParticles; }
}
function setStrokeColor(distance){ var attachRadius = paramSet.attachRadius; var value = distance/(attachRadius*attachRadius); var hue = value*paramSet.hueVariance; hue -= paramSet.hueVariance/2; hue += paramSet.hueBase; if (hue > 360) hue -= 360; if (hue < 0) hue += 360; var alpha = 100 - value*value*(paramSet.lineFade*paramSet.lineFade)*100; stroke(hue, 80, 100, alpha);
}
function resize(){ resizeCanvas(window.innerWidth, window.innerHeight); width = window.innerWidth; height = window.innerHeight; background(1, 1, 1, 100);
}
Developer | Ben Matthews |
Username | tsuhre |
Uploaded | September 10, 2022 |
Rating | 3 |
Size | 2,961 Kb |
Views | 24,288 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Tree Generator V2 | 5,891 Kb |
Corny e-Valentine | 2,587 Kb |
Fractal Fun | 3,965 Kb |
GameBunny1 | 2,270 Kb |
Random Chess Game | 3,330 Kb |
Voronoi Pursuit Curves | 4,234 Kb |
Conways Fireworks | 2,960 Kb |
Flame Animation Test 2 | 2,937 Kb |
Diffusion Limited Aggregation | 3,361 Kb |
Snow | 2,078 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Responsive Menu I | Rodericksandoval | 3,045 Kb |
A Pen by Malith Hettiarachchi | MalZiiirA | 5,531 Kb |
Minimelephant | Smashlee | 2,507 Kb |
Css3 slide | Nakome | 3,190 Kb |
Sticky div | Kaslab | 2,225 Kb |
Polo, the flying squirrel | Agbales | 2,445 Kb |
Zeichensatz | Moklick | 2,058 Kb |
Hamburger Menu Animation | Salmanraza | 2,580 Kb |
Snow collision | Wojtek1150 | 3,542 Kb |
Nested table email layout | Massimo-cassandro | 2,355 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!