Generative Art on p5js by The Philosopher.
Code:
let stickFigures = []; let poses = [ { type: "human", pose: "standing" }, { type: "human", pose: "running" }, { type: "human", pose: "walking" }, { type: "animal", pose: "dog" }, { type: "animal", pose: "cat" }, { type: "animal", pose: "bird" } ];
function setup() { createCanvas(800, 600); background(255); }
function draw() { background(255); // Clear the background on each frame
// Move and draw existing stick figures for (let i = stickFigures.length - 1; i >= 0; i--) { let stickFigure = stickFigures[i]; stickFigure.move(); stickFigure.display(); if (stickFigure.isOffscreen()) { // Remove stick figure if it's offscreen stickFigures.splice(i, 1); } } }
function mousePressed() { // Create a new stick figure and add it to the array let pose = random(poses); if (pose.type === "human") { let newStickFigure = new StickFigureHuman(random(width), height, pose.pose); stickFigures.push(newStickFigure); } else if (pose.type === "animal") { let newStickFigure = new StickFigureAnimal(random(width), height, pose.pose); stickFigures.push(newStickFigure); } }
class StickFigure { constructor(x, y) { this.x = x; this.y = y; this.speed = random(1, 3); // Random speed }
move() { // Move stick figure up the screen this.y -= this.speed; }
isOffscreen() { // Check if stick figure is offscreen return this.y < -100; }
display() { // Abstract method to be overridden by subclasses } }
class StickFigureHuman extends StickFigure { constructor(x, y, pose) { super(x, y); this.pose = pose; this.size = 50; // Size of the stick figure }
display() { super.display(); // Draw stick figure human based on pose fill(0); // Black color for simplicity ellipse(this.x, this.y - this.size * 0.7, this.size * 0.4, this.size * 0.4); // Head line(this.x, this.y - this.size * 0.5, this.x, this.y + this.size * 0.5); // Body line(this.x - this.size * 0.3, this.y, this.x + this.size * 0.3, this.y); // Arms line(this.x, this.y + this.size * 0.5, this.x - this.size * 0.3, this.y + this.size * 1.2); // Legs line(this.x, this.y + this.size * 0.5, this.x + this.size * 0.3, this.y + this.size * 1.2); // Legs } }
class StickFigureAnimal extends StickFigure { constructor(x, y, pose) { super(x, y); this.pose = pose; this.size = 50; // Size of the stick figure }
display() { super.display(); // Draw stick figure animal based on pose fill(0); // Black color for simplicity ellipse(this.x, this.y - this.size * 0.7, this.size * 0.4, this.size * 0.4); // Head line(this.x, this.y - this.size * 0.5, this.x, this.y + this.size * 0.5); // Body line(this.x - this.size * 0.3, this.y, this.x + this.size * 0.3, this.y); // Arms line(this.x, this.y + this.size * 0.5, this.x - this.size * 0.3, this.y + this.size * 1.2); // Legs line(this.x, this.y + this.size * 0.5, this.x + this.size * 0.3, this.y + this.size * 1.2); // Legs } }