Two segments: a fixed one (black) and one from a fixed point to the mouse (blue). Red marker appears when they actually cross.
See also: Reflect a line off another line — uses the same parametric intersection to find where a ray hits the mirror.
r = b - a
s = d - c
denom = r.x * s.y - r.y * s.x // 0 if parallel
t = ((c - a) cross s) / denom
u = ((c - a) cross r) / denom
// segments intersect iff 0 <= t <= 1 and 0 <= u <= 1
point = a + r * t
// Segment 1: (ax, ay) -> (bx, by)
// Segment 2: (cx, cy) -> (mouseX, mouseY)
const rx = bx - ax, ry = by - ay
const sx = mouseX - cx, sy = mouseY - cy
const denom = rx * sy - ry * sx
if (denom !== 0) {
const t = ((cx - ax) * sy - (cy - ay) * sx) / denom
const u = ((cx - ax) * ry - (cy - ay) * rx) / denom
if (t >= 0 && t <= 1 && u >= 0 && u <= 1) {
const ix = ax + rx * t
const iy = ay + ry * t
}
}