intersect.d.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Find or counts the intersections between two SVG paths.
  3. *
  4. * Returns a number in counting mode and a list of intersections otherwise.
  5. *
  6. * A single intersection entry contains the intersection coordinates (x, y)
  7. * as well as additional information regarding the intersecting segments
  8. * on each path (segment1, segment2) and the relative location of the
  9. * intersection on these segments (t1, t2).
  10. *
  11. * The path may be an SVG path string or a list of path components
  12. * such as `[ [ 'M', 0, 10 ], [ 'L', 20, 0 ] ]`.
  13. *
  14. * @example
  15. *
  16. * var intersections = findPathIntersections(
  17. * 'M0,0L100,100',
  18. * [ [ 'M', 0, 100 ], [ 'L', 100, 0 ] ]
  19. * );
  20. *
  21. * // intersections = [
  22. * // { x: 50, y: 50, segment1: 1, segment2: 1, t1: 0.5, t2: 0.5 }
  23. * // ];
  24. *
  25. * @param {String|Array<PathComponent>} path1
  26. * @param {String|Array<PathComponent>} path2
  27. * @param {Boolean} [justCount=false]
  28. *
  29. * @return {Array<Intersection>|Number}
  30. */
  31. declare function findPathIntersections(path1: Path, path2: Path, justCount: true): number;
  32. declare function findPathIntersections(path1: Path, path2: Path, justCount: false): Intersection[];
  33. declare function findPathIntersections(path1: Path, path2: Path): Intersection[];
  34. declare function findPathIntersections(path1: Path, path2: Path, justCount?: boolean): Intersection[] | number;
  35. export = findPathIntersections;
  36. /**
  37. * A string in the form of 'M150,150m0,-18a18,18,0,1,1,0,36a18,18,0,1,1,0,-36z'
  38. * or something like:
  39. * [
  40. * ['M', 1, 2],
  41. * ['m', 0, -2],
  42. * ['a', 1, 1, 0, 1, 1, 0, 2 * 1],
  43. * ['a', 1, 1, 0, 1, 1, 0, -2 * 1],
  44. * ['z']
  45. * ]
  46. */
  47. declare type Path = string | PathComponent[];
  48. declare type PathComponent = any[];
  49. declare interface Intersection {
  50. /**
  51. * Segment of first path.
  52. */
  53. segment1: number;
  54. /**
  55. * Segment of first path.
  56. */
  57. segment2: number;
  58. /**
  59. * The x coordinate.
  60. */
  61. x: number;
  62. /**
  63. * The y coordinate.
  64. */
  65. y: number;
  66. /**
  67. * Bezier curve for matching path segment 1.
  68. */
  69. bez1: number[];
  70. /**
  71. * Bezier curve for matching path segment 2.
  72. */
  73. bez2: number[];
  74. /**
  75. * Relative position of intersection on path segment1 (0.5 => in middle, 0.0 => at start, 1.0 => at end).
  76. */
  77. t1: number;
  78. /**
  79. * Relative position of intersection on path segment2 (0.5 => in middle, 0.0 => at start, 1.0 => at end).
  80. */
  81. t2: number;
  82. }