let sketch = function(p) { let bgCol = hexToRgb('#222233'); let starCol = [hexToRgb('#ffcdff'), hexToRgb('#804442'), hexToRgb('#ffff7d')] let starDensity = 0.0005; //0.0005 is safe. Above 0.01 is absurd let currentWidth; let currentHeight; let nbStars; let stars; let bg; let time = 0; p.setup = function(){ p.frameRate(24); currentWidth = p.windowWidth; currentHeight = p.windowHeight; p.createCanvas(currentWidth, currentHeight); bgInit(); // p.noLoop(); } p.draw = function(){ p.background(bgCol); p.image(bg, 0, 0); // p.circle(30, time*1.5, 10); // for (let i=0; i currentWidth || newHeight > currentHeight) { currentWidth = newWidth; currentHeight = newHeight; p.resizeCanvas(currentWidth, currentHeight); bgInit(); } } function bgInit() { nbStars = calcNumberOfStars(); bg = p.createGraphics(p.width, p.height); bg.background(bgCol); setGradient(0, 0, p.width, p.height, bgCol, starCol[1]); stars = new Stars(nbStars); stars.make(); stars.draw(); } function calcNumberOfStars() { //Helps to get a consistent number of starts regardless the window size if (starDensity > 0.01) { console.log('starDensity is too high! going back to the maximum (0.01) in order to prevent too much CPU load when starting the patch'); starDensity = 0.01; } let nb = p.round(starDensity * currentWidth * currentHeight) console.log('nbStars: ' + nb); return nb; } function hexToRgb(hex) { hex = hex.replace('#', ''); var bigint = parseInt(hex, 16); var r = (bigint >> 16) & 255; var g = (bigint >> 8) & 255; var b = bigint & 255; return p.color(r, g, b); } function setGradient(x, y, w, h, c1, c2) { bg.noFill(); // Top to bottom gradient for (let i = y; i <= y + h; i++) { let inter = p.pow(p.map(i, y, y + h, 0, 1),6); let c = bg.lerpColor(c1, c2, inter); bg.stroke(c); bg.line(x, i, x + w, i); } } }; new p5(sketch, 'bg');