2021-10-17 18:26:00 +02:00
|
|
|
---
|
2022-09-27 18:24:35 +02:00
|
|
|
title: Path.split()
|
2021-10-17 18:26:00 +02:00
|
|
|
---
|
2022-02-19 08:04:25 +01:00
|
|
|
|
2022-09-27 18:24:35 +02:00
|
|
|
The `Path.split()` method splits a path in two halves, on a point along that
|
|
|
|
path that you pass it.
|
|
|
|
|
|
|
|
## Signature
|
|
|
|
|
2021-08-25 16:09:31 +02:00
|
|
|
```js
|
|
|
|
array path.split(Point splitPoint)
|
|
|
|
```
|
|
|
|
|
2022-09-27 18:24:35 +02:00
|
|
|
## Example
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-09-27 18:24:35 +02:00
|
|
|
<Example caption="Example of the Path.split() method">
|
2021-08-25 16:09:31 +02:00
|
|
|
```js
|
2022-09-27 18:24:35 +02:00
|
|
|
({ Point, points, Path, paths, snippets, Snippet, part }) => {
|
|
|
|
|
2022-09-29 17:50:53 +02:00
|
|
|
points.A = new Point(45, 60)
|
|
|
|
points.B = new Point(10, 30)
|
|
|
|
points.BCp2 = new Point(40, 20)
|
|
|
|
points.C = new Point(90, 30)
|
|
|
|
points.CCp1 = new Point(50, -30)
|
|
|
|
points.D = new Point(50, 130)
|
|
|
|
points.DCp1 = new Point(150, 30)
|
2022-12-30 07:47:29 -08:00
|
|
|
|
2022-09-27 18:24:35 +02:00
|
|
|
paths.demo = new Path()
|
|
|
|
.move(points.D)
|
|
|
|
.curve(points.DCp1, points.DCp1, points.C)
|
|
|
|
.curve(points.CCp1, points.BCp2, points.B)
|
2022-09-29 17:50:53 +02:00
|
|
|
.line(points.A)
|
2022-12-30 07:47:29 -08:00
|
|
|
|
2022-09-29 17:50:53 +02:00
|
|
|
points.split = paths.demo.shiftFractionAlong(0.75)
|
|
|
|
snippets.split = new Snippet("notch", points.split)
|
2022-12-30 07:47:29 -08:00
|
|
|
|
2022-09-29 17:50:53 +02:00
|
|
|
let halves = paths.demo.split(points.split)
|
2022-09-27 18:24:35 +02:00
|
|
|
for (let i in halves) {
|
|
|
|
paths[i] = halves[i]
|
2022-09-29 17:50:53 +02:00
|
|
|
.attr("style", "stroke-width: 3; stroke-opacity: 0.5;")
|
|
|
|
.attr("style", `stroke: hsl(${i * 70}, 100%, 50%)`)
|
2022-09-27 18:24:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return part
|
2021-08-25 16:09:31 +02:00
|
|
|
}
|
|
|
|
```
|
2022-09-27 18:24:35 +02:00
|
|
|
</Example>
|
2024-06-12 13:24:42 +02:00
|
|
|
|
|
|
|
## Notes
|
|
|
|
|
|
|
|
### The returned array will hold null for edge cases
|
|
|
|
|
|
|
|
Typically, the returned array will hold a `Path` object for each half.
|
|
|
|
But in some cases, one of the array entries can hold `null` if the split failed to find a path.
|
|
|
|
For example because you are splitting a `Path` on its start or end point.
|
|
|
|
|
|
|
|
```mjs
|
|
|
|
// Return value for a normal case
|
|
|
|
[Path, Path]
|
|
|
|
// Return value when calling Path.split() on/near the path's start point
|
|
|
|
[null, Path]
|
|
|
|
// Return value when calling Path.split() on/near the path's end point
|
|
|
|
[Path, null]
|
|
|
|
```
|
|
|
|
|
|
|
|
### This method will snap the split point to start or end points
|
|
|
|
This method will also _snap_ to the start or end point if you are splitting a path
|
|
|
|
(very) close to it, as it checks with [`Point.sitsRoughlyOn()`](/reference/api/point/sitsroughlyon).
|
|
|
|
|