* Calculate min distance corresponding point. * This method won't evaluate if point is in the path.
(pt: Point, path: PathProxy, out: Point)
| 211 | * This method won't evaluate if point is in the path. |
| 212 | */ |
| 213 | function nearestPointOnPath(pt: Point, path: PathProxy, out: Point) { |
| 214 | let xi = 0; |
| 215 | let yi = 0; |
| 216 | let x0 = 0; |
| 217 | let y0 = 0; |
| 218 | let x1; |
| 219 | let y1; |
| 220 | |
| 221 | let minDist = Infinity; |
| 222 | |
| 223 | const data = path.data; |
| 224 | const x = pt.x; |
| 225 | const y = pt.y; |
| 226 | |
| 227 | for (let i = 0; i < data.length;) { |
| 228 | const cmd = data[i++]; |
| 229 | |
| 230 | if (i === 1) { |
| 231 | xi = data[i]; |
| 232 | yi = data[i + 1]; |
| 233 | x0 = xi; |
| 234 | y0 = yi; |
| 235 | } |
| 236 | |
| 237 | let d = minDist; |
| 238 | |
| 239 | switch (cmd) { |
| 240 | case CMD.M: |
| 241 | // moveTo 命令重新创建一个新的 subpath, 并且更新新的起点 |
| 242 | // 在 closePath 的时候使用 |
| 243 | x0 = data[i++]; |
| 244 | y0 = data[i++]; |
| 245 | xi = x0; |
| 246 | yi = y0; |
| 247 | break; |
| 248 | case CMD.L: |
| 249 | d = projectPointToLine(xi, yi, data[i], data[i + 1], x, y, tmpPt, true); |
| 250 | xi = data[i++]; |
| 251 | yi = data[i++]; |
| 252 | break; |
| 253 | case CMD.C: |
| 254 | d = cubicProjectPoint( |
| 255 | xi, yi, |
| 256 | data[i++], data[i++], data[i++], data[i++], data[i], data[i + 1], |
| 257 | x, y, tmpPt |
| 258 | ); |
| 259 | |
| 260 | xi = data[i++]; |
| 261 | yi = data[i++]; |
| 262 | break; |
| 263 | case CMD.Q: |
| 264 | d = quadraticProjectPoint( |
| 265 | xi, yi, |
| 266 | data[i++], data[i++], data[i], data[i + 1], |
| 267 | x, y, tmpPt |
| 268 | ); |
| 269 | xi = data[i++]; |
| 270 | yi = data[i++]; |
no test coverage detected
searching dependent graphs…