Move your mouse and press S

The P5.js code provided creates a dynamic canvas animation, where ellipses follow the mouse cursor, featuring random colors and sizes. The setup begins with the setup() function, initializing a canvas of 600×600 pixels (lines 2-3) and positioning it at the top-left corner (line 4). The background color is set to a dark gray (line 5), and color mode is switched to HSB with a range of 255 for better color control (line 6). Additionally, ellipses are drawn from the center (line 7), and stroke weight is set to 4 pixels for outline thickness (line 8).

The draw() function (line 10) runs in a loop, continuously executing to create the animation. A random hue (rndC) is generated for the color, and a random radius offset (rndR) is determined (lines 11-12). The stroke color is set using HSB values with a hue opposite to rndC for a complementary effect (line 13), while the fill color uses rndC for the hue (line 14). The ellipse is drawn at the mouse’s position with a size influenced by rndR (line 15).

The keyPressed() function (line 17) checks for the ‘s’ key press (line 18). If detected, it stops the animation loop (line 19) and saves the canvas as ‘Canvas.png’ (line 20).

function setup() {
  let cnv=createCanvas(600,600);
  cnv.position(0,0);
  background(30); 
  colorMode(HSB,255);
  ellipseMode(CENTER);
  strokeWeight(4);
}

function draw() {
  let rndC=random(0,255);
  let rndR=random(0,100);
  stroke(255-rndC,255,255,50);
  fill(rndC,255,255,75);
  
  ellipse(mouseX,mouseY,75+rndR,75+rndR);
}
function keyPressed() {
  if(key==='s'){
    noLoop();
    saveCanvas('Canvas.png')

}

Scroll to top