Click to re-roll random variables; s to save a frame; r to save a gif
xxxxxxxxxx
// Genuary15 - Sine Waves
var gB, fullRes, windowRes;
var requiredFrames = 360;
var frameTarget, streamCount, f0, f1, p0, p1;
function setup() {
windowRes = min(windowWidth, windowHeight);
createCanvas(windowRes, windowRes);
imageMode(CENTER);
gB = createGraphics(fullRes = 1024, fullRes);
gB.colorMode(HSB, 360);
initiate();
}
function initiate() {
streamCount = 128;
f0 = random(-1, 1);
f1 = random(-2, 2);
p0 = random(-4, 4);
p1 = random(-8, 8);
frameTarget = frameCount + requiredFrames;
gB.clear();
gB.background(360);
}
function draw() {
gB.resetMatrix();
gB.translate(fullRes * 0.5, fullRes * 0.5);
var animationProgress = ((frameTarget - frameCount) / requiredFrames) % 1;
var xPos = map(animationProgress, 0, 1, fullRes * 0.525, -fullRes * 0.525);
for (var i = 0; i < streamCount; i += 1) {
gB.push();
gB.translate(0, map(i, 0, streamCount, fullRes * -0.6, fullRes * 0.6));
var mappedxPos = i * 0.1 + map(animationProgress, 0, 1, -1, 1);
var yPos = sin(f0 * mappedxPos + 2 * sin(f1 * mappedxPos * p1) + p0);
for (var j = 0; j < 8; j += 1) {
gB.push();
gB.stroke(random(180), 180);
gB.strokeWeight(random(0.5, 1.5));
gB.translate(random(-4, 4) * random() * random(), random(-4, 4) * random() * random());
gB.point(xPos, yPos * 128);
gB.pop();
}
gB.pop();
}
f0 += random(-1, 1) * 0.01;
f1 += random(-1, 1) * 0.01;
p0 += random(-1, 1) * 0.01;
p1 += random(-1, 1) * 0.01;
background(360);
displayBuffer(gB);
if (frameCount === frameTarget) {
noLoop();
}
}
function mouseClicked() {
initiate();
loop();
}
function keyPressed() {
if (key === "r") {
windowRes = 512;
resizeCanvas(512, 512);
saveGif('export', requiredFrames, {
delay: 0,
units: "frames"
});
}
if (key === "s") {
gB.save('output.png');
}
}
function displayBuffer(buffer) {
image(buffer, windowRes / 2, windowRes / 2, windowRes, windowRes);
}
function windowResized() {
windowRes = min(windowWidth, windowHeight);
resizeCanvas(windowRes, windowRes);
}