I have designed a Duval's Triangle ( a diagnostic tool ), on SVG, cosisting of Segments (closed Paths) of different colors. The diagnostic result will be a coordinate. Need to detect the resultant coordinate is in which Closed Path ?
Like in the following case the diagnostic result is a RED DOT. Need to detect the close path i.e in this case : D2
You have several options:
SVG 2 has the
isPointInFill()
method. Which you can call on each shape to see if a point is within the fill of a path. However I believe only Chrome and Opera have implemented this new feature of SVG2.https://developer.mozilla.org/en-US/docs/Web/API/SVGGeometryElement/isPointInFill
You can draw your SVG to a Canvas and use the
isPointInPath()
method.https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/isPointInPath
You could draw the SVG to a Canvas and use the
getImageData()
method to get the colour of that particular pixel.https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData
And of course you can also do it numerically by calculating it yourself in some way.
This is using Paul LeBeau's answer.
1.
First a demo where you check if the point is in a certain path, the
#c
path in this case. Please read the comments in my code.2.
A second demo where I'm drawing the polygons on a canvas and using the method
isPointInPath()
of the context: Please read the comments in my code.3
This time using the
getImageData()
method to get the colour of the pixel at the point. The code is similar to the one in the previous example