xxxxxxxxxx
122
/*
This script can render avi's and gifs. Setup recording in setup().
Press enter to start and stop recording.
Will also stop recording after full cycle. Use this for perfect loops :)
Feel free to share your gifs on my discord server!
https://discord.com/invite/DxbUee2y6D
(max upload 8mb)
recommended gif settings:
canvas size: 500x500
quality: 1 (highest for 'accurate' colours)
fps: 30 (or lower to save space)
frames: 500
libraries used:
Video Builder: https://github.com/theshock/VideoBuilder
gif.js: https://github.com/jnordberg/gif.js
download: https://github.com/rndme/download
*/
const fps = 60;
let rec;
function setup() {
// const size = min(windowWidth, windowHeight); // fill window
const size = 1080; // avi recording
// const size = 500; // gif recording
const canvas = createCanvas(size, size);
colorMode(HSL, 1);
frameRate(fps);
rec = setupRecording("fractal n-gon", canvas, {
avi: {
fps: 60,
quality: 0.92 // full quality (1) does not encode properly (sad face)
},
// gif: {
// fps: 30,
// quality: 1, // lower is better (1 = best). Works to reduce filesize but fucks with colour, would recommend lowering fps instead ✌️
// workers: 10 // multithreaded workers
// }
});
noStroke();
}
const eps = 0.00000000001;
const ssin = (v) => sin(v * TWO_PI);
const scos = (v) => cos(v * TWO_PI);
const sinn = (v) => ssin(v) * 0.5 + 0.5;
const cosn = (v) => scos(v) * 0.5 + 0.5;
const invCosn = (v) => 1 - cosn(v);
const tri = (v) => 1 - abs(1 - fract(v) * 2);
const bias = (x, s, t) => x < t ?
t * x / (x + s * (t - x) + eps) :
(1 - t) * (x - 1) / (1 - x - s * (t - x) + eps) + 1
const frames = 1000;
let frame = 0;
let t;
function draw() {
let inc = 1;
if (!rec.recording)
inc = deltaTime / (1000 / fps);
frame += inc;
t = fract(frame / frames);
// t = fract(mouseX / width);
scale(width, height);
drawScene();
if (rec.recording)
rec.recordFrame();
}
const maxDepth = 5;
function drawScene() {
background(0);
stroke(1);
strokeWeight(0.002);
const count = 3 + floor(4 * t);
const depth = floor(maxDepth * fract(t * 4)) //invCosn(t * 4);
drawFractal(0.5, 0.5, 1 / 2.5, count, depth);
}
function polar(angle, radius) {
return {
x: cos(angle * TWO_PI) * radius,
y: sin(angle * TWO_PI) * radius,
}
}
function drawFractal(x, y, size, count, depth) {
const df = 1; //constrain(depth, 0, 1)
for (let i = 0; i < count; i++) {
const f = i / count;
const angle = f + 0.25;
if (depth > 0) {
const p = polar(angle, size * (df * 0.5));
const s = size * (1 - df * 0.5);
drawFractal(x + p.x, y + p.y, s, count, depth - 1);
} else {
const p1 = polar(angle, size);
const p2 = polar(angle + 1 / count, size);
// const c = color(fract(t + y * 0.25), 0.75, x * 0.2 + 0.5);
// stroke(c);
line(x + p1.x, y + p1.y, x + p2.x, y + p2.y);
}
}
}