76 lines, 13 of them comments ยท 2,506 tokens, written in 22 seconds for $0.025


 1  let planets = [];
 2  const numPlanets = 10;
 3  
 4  // A class to describe a planet
 5  class Planet {
 6    constructor() {
 7      // Ensure orbits are within a reasonable range
 8      let maxOrbit = min(width, height) / 2 - 20;
 9      let minOrbit = 50;
10      this.distance = random(minOrbit, maxOrbit);
11      this.radius = random(4, 12);
12      this.angle = random(TWO_PI);
13      // Slower planets for farther distances to simulate gravity
14      this.speed = map(this.distance, minOrbit, maxOrbit, 0.02, 0.005);
15      this.hue = random(360);
16    }
17  
18    update() {
19      this.angle += this.speed;
20    }
21  
22    display() {
23      // Calculate position based on angle and distance
24      let x = this.distance * cos(this.angle);
25      let y = this.distance * sin(this.angle);
26  
27      fill(this.hue, 80, 95);
28      noStroke();
29      ellipse(x, y, this.radius * 2);
30    }
31  }
32  
33  function setup() {
34    createCanvas(windowWidth, windowHeight);
35    colorMode(HSB, 360, 100, 100, 1);
36    // Create an initial set of planets
37    createPlanets();
38  }
39  
40  function draw() {
41    // Use a semi-transparent background for fading trails
42    background(0, 0, 5, 0.1);
43  
44    // Center the coordinate system
45    translate(width / 2, height / 2);
46  
47    // Draw the central "sun"
48    fill(50, 100, 100);
49    noStroke();
50    ellipse(0, 0, 40);
51  
52    // Update and display each planet
53    for (let planet of planets) {
54      planet.update();
55      planet.display();
56    }
57  }
58  
59  // Reset sketch on mouse press
60  function mousePressed() {
61    createPlanets();
62  }
63  
64  // Helper function to generate new planets
65  function createPlanets() {
66    planets = []; // Clear existing planets
67    for (let i = 0; i < numPlanets; i++) {
68      planets.push(new Planet());
69    }
70  }
71  
72  // Adjust canvas size when window is resized
73  function windowResized() {
74    resizeCanvas(windowWidth, windowHeight);
75  }
76