You need to scroll through the linear array for each line segment:
The basic principle is to add a line to the path, and then check if (x, y) is in that line:
ctx.beginPath(); ctx.moveTo(x1, y1); // start of line ctx.lineTo(x2, y2); // end of line // this will test the point against the line (lineWidth matters) if (ctx.isPointInStroke(x, y)) { // draw line segment in f.ex. different color here ctx.strokeStyle = "red"; ctx.stroke(); // we already have a line segment on the path }
There is no need to actually draw a line, just rearrange the path. Take if necessary.
Here is a complete example:
var ctx = canvas.getContext("2d"), lines = [],
<canvas id=canvas width=500 height=500></canvas>
To increase sensitivity, simply add a second (and third) line slightly offset from the first line. You can also use transformations: translate to the beginning (start point), find the angle, rotate, add a rectangle representing the sensitivity region, and check.
To perform the verification, internal compiled code is used (i.e. DirectX on Windows). This way you don't have to deal with math, etc. And get a quick result.
source share