xxxxxxxxxx
61
// Praveen Raj R
// Animated Snowflake
let lineArray = [];
let orderOfRotation;
let orderSlider;
function setup() {
createCanvas(600, 600);
angleMode(DEGREES);
stroke(255);
strokeWeight(4);
noFill();
orderSlider = createSlider(1, 10, 6)
}
function draw() {
orderOfRotation = orderSlider.value();
background(0);
translate(width / 2, height / 2);
for (let order = 0; order < orderOfRotation; order++) {
for (let i = 0; i < lineArray.length; i++) {
beginShape();
for (let j = 0; j < lineArray[i].length; j++) {
vertex(lineArray[i][j][0], lineArray[i][j][1]);
}
endShape();
}
for (let i = 0; i < lineArray.length; i++) {
beginShape();
for (let j = 0; j < lineArray[i].length; j++) {
vertex(lineArray[i][j][0], lineArray[i][j][1] * -1);
}
endShape();
}
rotate(360 / orderOfRotation);
}
for (let i = lineArray.length - 1; i >= 0; i--) {
for (let j = lineArray[i].length - 1; j >= 0; j--) {
lineArray[i][j][0] += 2;
if (lineArray[i][j][0] > 450) {
lineArray[i].splice(j, 1);
}
}
if (lineArray[i].length < 1) {
lineArray.splice(i, 1);
}
}
}
function mousePressed() {
lineArray.push([
[mouseX - width / 2, mouseY - height / 2]
]);
}
function mouseDragged() {
if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
lineArray[lineArray.length - 1].push([mouseX - width / 2, mouseY - height / 2]);
}
}