xxxxxxxxxx
68
// Variables below have a global scope.
// Can be accessed throughout the program by any function.
let radius = 0;
let angle = 0;
let radiusInc;
let angleInc;
let hue;
let hueInc;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
angleMode(DEGREES); // More convenient for understanding
colorMode(HSB); // Again, for for easier understanding
// H - Hue : Ranges from 0 - 360
// S - Saturation : Ranges from 0 - 100
// B - Brightness : Ranges from 0 - 100
reset(); // Basically sets everything up from Start
}
function draw() {
translate(width / 2, height / 2); // Changes the Origin to the center of our canvas instead of the default top-left point
// Incrementing angle and radius every time setup is called
angle += angleInc;
radius += radiusInc;
// Polar representation of the Two Coordinates
let x = radius * cos(angle);
let y = radius * sin(angle);
// Increments the hue value to change the color
hue += hueInc;
// Drawing the point on the Canvas
strokeWeight(7);
stroke(color(hue, 60, 100));
point(x, y);
// Resetting the hue value whenever it goes out of range
if (hue > 360) {
hue = 0;
}
// Reverse direction of movement so that spiral moves inwards to give desired pattern
if (radius > height / 2 - 25) {
// 25 is for padding, aesthetics only
radiusInc = -radiusInc;
}
// Everytime the entire flower pattern is printed, reset() function is called to repeat the process with a new config.
if (radius < 0) {
reset();
}
}
function reset() {
background(0);
radiusInc = random(0.5,2) // Lower Value = Closer Points
let numArms = floor(random(3, 8)); // floor to get an integer
let curl = random(0.3, 1); // Higher Value = More Twist
// Now the change in angle (angleInc) is 360/numArms + curl
// Curl is what makes the lines curve instead of being straight
angleInc = 360 / numArms + curl;
hue = random(0, 360);
hueInc = random(1, 5);
}