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

172
Vistas
Calculating the area of a Path2D consisting of many connecting lines

I am giving the user the ability to click from point to point however they want. My goal is to calculate the area of whatever they created. The problem is that it isn't always going to be a perfect square or triangle, etc.. It seems like all I have to work with is logging the changes in the coordinates each time they click and doing a manual calculation. Is there an easy way to get the filled in area of a Path2D() or is it very complicated?

enter image description here

Here is my current code:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// ctx.moveTo(0, 0);
// ctx.lineTo(200, 100);
// ctx.stroke();

const path = new Path2D();

c.addEventListener('mousedown', function(e) {
    const x = event.clientX - rect.left;
    const y = event.clientY - rect.top;


    ctx.strokeStyle = "black";
    ctx.lineWidth = 5;

    path.lineTo(x, y);
    ctx.stroke(path);
    ctx.fill(path);

    console.log(path);

    // console.log(x);
    // console.log(y);

    // ctx.lineTo(x, y);
    // ctx.stroke();
    // ctx.fillStyle = "red";
    // ctx.fill();
});
about 4 years ago · Juan Pablo Isaza
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