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

225
Vistas
Does ctx.stroke() and ctx.fill() automatically close path in canvas?

Is it necessary to include ctx.closePath() if I am going to use ctx.fill() or ctx.stroke() when drawing paths in canvas?
Here is an example below:

ctx.beginPath()
// point 1
ctx.moveTo(x+0.5,y+0.5)
// point2
ctx.lineTo(x2+0.5, y2+0.5)
// point 3
ctx.lineTo(x3+0.5, y3+0.5)

ctx.closePath() // <--- IS THIS NECESSARY ?
ctx.stroke()
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The function does what it says, it closes the path, if you want a closed triangle in your example then the closepath() is required, if you want two lines then it's not.

So the answer to your question is: No, stroke() and fill() does not close your path.

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

let x = 10;
let y = 10;

ctx.beginPath()
// point 1
ctx.moveTo(x/2+0.5,y/2+0.5)
// point2
ctx.lineTo(x*2+0.5, y*4+0.5)
// point 3
ctx.lineTo(x*3+0.5, y*2+0.5)

ctx.closePath() // <--- IS THIS NECESSARY ?
ctx.stroke()

// New path

x = 50;
y = 20;

ctx.beginPath()
// point 1
ctx.moveTo(x+0.5,y+0.5)
// point2
ctx.lineTo(x*2+0.5, y*4+0.5)
// point 3
ctx.lineTo(x*3+0.5, y*2+0.5)

ctx.stroke()
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML canvas tag.</canvas>

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