Compartilho minha solução:
<!DOCTYPE html>
<html lang="pt_BR">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
div {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border: 3px double brown;
padding: 25px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.6);
}
div h1,
h3 {
margin-bottom: 5px;
color: brown;
}
canvas {
background-color: lightgray;
}
</style>
<title>Lógica de Programação II</title>
</head>
<body>
<div>
<h1>04 - Movendo elementos: animações simples</h1>
<h3>09 - Trocando bandeiras</h3>
<canvas width="600" height="400"></canvas>
</div>
<script>
var flagDefault = drawBrazilFlag;
function drawBrazilFlag() {
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
context.fillStyle = "green";
context.fillRect(0, 0, 600, 400);
context.fillStyle = "yellow";
context.beginPath();
context.moveTo(300, 50);
context.lineTo(50, 200);
context.lineTo(550, 200);
context.fill();
context.beginPath();
context.moveTo(50, 200);
context.lineTo(300, 350);
context.lineTo(550, 200);
context.fill();
context.fillStyle = "darkblue";
context.beginPath();
context.arc(300, 200, 100, 0, 2 * 3.14);
context.fill();
}
function drawGermanyFlag() {
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
context.fillStyle = 'black';
context.fillRect(0, 0, 600, 133);
context.fillStyle = 'red';
context.fillRect(0, 133, 600, 133);
context.fillStyle = 'yellow';
context.fillRect(0, 266, 600, 133);
}
function swapFlag() {
switch (flagDefault) {
case drawBrazilFlag:
flagDefault = drawGermanyFlag;
break;
case drawGermanyFlag:
flagDefault = drawBrazilFlag;
break;
}
flagDefault();
}
flagDefault();
setInterval(swapFlag, 3000);
</script>
</body>
</html>