xxxxxxxxxx
47
/*
Objectives:
- Learn Arrays.
- Declare variables with const. Difference between const and let.
- Event listeners
*/
// This code creates a list of elements. The list is of length 11.
const emojis = ['🦄','👾','🐰','🐳','🍄'];
// We access the first element on the emojis array at position zero (0).
let chosen = emojis[0];
function setup() {
createCanvas(400, 400);
}
function draw() {
background(255);
textAlign(CENTER, CENTER);
textSize(32);
text('emojis.length = ' + emojis.length,200,50)
textSize(200);
text(chosen, width/2, height/2);
}
// this function is provided by p5. It creates an event listener that is enabled when the mouse is clicked.
function mouseClicked(){
let randomIndex = floor(random(emojis.length-1));
chosen = emojis[randomIndex];
console.log('randomIndex ', randomIndex)
}
function keyPressed(e){
if(key === 'd'){
emojis.push('🐲');
console.log(emojis);
}
if(key === 'x'){
emojis.pop();
console.log(emojis);
}
}