2021-10-17 18:26:00 +02:00
|
|
|
---
|
2022-09-27 18:24:35 +02:00
|
|
|
title: Path.trim()
|
2021-10-17 18:26:00 +02:00
|
|
|
---
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-09-27 18:24:35 +02:00
|
|
|
The `Path.trim()` method Returns a new Path that is this path with overlapping
|
|
|
|
parts removed.
|
|
|
|
|
2021-08-25 16:09:31 +02:00
|
|
|
```js
|
|
|
|
Path path.trim()
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-27 18:24:35 +02:00
|
|
|
<Example caption="Example of the Path.trim() method">
|
|
|
|
```js
|
|
|
|
({ Point, points, Path, paths, part }) => {
|
|
|
|
|
|
|
|
points.center = new Point(0, 0)
|
|
|
|
points.base = new Point(0, 10)
|
|
|
|
points.tip = new Point(0, 50)
|
|
|
|
points.tipCpRight = new Point(30, 50)
|
|
|
|
points.tipCpLeft = new Point(-30, 50)
|
|
|
|
paths.example = new Path().move(points.base)
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
points["base" + i] = points.base.rotate(60 * i, points.center)
|
|
|
|
points["tip" + i] = points.tip.rotate(60 * i, points.center)
|
|
|
|
points["tipCpRight" + i] = points.tipCpRight.rotate(60 * i, points.center)
|
|
|
|
points["tipCpLeft" + i] = points.tipCpLeft.rotate(60 * i, points.center)
|
|
|
|
if (i < 2) {
|
|
|
|
paths.example
|
|
|
|
.line(points["base" + i])
|
|
|
|
.curve(points["base" + i], points["tipCpLeft" + i], points["tip" + i])
|
|
|
|
.curve(
|
|
|
|
points["tipCpRight" + i],
|
|
|
|
points["base" + i],
|
|
|
|
points["base" + i]
|
2022-09-29 17:50:53 +02:00
|
|
|
)
|
2022-09-27 18:24:35 +02:00
|
|
|
} else {
|
|
|
|
paths.example
|
|
|
|
.line(points["base" + i])
|
|
|
|
.line(points["tip" + i])
|
|
|
|
.line(points["tipCpRight" + i])
|
|
|
|
.line(points["base" + i])
|
|
|
|
}
|
|
|
|
}
|
2022-12-30 07:47:29 -08:00
|
|
|
|
2022-09-27 18:24:35 +02:00
|
|
|
paths.offset = paths.example
|
|
|
|
.offset(10)
|
|
|
|
.setClass("lining dotted stroke-sm")
|
2022-12-30 07:47:29 -08:00
|
|
|
|
2022-09-27 18:24:35 +02:00
|
|
|
paths.trimmed = paths.offset
|
|
|
|
.trim()
|
|
|
|
.setClass("various stroke-xl")
|
2022-09-29 17:50:53 +02:00
|
|
|
.attr("style", "stroke-opacity: 0.5")
|
2022-09-27 18:24:35 +02:00
|
|
|
|
|
|
|
return part
|
|
|
|
}
|
|
|
|
```
|
|
|
|
</Example>
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-09-27 18:24:35 +02:00
|
|
|
|
|
|
|
## Notes
|
|
|
|
|
|
|
|
This method is typically used when [Path.offset()](/reference/api/path/offset) caused some overlap.
|
|
|
|
However, use this sparsely or performance will suffer.
|
2021-08-25 16:09:31 +02:00
|
|
|
|
|
|
|
This method is recursive and complex, and the performance penalty for using
|
|
|
|
it on a long/complex path will be significant.
|
|
|
|
|
|
|
|
To limit the impact of path.trim(), follow this approach:
|
|
|
|
|
2022-02-20 14:44:38 +01:00
|
|
|
- construct a minimal path that contains the overlap
|
|
|
|
- trim it
|
|
|
|
- now join it to the rest of your path
|