xxxxxxxxxx
269
let shapeTypes = ['Circle', 'Spikey', 'Triangle', 'Star', 'Sinusoidal', 'ShapeRing'];
let mandalas = [];
let maxMandalas = 1;
function keyPressed() {
if (key === ' ') { // Check if the pressed key is space
save("test.svg");
}
}
function setup() {
createCanvas(windowWidth, windowHeight, SVG);
colorMode(HSB, 255);
noFill();
frameRate(30);
}
function draw() {
clear();
background(0);
for (let mandala of mandalas) {
mandala.update();
mandala.display();
}
}
function mousePressed() {
let newMandala = new Mandala(mouseX, mouseY, random(50, 200), 10); // Starting with 10 layers
mandalas.push(newMandala);
if (mandalas.length > maxMandalas) mandalas.shift();
}
class Mandala {
constructor(x, y, radius, maxLayers) {
this.shapes = [];
this.layers = int(random(1, maxLayers + 1)); // Ensure layers don't exceed maxLayers
this.baseColor = random(255);
let totalRadius = 0;
for (let i = 0; i < this.layers; i++) {
let layerType = random(shapeTypes);
let layerRadius = radius * ((i + 1) / this.layers);
totalRadius += layerRadius;
let hue = (this.baseColor + i * 25) % 255;
let shape;
switch (layerType) {
case 'Circle':
shape = new Circle(x, y, totalRadius, hue);
break;
case 'Spikey':
shape = new Spikey(x, y, totalRadius, hue);
break;
case 'Triangle':
shape = new Triangle(x, y, totalRadius, hue);
break;
case 'Star':
shape = new Star(x, y, totalRadius, hue);
break;
case 'Sinusoidal':
shape = new Sinusoidal(x, y, totalRadius, hue);
break;
case 'ShapeRing':
shape = new ShapeRing(x, y, totalRadius, hue, random(shapeTypes), this.layers);
break;
}
if (shape) {
this.shapes.push(shape);
}
}
}
update() {
for (let shape of this.shapes) {
shape.update();
}
}
display() {
for (let shape of this.shapes) {
shape.display();
}
}
}
class Shape {
constructor(x, y, radius, hue) {
this.x = x;
this.y = y;
this.radius = radius;
this.hue = hue;
this.angle = 0;
this.angleSpeed = random(0.01, 0.05);
this.scaleFactor = 1;
this.scalingSpeed = random(0.005, 0.015);
}
update() {
this.angle += this.angleSpeed;
this.scaleFactor += this.scalingSpeed;
if (this.scaleFactor > 1.3 || this.scaleFactor < 0.7) {
this.scalingSpeed *= -1;
}
}
display() {
push();
translate(this.x, this.y);
scale(this.scaleFactor);
rotate(this.angle);
this.draw();
pop();
}
draw() {
// This method will be implemented by subclasses
}
}
class Circle extends Shape {
draw() {
stroke(this.hue, 200, 255);
ellipse(0, 0, this.radius * 2);
}
}
class Spikey extends Shape {
draw() {
stroke(this.hue, 200, 255);
this.drawSpikeyCircle();
}
drawSpikeyCircle() {
beginShape();
let spikes = 10;
for (let i = 0; i < TWO_PI; i += TWO_PI / spikes) {
let x = (this.radius + 10 * sin(i * 5)) * cos(i);
let y = (this.radius + 10 * sin(i * 5)) * sin(i);
vertex(x, y);
}
endShape(CLOSE);
}
}
class Triangle extends Shape {
draw() {
stroke(this.hue, 200, 255);
this.drawPolygon(3);
}
drawPolygon(sides) {
beginShape();
for (let i = 0; i < TWO_PI; i += TWO_PI / sides) {
let x = this.radius * cos(i);
let y = this.radius * sin(i);
vertex(x, y);
}
endShape(CLOSE);
}
}
class Star extends Shape {
draw() {
stroke(this.hue, 200, 255);
this.drawStar();
}
drawStar() {
beginShape();
for (let i = 0; i < TWO_PI; i += PI / 5) {
let outerX = this.radius * cos(i);
let outerY = this.radius * sin(i);
vertex(outerX, outerY);
let innerX = this.radius * 0.5 * cos(i + PI / 10);
let innerY = this.radius * 0.5 * sin(i + PI / 10);
vertex(innerX, innerY);
}
endShape(CLOSE);
}
}
class Sinusoidal extends Shape {
constructor(x, y, radius, hue) {
super(x, y, radius, hue);
this.sinOffset = random(100);
}
update() {
super.update();
this.sinOffset += 0.05;
}
draw() {
stroke(this.hue, 200, 255);
this.drawSinusoidalCircle();
}
drawSinusoidalCircle() {
beginShape();
for (let i = 0; i < TWO_PI; i += 0.1) {
let r = this.radius + 10 * sin(5 * i + this.sinOffset);
let x = r * cos(i);
let y = r * sin(i);
vertex(x, y);
}
endShape(CLOSE);
}
}
class ShapeRing extends Shape {
constructor(x, y, radius, hue, shapeType, parentLayers) {
super(x, y, radius, hue);
this.shapeType = shapeType;
this.smallShapes = [];
let seed = Math.floor(random(10000)); // Generate a seed for the random number generator
for (let i = 0; i < TWO_PI; i += TWO_PI / 12) {
let shapeRadius = this.radius / 4; // Adjusted size for inner mandalas
let shapeX = this.x + (shapeRadius * 1.5 * cos(i)); // Adjusted positioning
let shapeY = this.y + (shapeRadius * 1.5 * sin(i)); // Adjusted positioning
let shape;
if (parentLayers > 1) {
randomSeed(seed); // Set the seed for the random number generator
let childMandala = new Mandala(shapeX, shapeY, shapeRadius, parentLayers - 1);
this.smallShapes.push(childMandala);
} else {
switch (shapeType) {
case 'Circle':
shape = new Circle(shapeX, shapeY, shapeRadius, hue);
break;
case 'Spikey':
shape = new Spikey(shapeX, shapeY, shapeRadius, hue);
break;
case 'Triangle':
shape = new Triangle(shapeX, shapeY, shapeRadius, hue);
break;
case 'Star':
shape = new Star(shapeX, shapeY, shapeRadius, hue);
break;
case 'Sinusoidal':
shape = new Sinusoidal(shapeX, shapeY, shapeRadius, hue);
break;
}
if (shape) {
this.smallShapes.push(shape);
}
}
}
}
update() {
super.update();
for (let shape of this.smallShapes) {
shape.update();
}
}
display() {
for (let shape of this.smallShapes) {
shape.display();
}
}
}