Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda