Added more documenattion for better soluction

This commit is contained in:
sam
2024-01-23 14:10:07 +01:00
parent 1d79f45694
commit 477c24f7c0

View File

@@ -40,4 +40,49 @@ function canvas2(){
fill(255, 0, 0)
circle (300, 200, 50);
}
```
```
Wat ik later heb gevonden is een betere manier waarbij je een variabel een int maakt en dan een switch statement gebruikt. Dit is een stuk beter dan wat ik heb gedaan. Hieronder staat de code:
```js
let canvasSwitch = 0;
function setup(){
createCanvas(400, 400);
}
function draw(){
//draw a background every frame
background(220)
//switch canvas when spacebar is pressed
if (canvasSwitch == 0){
canvas1();
}
if (canvasSwitch == 1){
canvas2();
}
}
function keyPressed(){
if (keyCode == 32){ // 32 is the keyCode for the space bar
canvasSwitch = 1
}
if (keyCode == 16){ // 16 is the keyCode for the Shift key
canvasSwitch = 0
}
}
//draw a yellow circle on canvas 1
function canvas1(){
fill(255, 255, 0)
circle (200, 200, 50);
}
//draw a red circle on canvas 2
function canvas2(){
fill(255, 0, 0)
circle (300, 200, 50);
}
```