xxxxxxxxxx
79
const maxStars = 120;
const maxTrails = 5;
const starSizeCaps = [3, 10];
const starAlphaCaps = [100, 255];
const trailSizeCaps = [20, 60];
const angleCaps = [100, 130];
const trailWidthCaps = [1, 2];
const offset = 25;
const segs = 50;
const cnv = {w: 800, h: 400};
var colourSet;
function setup() {
createCanvas(cnv.w, cnv.h);
background(50, 30, 90);
colourSet = [color(255, 255, 255), color(150, 150, 255), color(255, 255, 120)];
}
function draw() {
if(frameCount <= maxStars) renderStars();
else if(frameCount <= maxStars + maxTrails) renderTrails();
else if(frameCount == maxStars + maxTrails + 1) noLoop();
}
function renderStars() {
let randSize = random(starSizeCaps[0], starSizeCaps[1]);
let randColour = random(colourSet);
let randPos = [random(0 + offset, cnv.w - offset), random(0 + offset, cnv.h - offset)];
let randAlpha = random(starAlphaCaps[0], starAlphaCaps[1]);
randColour.setAlpha(randAlpha);
if(random() > 0.3 && randSize >= 8) {
noFill();
stroke(randColour);
strokeWeight(2);
}
else {
noStroke();
fill(randColour);
}
ellipse(randPos[0], randPos[1], randSize);
}
function renderTrails() {
let randLen = random(trailSizeCaps[0], trailSizeCaps[1]);
let randAngle = random(angleCaps[0], angleCaps[1]);
let randWid = random(trailWidthCaps[0], trailWidthCaps[1]);
let randColour = random(colourSet);
stroke(randColour);
angleMode(DEGREES);
let start = createVector(random(-cnv.w / 3, cnv.w / 3) + cnv.w / 2, random(-cnv.h / 4, cnv.h / 4) + 5 * cnv.h / 12);
translate(start);
let end = createVector(cos(randAngle), sin(randAngle)).mult(randLen);
gradientLine2(createVector(0, 0), end, randWid, randWid + 2, segs);
}
function gradientLine2(
start_v,
end_v,
start_weight,
end_weight,
segments
) {
let prev_loc_v = start_v.copy();
for (let i = 1; i <= segments; i++) {
let cur_loc_v = p5.Vector.lerp(start_v, end_v, i / segments);
push();
strokeCap(SQUARE);
strokeWeight(lerp(start_weight, end_weight, i / segments));
line(prev_loc_v.x, prev_loc_v.y, cur_loc_v.x, cur_loc_v.y);
pop();
prev_loc_v = cur_loc_v.copy();
}
}