Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
why does my arc doesn't grow every frame?

I am trying to make an animation of a sunrise, so for that i need a circle that grows in size. At the moment i'm using an arc, but my sun isnt growing every frame so it just stays static. I've tried calling another function but I'm not sure how to do that or if the code I've already made is missing something. This is the code I've written so far.

<!doctype html>
<html>

<head>
  <title>Animacion</title>
</head>

<body onload="call()">
  <!--canvas-->
  <canvas id="canvas" width="300" height="300" style="border: 1px solid black"></canvas>
  <script>
    function call() {
      window.requestAnimationFrame(draw);
    }

    function draw() {
      //context
      var canvas = document.getElementById('canvas');
      var ctx = canvas.getContext('2d');
      ctx.globalCompositeOperation = 'destination-over';
      ctx.clearRect(0, 0, 300, 300);
      ctx.fillStyle = "green";
      ctx.strokeStyle = "green";
      ctx.save();

      //ground
      ctx.beginPath();
      ctx.rect(0, 200, 300, 100);
      ctx.fill();
      ctx.stroke();
      ctx.closePath();
      ctx.save();

      //sun size    
      var sunSize = 50;
      //sun
      ctx.beginPath();
      ctx.strokeStyle = "yellow";
      ctx.fillStyle = "yellow";
      ctx.arc(145, 220, sunSize, sunSize, 0, Math.PI * 2, true);
      ctx.fill();
      ctx.stroke();
      sunSize = sunSize + 5;
      ctx.fill();
      ctx.stroke();
      ctx.closePath();
      ctx.save();
      //sky
      ctx.save();
      ctx.beginPath();
      ctx.strokeStyle = "cyan";
      ctx.fillStyle = "cyan";
      ctx.rect(0, 0, 300, 300);
      ctx.fill();
      ctx.stroke();
      ctx.closePath();
      ctx.save();


      window.requestAnimationFrame(draw);
    }
    call();
  </script>
</body>

</html>

I've tried using another function inside of the draw() but it basically does nothing and erases the sky i have.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you initalize sunSize inside your draw() function so it resets the sunSize to 50 every time it runs. move the sunSize outside the draw function below the <script> part here;

<script>
      var sunSize = 50;
        function call(){
            window.requestAnimationFrame(draw);
        }
etc, etc

also, instad of using sunSize = sunSize + 5 use sunSize += 5. they mean the exact same thing just written faster. 5 may be a bit fast though. try using 0.1 or 0.2

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda