I am trying to create an animation where I have a simple red block which changes to a blue block of the same size if I click a button. I wrote my code but it doesn't work and I don't know what's wrong with it. I think that it is something to do with the 'switch' and 'case' functions(?(I'm not sure if they're called 'functions')) I was wondering if anyone knew what was wrong. This is my code:
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
function switch1(){
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.rect(20, 20, 150, 100);
ctx.fillStyle = "red";
ctx.fill();
}
var status = 0;
function switch2(){
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.rect(20, 20, 150, 100);
ctx.fillStyle = "blue";
ctx.fill();
}
function backtored(){
var canvas = document.getElementById("myCanvas")
var ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
switch(Number(status)){
ctx.beginPath();
ctx.rect(20,20,150,100);
ctx.fill()
}
}
function swappy(){
switch(Number(status)){
case 0:
switch1();
status = 1;
case 1:
switch2();
status = 0;
}
}
</script>
<body onload="swappy();backtored()">
<button type="button" onclick="swappy()">Change colour</button>
</body>
</html>
When I run it, there is just a blank box with a button next to it saying "Change colour". Can someone please help and alert me to my mistakes. Thanks