JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<svg height="210" width="500">
<circle id="svgPnt" cx="100" cy="100" r="5" fill="blue" />
<circle id="svgNrm" cx="100" cy="100" r="5" fill="black" />
<line id="svgLine" x1="200" y1="200" x2="300" y2="100" style="stroke:rgb(255,0,0);stroke-width:2" />
<circle id="svgLine1" cx="200" cy="200" r="20" fill="none" stroke="red" stroke-width="2" />
<circle id="svgLine2" cx="300" cy="100" r="20" fill="none" stroke="red" stroke-width="2" /> Sorry, your browser does not support inline SVG.
</svg>
<p id="pOnLineSegment"></p>
<p id="pInsideThickLine"></p>
<p id="pDistanceToLine"></p>
<p>The following sample shows the mouse point, the projected point on the line segment, and the thickness of the line (represented by the radius of the two line corner circles).</p>
<p>
The most useful functions to look at, are:
</p>
<ul>
<li>calcNearestPointOnLine(...): Returns a projected point perpendicular on the (infinite) line.</li>
<li>calcDistancePointToLine(...): Returns the shortest distance to the (infinite) line.</li>
<li>calcIsInsideLineSegment(...): Returns whether the projected point is actually inside the (finite) line segment.</li>
<li>calcIsInsideThickLineSegment(...): The most useful function. Returns true if the mouse point is actually inside the (finite) line, given a line thickness from the theoretical line away. It also assumes that the line end points are circular, not square.</li>
</ul>
<p>You should copy calcIsInsideThickLineSegment(...) for your own purposes, eg. To see if a mouse point has been clicked on a line segment.</p>
</body>
JavaScript
//Find your root SVG element
var svg = document.querySelector('svg');
// Create an SVGPoint for future math
var pt = svg.createSVGPoint();
// Get point in global SVG space
var m = null;
function cursorPoint(evt) {
if (m === null)
m = svg.getScreenCTM().inverse();
pt.x = evt.clientX;
pt.y = evt.clientY;
return pt.matrixTransform(m);
}
//Setup the points
var pM = {
x: null,
y: null
};
var pN = {
x: null,
y: null
};
var pL1 = {
x: 200,
y: 200
};
var pL2 = {
x: 300,
y: 100
};
svgM = document.getElementById("svgPnt");
svgN = document.getElementById("svgNrm");
svgL = document.getElementById("svgLine");
svgL1 = document.getElementById("svgLine1");
svgL2 = document.getElementById("svgLine2");
pOnLineSegment = document.getElementById("pOnLineSegment");
pInsideThickLine = document.getElementById("pInsideThickLine");
pDistanceToLine = document.getElementById("pDistanceToLine");
function updateSvg() {
if (pM.x && pM.y)
svgM.style.visibility = "visible";
else
svgM.style.visibility = "hidden";
//console.log("Mouse.visibility: "+svgM.style.visibility);
if (pN.x && pN.y)
svgN.style.visibility = "visible";
else
svgN.style.visibility = "hidden";
//console.log("Normal.visibility: "+svgN.style.visibility);
if (pM.x) svgM.setAttribute("cx", pM.x);
if (pM.y) svgM.setAttribute("cy", pM.y);
if (pN.x) svgN.setAttribute("cx", pN.x);
if (pN.y) svgN.setAttribute("cy", pN.y);
svgL.setAttribute("x1", pL1.x);
svgL.setAttribute("y1", pL1.y);
svgL.setAttribute("x2", pL2.x);
svgL.setAttribute("y2", pL2.y);
svgL1.setAttribute("cx", pL1.x);
svgL1.setAttribute("cy", pL1.y);
svgL2.setAttribute("cx", pL2.x);
svgL2.setAttribute("cy", pL2.y);
}
updateSvg();
//http://stackoverflow.com/a/6868610
//Tested and Working
function calcNearestPointOnLine(line1, line2, pnt) {
var L2 = (((line2.x - line1.x) * (line2.x - line1.x)) + ((line2.y - line1.y) * (line2.y - line1.y)));
if (L2 == 0) return false;
var r = (((pnt.x...