1

I have draw several polygons in a canvas. I am now interested in how to find the coordinates (vertices) of the selected polygon. I can find the selected polygon (region) using mouse coordinates, but I need to redraw it if it is selected, which is why I need its coordinates to do that.

A similar answer that I found so far is this one https://help.geogebra.org/topic/-solved-how-do-i-get-a-list-of-coordinates-from-a-polygon- using GeoGebra, but I am using HTML5 and JavaScript!

Hannington Mambo
  • 998
  • 2
  • 13
  • 28
  • https://stackoverflow.com/help/mcve – mpm Sep 04 '18 at 09:58
  • @mpm I guess you are downgrading my question because I am not providing a reproduceable example: but there is none! It is easy to find isPointInPath, but how do you use that info to scale the polygon selected, without throwing it off the canvas...? This was my thought. – Hannington Mambo Sep 04 '18 at 10:41

2 Answers2

2

Once you draw something on a canvas, you can not get the coordinates of the object you drew unless you have some image processing algorithm.

Instead, you need need to keep the polygon object you drew on the canvas in memory and you need to define it's boundaries on the canvas.

This way, when the user clicks on the canvas, you simply get the mouse coordinates on the canvas and you calculate which polygon was clicked by checking where you drew your polygon and checking if the mouse clicks enters the boundaries of this polygon.

kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
  • Thanks, @rottenoats. I understand the part of getting isPointInPath very well, but I need to redraw the selected polygon to scale without throwing it off the canvans. I guessed if I have the coordinates I would then compare to the closest distance to the edge of the canvas to redraw it... – Hannington Mambo Sep 04 '18 at 10:44
1

Sounds like you are trying to solve a couple different problems:

1) Figure out which polygon the user clicked. As was mentioned, if you are keeping an array of your polygons in memory, and can implement an isPointInPath method, then you can loop through your in-memory polygons and check if any of them were clicked. Don't forget that to get an accurate check, you may need to take your given "mouse x" and "mouse y" coordinates (from a click event, for example) and subtract the canvas's page X and Y coordinate.

(See the SO answer How do I get the coordinates of a mouse click on a canvas element? for more detail.)

2) Once you've determined the user has clicked polygon P, you want to redraw it. You should be able to redraw it no problem, by simply redrawing the same polygon using the same coordinates as before (for example, you could change your strokeStyle or fillStyle to a different color to represent the selection).

If you want to make the polygon larger and in the same position, that's a different question - basically, you need to translate the canvas to the center of the polygon, then scale the canvas, then draw your polygon. If this is new to you, there's a tutorial available at http://codetheory.in/canvas-rotating-and-scaling-images-around-a-particular-point/ (uses images instead of polygons, but same concept).

Note that if your polygon is complex, even determining the right point to scale around may be difficult. The question "what is the center of a complex polygon" has several different answers; a simple answer is to average all of the polygon's point's X and Y values (Center X = avg(X), Center Y = avg(Y)). If that doesn't look right, there are more complicated approaches available in another SO question, What is the fastest way to find the "visual" center of an irregularly shaped polygon? .

Hope this points you in the right direction!

Elliot Nelson
  • 11,371
  • 3
  • 30
  • 44
  • Thanks a lot @Elliot! You are spot on in breaking down my requirement. I figured out the first part already. The second part is what has been challenging, but your references are excellent. I will thus accept this as the answer. Just a note on this: I had solved this before using Adobe Flash, which is a lot easier that the complexities I am seeing in doing it in HTML5, I will for now revert back to my Flash project for the sake of time. I guess it is easier to do it with Flash because the polygons are already images, and not a bunch of coordinates. – Hannington Mambo Sep 05 '18 at 02:30