Introduzione a p5.js

:play_button:  provare a eseguire il codice dopo la modifica

01. Funzioni setup() e draw()

02. Colori (e background())

Modifiche complessive

  function setup() {
    colorMode(HSL)
    background(210, 50, 80)
  }

03. Dimensioni del canvas

  function windowResized() {
    resizeCanvas(windowWidth, windowHeight)
  }

Modifiche complessive

  function setup() {
    createCanvas(windowWidth, windowHeight)
    colorMode(HSL)
    background(210, 50, 80)
  }
  function windowResized() {
    resizeCanvas(windowWidth, windowHeight)
  }

04. Disegno di figure geometriche

Modifiche complessive

  ...
  function draw() {
    fill(200, 55, 85)
    stroke(100)
    strokeWeight(1)
    circle(50, 50, 100)
  }
  ...

05. Variabili e parametrizzazione

  let x = 50
  let y = 50
  let diameter = 100
  function setup() {
    ...
  }
  let offset = diameter / 5  // variabile locale utilizzabile solo
                             // nel blocco ({...}) in cui è definita
  circle(x-offset, y-offset, diameter/8)

Modifiche complessive

  let x = 50
  let y = 50
  let diameter = 100
  function setup() {
    ...
  }
  function draw() {
    fill(200, 55, 85)
    stroke(100)
    strokeWeight(1)
    circle(x, y, diameter)

    fill(100);
    noStroke();
    let offset = diameter * 0.2
    circle(x-offset, y-offset, diameter/8)
  }

06. Animazione

Modifiche complessive

  ...
  let y = height
  ...
  let speed = 5
  function setup() {
    ...
  }
  function draw() {
    background(210, 50, 80)
    ...
    y = y - speed
  }

07. Casualità

Modifiche complessive

  let diameter = random(50, 120)
  let x = random(width)
  let y = height + diameter/2
  let speed = diameter / 30
  ...

08. Classi e proprietà

  class Bubble {
    
  }
  ... // variabili globali
  function setup() {
    ...
  }
  function draw() {
    ...
  }
  ...
  class Bubble {
    constructor() {
      this.diameter = random(50, 120)
      this.x = random(width)
      this.y = height + diameter/2
      this.speed = diameter / 30
    }
  }

Modifiche complessive

  class Bubble {
    constructor() {
      this.diameter = random(50, 120)
      this.x = random(width)
      this.y = height + diameter/2
      this.speed = diameter / 30
    }
  }
  let bubble
  function setup() {
    ...
    bubble = new Bubble()
  }
  function draw() {
    ...
    circle(bubble.x, bubble.y, bubble.diameter)
    ...
    let offset = bubble.diameter * 0.2
    circle(bubble.x-offset, bubble.y-offset, bubble.diameter/8)
  }

09. Metodi delle classi

  class Bubble {
    ...
    display() {

    }
  }

Modifiche complessive

  class Bubble {
    ...
    display() {
      fill(200, 55, 85)
      stroke(255)
      strokeWeight(1)
      circle(this.x, this.y, this.diameter)
      
      fill(100)
      noStroke()
      let offset = this.diameter * 0.2
      circle(this.x-offset, this.y-offset, this.diameter/8)
    }  
  }
  ...
  function draw() {
    background(210, 50, 80)
    bubble.display()
  }

10. Array di istanze

Modifiche complessive

  ...
  let bubbles = []
  function setup() {
    ...
    bubbles[0] = new Bubble()
    bubbles[1] = new Bubble()
  }
  function draw() {
    background(210, 50, 80)
    bubbles[0].display()
    bubbles[1].display()
  }

11. Iterazione di istruzioni (for)

    for (let i = 0; i < totalBubbles; i++) {
      let b = new Bubble()
      bubbles.push(b)
    }
    for (let i = 0; i < bubbles.length; i++) {
      let b = bubbles[i]
      b.display()
    }

Modifiche complessive

  let bubbles = []
  let totalBubbles = 50
  function setup() {
    ...
    for (let i = 0; i < totalBubbles; i++) {
      let b = new Bubble()
      bubbles.push(b)
    }
  }
  function draw() {
    background(210, 50, 80)
    for (let i = 0; i < bubbles.length; i++) {
      let b = bubbles[i]
      b.display()
    }
  }

