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

267
Visualizações
How to paint a closed area which consists of separate lines on сanvas js?

I use canvas and draw a closed area and want the area to fill by some color. Here is how I do it. I draw closed area with lines

  1. I draw line (1)
  2. Next line (2) starts from end of the previous one
  3. Last line (3) starts from end of line (2) and ends at the start of line (1)

So as result I have closed area and want to fill by some color

Here is the chunk of code I used:

context.fillStyle = '#8ED6FF';
context.fill();

But it not works. Неre is the code I use to accomplish the task.

<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="978" height="900"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');

      // begin custom shape
      context.beginPath();
      // line (1)
      context.moveTo(10, 10);
      context.lineTo(190, 190);
      // line (2)
      context.moveTo(190, 190);
      context.lineTo(200, 280);
      // line (3)
      context.moveTo(200, 280);
      context.lineTo(10, 10);
      // here I try fill the area by color
      context.fillStyle = '#8ED6FF';
      context.fill();
      
      context.stroke();
    </script>
  </body>
</html>      

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

0

I found the solution.

<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="978" height="900"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');

      // begin custom shape
      context.beginPath();
      context.moveTo(70, 70);
      context.lineTo(290, 290);
      context.lineTo(300, 380);
      context.closePath()
      context.fillStyle = '#8ED6FF';
      context.fill();
      context.stroke();
    </script>
  </body>
</html>      

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