code: function (x, y, x0, y0, x1, y1, o) {
                  if (o && !(o =
                      function (x, y, x0, y0, x1, y1) {
                          if (typeof x0 !== 'undefined') return { x: x0, y: y };
                          else if (typeof y0 !== 'undefined') return { x: x, y: y0 };
  
                          var left,
                              tg = -1 / ((y1 - y0) / (x1 - x0));
  
                          return {
                              x: left = (x1 * (x * tg - y + y0) + x0 * (x * -tg + y - y1)) / (tg * (x1 - x0) + y0 - y1),
                              y: tg * left - tg * x + y
                          };
                      } (x, y, x0, y0, x1, y1),
                      o.x >= Math.min(x0, x1) && o.x <= Math.max(x0, x1) && o.y >= Math.min(y0, y1) && o.y <= Math.max(y0, y1))
                  ) {
                      var l1 = lineDistance(x, y, x0, y0), l2 = lineDistance(x, y, x1, y1);
                      return l1 > l2 ? l2 : l1;
                  } else {
                      var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
                      return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
                  }
              }
docs: Here is some voodoo magic for determining the distance to a line form a given point {x, y}.