I just found a simple fractal that I want to recreate with P5.js
This is my code:
function setup() {
createCanvas(400, 400);
noLoop();
angleMode(DEGREES)
}
function draw() {
translate(width / 2, height / 2);
fill(0,0,0);
fractal(-10, -100, 10, 200, 0.7);
}
function fractal(x, y, w, h, factor) {
if (w > 1 && h > 1) {
rect(x, y, w, h);
push();
rotate(90);
translate(y, 0);
scale(factor);
fractal(x, y, w, h, factor);
pop();
rect(x, y, w, h);
push();
rotate(90);
translate(-y, 0);
scale(factor);
fractal(x, y, w, h, factor);
pop();
}
}
html, body {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
html {
flex-direction: column;
}
body {
flex-direction: row;
margin: 0;
}
canvas {
margin: auto;
display: block;
box-shadow: 0px 2px 12px -2px rgb(0 0 0 / 30%);
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.min.js"></script>
I probably do something silly, but I’m not able to find where I made a mistake.
I only draw one branch of the fractal. If I comment out code between push() and pop() it draws the other branch.