12. Istruzioni condizionali

  if (this.y < 0) {
    this.y = height + this.diameter/2;
  }

Modifiche complessive

  class Bubble {
    ...
    display() {
      ...
      if (this.y < this.diameter/2) {
        this.y = height + this.diameter/2;
      }
    }  
  }

13. Interattività

  function mousePressed() {
    bubbles.push( new Bubble() )
  }
  let b = new Bubble()
  b.x = mouseX
  b.y = mouseY
  bubbles.push( b )

Modifiche complessive

  function setup() {
    ...
    cursor(HAND)
  }
  function draw() {
    ...
  }
  function mousePressed() {
    let b = new Bubble()
    b.x = mouseX
    b.y = mouseY
    bubbles.push( b )
  }

14. Aggiunta proprietà (life)

    if (this.life > 0) {
        this.life = this.life - 1;
    }
  class Bubble {
    ...
    update() {
      
    }  
  }
  class Bubble {
    ...
    update() {
      this.y = this.y - this.speed;
      if (this.y < -this.diameter/2) {
        this.y = height+this.diameter/2;
      }
      if (this.life > 0) {
        this.life = this.life - 1;
      }
    }  
  }
  let b = bubbles[i];
  b.update();
  if (b.life > 0) {
    b.display();
  }
  if (b.life > 0) {
    ...
  } else {
    bubbles.splice(i, 1)
  }

Modifiche complessive

  class Bubble {
    constructor() {
      ...
      this.life = random( 100, 1000 );
    }
    display() {
      fill(200, 55, 85);
      stroke(255);
      strokeWeight(1);
      circle(this.x, this.y, this.diameter);

      fill(100);
      noStroke();
      let offset = this.diameter * 0.2;
      circle(this.x - offset, this.y - offset, this.diameter / 8);
    }
    update() {
      this.y = this.y - this.speed;
      if (this.y < -this.diameter/2) {
        this.y = height+this.diameter/2;
      }
      if (this.life > 0) {
        this.life = this.life - 1;
      }
    }  
  }
  ...
  function draw() {
    ...
    for ( let i=bubbles.length-1;  i >= 0;  i=i-1 ) {
      let b = bubbles[i];
      b.update()
      if (b.life > 0) {
        b.display()
      } else {
        bubbles.splice(i, 1);
      }
    }
  }
  function mousePressed() {
    ...
    bubbles.unshift( b );
  } 

Riferimenti

Codice finale

\

class Bubble {
  constructor() {
    this.diameter = random(50, 120);
    this.x = random(width);
    this.y = height+this.diameter/2;
    this.speed = this.diameter / 30;
    this.life = random( 100, 1000 );
  }
  display() {
    fill(200, 55, 85);
    stroke(255);
    strokeWeight(1);
    circle(this.x, this.y, this.diameter);

    fill(100);
    noStroke();
    let offset = this.diameter * 0.2;
    circle(this.x - offset, this.y - offset, this.diameter / 8);
  }
  update() {
    this.y = this.y - this.speed;
    if (this.y < -this.diameter/2) {
      this.y = height+this.diameter/2;
    }
    if (this.life > 0) {
      this.life = this.life - 1;
    }
  }
}

let bubbles = [];
let totalBubbles = 100;

function setup() {
  createCanvas(windowWidth, windowHeight);
  colorMode(HSL);

  for ( let i=0;  i < totalBubbles;  i=i+1 ) {
      bubbles[i] = new Bubble();
  }
}

function draw() {
  background(210, 50, 80);
  
  for ( let i=bubbles.length-1;  i >= 0;  i=i-1 ) {
    let b = bubbles[i];
    b.update()
    if (b.life > 0) {
      b.display()
    } else {
      bubbles.splice(i, 1);
    }
  }
}

function mousePressed() {
  let b = new Bubble();
  b.x = mouseX;
  b.y = mouseY;
  bubbles.unshift( b );
} 

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
  background(210, 50, 80);
}

Codice completo nel web editor:
Full immersion in p5.js

Esempio di partenza:
How to Program Interactive Art With p5.js.

Introduzione ai concetti base di p5.js:
Introduzione a p5.js

Guida di riferimento alle variabili e alle istruzioni di p5.js:
P5.js Reference