xxxxxxxxxx
123
/*
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;
let capture;
function setup() {
// const size = min(windowWidth, windowHeight); // fill window
const size = 1080; // avi recording
// const size = 500; // gif recording
const canvas = createCanvas(size, size);
capture = createCapture(VIDEO);
capture.size(320, 240);
colorMode(RGB, 1);
frameRate(fps);
rec = setupRecording("you should name me", 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 = 500;
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 * 0.9, height * 0.9);
strokeWeight(0.005);
drawScene();
if (rec.recording)
rec.recordFrame();
}
function drawScene() {
background(0);
stroke(1);
strokeWeight(0.004);
translate(0.05, 0.05);
// strokeCap(SQUARE);
capture.loadPixels();
const row = 99 * invCosn(t) + 1;
const col = 100;
const amp = 0.05;
for (let y = 0; y < row; y++)
for (let x = 0; x < col-1; x++) {
let xf = x / col;
let xf2 = (x + 1) / col;
let yf = y / row;
let v = sig(xf, yf) * amp;
let v2 = sig(xf2, yf) * amp;
line(xf, yf + v, xf2, yf + v2);
}
}
function sig(x, y) {
// return sin(TWO_PI * ((tri(x) + tri(y)) * 2 + t * 5)) * invCosn(x) * invCosn(y) * 0.5;
// return sin(TWO_PI * ((cosn(x + t) + sinn(y + t)) + t));
const px = floor(capture.width * x);
const py = floor(capture.height * y);
const i = floor(px + py * capture.width) * 4;
const cp = capture.pixels;
const lum = (cp[i] + cp[i + 1] + cp[i + 2]) / (255 * 3);
stroke(color(cp[i +0]/ 255, cp[i +1]/ 255, cp[i +2] / 255));
return sin(TWO_PI * (lum * x + t * 5)) * lum;
}