### Mouse click: add more random walk fragments ### Key 'c': restart and change background ### Key 's': save canvas
xxxxxxxxxx
/*
A random surfer browses the canvas starting from the center. Sometimes, it is teleported to a random position.
It witnesses its passage by drawing colored lines, triangles, and curves, possibly filling them with
complementary colors.
Features:
1. sparsity: the sparsity of the traces (80% of times it is sparse, 20% it is dense)
2. background: the color of background (95% of times it is black, 4.8% it is white, and 0.2% it is jade green)
3. teleport: the probability of making a random jump on the canvas (85% of times it is 15%, and 15% of times it is 5%)
4. leg: the length of the step made by the surfer (70% of times it is medium, 15% it is short, and 15% it is long)
Coded by hex6c --> linktr.ee/hex6c
*/
let weight = 0.5, opacity = 160, head = false;
let startStep, c1, c2, x, y, xn, yn, step, R, minIter, maxIter, size, teleport, leg;
function setup() {
size = min(windowWidth, windowHeight);
createCanvas(size, size);
R = new Random();
if (R.random_dec() < 0.7) {
leg = 1;
} else {
if (R.random_dec() < 0.5) {
leg = 0.75;
} else {
leg = 1.25;
}
}
startStep = leg * size / 60;
if (R.random_dec() < 0.8) {
minIter = size * 0.3;
maxIter = size * 0.6;
} else {
minIter = size * 0.6;
maxIter = size * 1.2;
}
let c = R.random_dec();
if (c < 0.94) {
background(0);
} else if (c < 0.998) {
background(255);
} else {
background(0, 66, 42);
}
if (R.random_dec() < 0.85) {
teleport = 0.15;
} else {
teleport = 0.05;
}
x = width / 2;
y = height / 2;
let inf = 0;
let sup = 255;
let r = R.random_int(inf, sup);
let g = R.random_int(inf, sup);
let b = R.random_int(inf, sup);
let dist = abs(r - 127) + abs(g - 127) + abs(b - 127);
while (dist < 60) {
r = R.random_int(inf, sup);
g = R.random_int(inf, sup);
b = R.random_int(inf, sup);
dist = abs(r - 127) + abs(g - 127) + abs(b - 127);
}
c1 = color(r, g, b);
c2 = color(255-r, 255-g, 255-b);
let iter = R.random_int(minIter, maxIter);
for (let i = 1; i <= iter; i++) {
InC();
}
}
function draw() {
}
function InC() {
step = startStep;
let s = R.random_dec();
strokeWeight(weight);
if (s < 0.2)
{
step = step * 1;
} else if (s < 0.5)
{
step = step * 1.5;
} else if (s < 0.85)
{
step = step * 3;
} else if (s < 0.95)
{
step = step * 6;
strokeWeight(weight * 1.5);
} else {
step = step * 12.5;
strokeWeight(weight * 3);
}
let r = R.random_num(-step, step);
if ( ((x + r) > width) | ((x + r) < 0) ) {
xn = x - r;
} else {
xn = x + r;
}
r = R.random_num(-step, step);
if ( ((y + r) > height) | ((y + r) < 0) ) {
yn = y - r;
} else {
yn = y + r;
}
if (R.random_dec() < 0.8) {
let t = 20;
if (R.random_dec() < 0.8) {
stroke(c1);
line(x, y, xn, yn);
} else {
noStroke();
fill(c1, opacity);
triangle(x, y, xn, yn, (x + xn) / 2 + R.random_num(-t, t), (y + yn) / 2 + R.random_num(-t, t));
}
} else {
let t = 30;
if (R.random_dec() < 0.8) {
stroke(c2);
noFill();
} else {
noStroke();
fill(c2, opacity);
}
bezier(x, y, x + R.random_num(-t, t), y + R.random_num(-t, t), xn + R.random_num(-t, t), yn + R.random_num(-t, t), xn, yn);
}
if (R.random_dec() > teleport) {
x = xn;
y = yn;
} else {
x = R.random_int(10, width - 10);
y = R.random_int(10, height - 10);
}
}
// delete this before deployment
function random_hash() {
let x = "0123456789abcdef";
let hash = '0x';
for (let i = 64; i > 0; --i) {
hash += x[Math.floor(Math.random() * x.length)];
}
return hash;
}
class Random {
constructor() {
this.useA = false;
let sfc32 = function (uint128Hex) {
let a = parseInt(uint128Hex.substr(0, 8), 16);
let b = parseInt(uint128Hex.substr(8, 8), 16);
let c = parseInt(uint128Hex.substr(16, 8), 16);
let d = parseInt(uint128Hex.substr(24, 8), 16);
return function () {
a |= 0; b |= 0; c |= 0; d |= 0;
let t = (((a + b) | 0) + d) | 0;
d = (d + 1) | 0;
a = b ^ (b >>> 9);
b = (c + (c << 3)) | 0;
c = (c << 21) | (c >>> 11);
c = (c + t) | 0;
return (t >>> 0) / 4294967296;
};
};
let hash = random_hash();
// seed prngA with first half of tokenData.hash
//this.prngA = new sfc32(tokenData.hash.substr(2, 32));
this.prngA = new sfc32(hash.substr(2, 32));
// seed prngB with second half of tokenData.hash
//this.prngB = new sfc32(tokenData.hash.substr(34, 32));
this.prngB = new sfc32(hash.substr(34, 32));
for (let i = 0; i < 1e6; i += 2) {
this.prngA();
this.prngB();
}
}
// random number between 0 (inclusive) and 1 (exclusive)
random_dec() {
this.useA = !this.useA;
return this.useA ? this.prngA() : this.prngB();
}
// random number between a (inclusive) and b (exclusive)
random_num(a, b) {
return a + (b - a) * this.random_dec();
}
// random integer between a (inclusive) and b (inclusive)
// requires a < b for proper probability distribution
random_int(a, b) {
return Math.floor(this.random_num(a, b + 1));
}
}
function mousePressed() {
x = mouseX;
y = mouseY;
let iter = size / 40;
teleport = 0;
for (let i = 1; i <= iter; i++) {
InC();
}
}
function keyReleased() {
if (key === 'c' || key === 'C') {
if (head) {
background(0);
} else {
background(255);
}
head = !head;
}
if (key === 's' || key === 'S') {
save("InC" + frameCount + ".jpg");
}
}