// Francesco Paolini @francescopaolini © 2017 MIT License
// 10 print porting to P5js
// Made for DSII2017 lab @UniRSM

var x = 0;
var y = 0;
var dimensioni = [ 20, 40, 80, 100 ];
var D;
var x1 = 10;
var y1 = 10;
var x3 = 10;
var y3 = 10;
var x4 = 10;
var y4 = 10;

function setup() {
  createCanvas(1310, 700);
  background(255);
  frameRate(60);

  var i = int(random(4));
D = dimensioni[i];

  y1 = (height-D);
  x3 = (width-D);
  y3 = (height-D);
  x4 = (width-D);
}


function draw() {

//  forma1();
  forma2();
  forma3();
  forma4();
}


//------------------------------forma 1------------------------------//
function forma1() {
  fill(random(255,40));
  noStroke();
  rect(x, y, D, D);

  x = x + D;
  if (x >= width) {
    x=0;
    y = y+D;
  }
  if (y >= height) {
    background(random(255));
    x=0;
    y=0;
  }
}
//------------------------------forma 2------------------------------//
function forma2() {
  fill(100, 40);
  noStroke();
ellipse(x3, y3, D, D);


  if (y1 <= 0) {
    y1 = (height);
    x1 = x1+D;
  }
  y1 = y1 - D;

  if (x1 >= width ) {
    background(255);
    x1 = 0;
    y1 = (height-D);
  }
}
//------------------------------forma 3------------------------------//
function forma3() {
  fill(100, 40);
  noStroke();
  ellipse(x3, y3, D, D);
  x3 = x3 - D;
  if (x3 < 0) {
    x3 = width-D;
    y3 = y3-D;
  }

  if (y3 <= 0-D) {
    background(random(255));
    x3 = (width-D);
    y3 = (height-D);
  }
}
//------------------------------forma 4------------------------------//
function forma4() {
  fill(random(255));
  noStroke();
triangle(x4, y4+D, x4+D/2, y4, x4+D, y4+D);

  y4 = y4 + D;
  if (y4 >= height) {
    y4 = 0;
    x4 = x4 -D;
  }

  if (x4 <= 0-D) {
    setup();
    background(random(255));
    x4 = (width-D);
    y4 = 0;
  }
}


// se premi "s" salva come immagine
function keyTyped() {

  if (key == 'a') {
    noLoop();
  } else {
  }
  if (key == 's') {  // se premo f lo fermo
    saveFrame("variazione-######.png");     //salva il frame, "fai un screenshot della finestra"
  } else {
  }
  if (key == 'd') { // se premo d riparte
    loop();
  } else {
  }if (key == 'q') { // se premo d riparte
    background(255);
  }
}

@@ -323,16 +323,32 @@
 nd(255);
+ //sfondo bianco
 %0A  frame
@@ -356,16 +356,37 @@
 ate(60);
+ //60 frame a secondo
 %0A%0A  var 
@@ -502,17 +502,16 @@
 -D);%0A%7D%0A%0A
-%0A
 function
@@ -520,19 +520,16 @@
 raw() %7B%0A
-%0A//
   forma1
@@ -571,17 +571,16 @@
 4();%0A%7D%0A%0A
-%0A
 //------
@@ -826,24 +826,53 @@
 andom(255));
+ //sfondo random ad un canale
 %0A    x=0;%0A  
@@ -1007,16 +1007,18 @@
 roke();%0A
+  
 ellipse(
@@ -1147,16 +1147,45 @@
 nd(255);
+ //sfondo random ad un canale
 %0A    x1 
@@ -1874,44 +1874,89 @@
 %0A%7D%0A%0A
-%0A
 //
- se premi %22s%22 salva come immagine
+------------------------------utilizzo tastiera------------------------------//
 %0Afun
@@ -1994,16 +1994,50 @@
 = 'a') %7B
+ // se premi %22a%22 si blocca il loop
 %0A    noL
@@ -2093,20 +2093,43 @@
 prem
-o f lo fermo
+i %22s%22 fai uno screenshot del canvas
 %0A   
@@ -2137,15 +2137,16 @@
 save
-Frame(%22
+Canvas('
 vari
@@ -2155,78 +2155,83 @@
 ione
--######.png%22);     //salva il frame, %22fai un screenshot della finestra
+', 'png'); //viene salvato nella cartella download col nome %22variazione.png
 %22%0A  
@@ -2269,35 +2269,37 @@
 %7B // se prem
-o d
+i %22d%22
  riparte
 %0A    loop();
@@ -2282,24 +2282,32 @@
  %22d%22 riparte
+ il loop
 %0A    loop();
@@ -2353,19 +2353,34 @@
 prem
-o d riparte
+i %22q%22 si pulisce il canvas
 %0A   

Update sketch.js