Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!