Uso di frameCount con l'operatore %
Per ottenere un valore che riparta da 0 superato un certo limite, è possibile utilizzare l'operatore % (modulo).
// height=100 lines=auto
function setup() {
createCanvas( 300, 100);
}
function draw() {
background(220);
let x = (frameCount-1) % width;
circle( x, 50, 10 );
}
let x = (frameCount-1) % width;
L'operatore %
restituisce il resto della divisione e permette di ripartire da 0 ogni volta che frameCount-1
diventa un multiplo esatto di width
.
Provare a sostituire width
con un valore esplicito inferiore a 300.