xxxxxxxxxx
157
//https://breaksome.tech/creating-an-interactive-fractal-in-p5js/
let size = 20;//220;
let angle = 60;
/*Cool angle Values:
20 = Flower
45 = Grid
60 = Snowflake
90 = Funky
120 = Sirpinski Triangle
*/
//Set animate to 1 to animate the fractal / 0 for static image
let animate = 0;
//Set mouseMove to 1 to bind the angle and size to your mouse!
//Generate your own Fractal!
let mouseMove = 0;
let xspacing = 16; // Distance between each horizontal location
let w; // Width of entire wave
let theta = 0.0; // Start angle at 0
let amplitude = 75.0; // Height of wave
let period = 500.0; // How many pixels before the wave repeats
let dx; // Value for incrementing x
let yvalues; // Using an array to store height values for the wave
let fps = 30;
function drawShadow(b,c) {
drawingContext.shadowOffsetX = 2;
drawingContext.shadowOffsetY = 2;
drawingContext.shadowBlur = b;
drawingContext.shadowColor = c; //"black";
}
function setup() {
createCanvas(800, 600, P2D);
w = width + 16;
dx = (TWO_PI / period) * xspacing;
yvalues = new Array(floor(w / xspacing));
// remap amplitude as ratio to window height
amplitude = map(amplitude, 0, 350, 0, windowHeight);
frameRate(fps);
}
function draw() {
background(0);
stroke(255);
strokeWeight(1);
calcWave();
renderWave();
//translate(width / 2, height / 2);
//createFractal(size, angle);
//if (animate == 1) {
// angle += 0.5;
//}
//if (mouseMove == 1) {
// angle = map(mouseX, 0, width, 0, 180, true);
// size = map(mouseY, 0, height, 100, 250, true);
// }
//if (frameCount > 10)
//noLoop();
}
function createFractal(size, angle) {
if (size > 3) {
stroke(0, 250 - size, 0);
strokeWeight(map(size, 0, 180, 0, 10, true));
//Up Right
push();
rotate(radians(angle));
line(0, 0, size, 0);
translate(size, 0);
createFractal(size / 2, angle);
pop();
//Down Right
push();
rotate(radians(-angle));
line(0, 0, size, 0);
translate(size, 0);
createFractal(size / 2, angle);
pop();
//Up Left
push();
rotate(radians(angle * -3));
line(0, 0, size, 0);
translate(size, 0);
createFractal(size / 2, angle);
pop();
//Down Left
push();
rotate(radians(angle * 3));
line(0, 0, size, 0);
translate(size, 0);
createFractal(size / 2, angle);
pop();
}
}
function calcWave() {
// Increment theta (try different values for
// 'angular velocity' here)
theta += 0.02;
// For every x value, calculate a y value with sine function
let x = theta;
for (let i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x) * amplitude;
x += dx;
}
}
function renderWave() {
noStroke();
fill(255);
// A simple way to draw the wave with an ellipse at each location
for (let x = 0; x < yvalues.length; x++) {
let s1;
let s2;
if ((x % 2) == 0) {
s1 = 1;
s2 = 5;
} else {
s1 = 5;
s2 = 1;
}
// avoid re-calculating
let _x = x*xspacing;
// top line
drawShadow(5, color(0,255,0));
strokeWeight(s1);
stroke(map(x,0,yvalues.length,255,0));
line(_x,height/2+yvalues[x],_x,0);
// bottom line
stroke(map(x,0,yvalues.length,0,255));
strokeWeight(s2);
line(_x,height/2+yvalues[x],_x,height);
// circle
//stroke(0);
//strokeWeight(1);
//ellipse(_x, height / 2 + yvalues[x], 8, 8);
push();
translate(_x, height/2+yvalues[x]);
drawShadow(5, color(255,0,255));
createFractal(size, angle);
pop();
angle += .05;
}
}