# Trimming a stroke

[Canonical page](https://nothing.graphics/docs/format/spec/contour-trim) · [Page source on GitHub](https://github.com/gridaco/nothing/blob/main/ncf/spec/contour-trim.md)

Reveal a stroke over an interval of the original contour’s length.



A stroke may own one `trim` with explicit `start` and `end` attributes.
Both are independently animatable, finite positions measured in lengths
of the shape's sampled path in local coordinates. The stroke paints
the selected part using its own width, caps,
joins, alignment, and paint stack. A stroke with no trim follows the whole
contour. All paints in the stroke share the trim.

## Full casing, growing stripe [#full-casing-growing-stripe]

```xml
<scene frame-rate="24" width="128" height="96">
  <shape translate-x="16" translate-y="16">
    <polyline points="0,0 60,0 60,40"/>
    <stroke stroke-width="12">
      <paint><solid color="#27272AFF"/></paint>
    </stroke>
    <stroke stroke-width="8" stroke-linecap="butt">
      <trim start="0" end="0">
        <animate attributeName="end" fill="freeze">
          <key frame="0" value="0" ease="linear"/>
          <key frame="24" value="1"/>
        </animate>
      </trim>
      <paint><solid color="#F4F4F5FF"/></paint>
    </stroke>
  </shape>
</scene>
```

The contour is 100 local units long. At frame 12 the stripe ends at
(50, 0) in shape coordinates. At frame 18 it turns the corner and ends
at (60, 15). The casing reaches (60, 40) throughout. The shape's
translation adds (16, 16) to each of those positions in the scene.
If the artist extends the final leg to (60, 60), the new total is 120.
At frame 18 the same key now places the stripe end at (60, 30).
The edit changes one point pair in the geometry.

## Directed contour and distance [#directed-contour-and-distance]

Measure along the contour in the direction in the table. For total length
`L`, the selected interval runs from `start × L` to `end × L`. Measure curves
by arc length. In particular, halfway along a Bézier can have a curve
parameter other than 0.5. Recompute from sampled geometry when its dimensions
or control points change. Apply the owner's spatial transform after stroking.

| Geometry            | Start and direction                                                                                                                                                                                             |
| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `line`, `polyline`  | Start at the first endpoint or vertex. Follow the authored order.                                                                                                                                               |
| `polygon`           | Follow the authored vertices and include the return segment to the first vertex.                                                                                                                                |
| `rect`              | Start at (left + used top-left radius, top) of the [sampled box](https://nothing.graphics/docs/format/spec/boxed-geometry), traverse the top edge to the right, and continue clockwise through the corner arcs. |
| `circle`, `ellipse` | Start at the rightmost point and travel clockwise in the local x-right, y-down coordinate system.                                                                                                               |
| `path`              | Follow the authored subpaths in command order. Include each closing edge in the drawn length.                                                                                                                   |

### Paths with several subpaths [#paths-with-several-subpaths]

One stroke owns one interval across the total drawn length of its path. Visit each subpath in authored order. A move starts a new contour and consumes zero distance. A close contributes the distance back to that contour’s start. A drawing command immediately after a close begins another contour at that same point.

For a compound path, both trim positions must lie in \[0, 1], including when every contour closes. Intersect the selected interval with each contour’s distance range. Emit each positive-length overlap as a separate stroked piece. At a pen lift, retain both exposed caps and draw no bridge. An endpoint exactly at the lift gives the next contour zero coverage, even with round caps. A fully selected closed contour keeps its closing join. A full \[0, 1] interval paints the ordinary stroke. In a proper subinterval, zero-length contours contribute neither distance nor ink.

For example, if a signature’s name measures 60 units and its underline measures 40, `<trim start="0" end="0.7"/>` draws the whole name and 10 units of the underline. Moving the underline farther away preserves the reveal timing. To animate the two marks independently, author separate shapes and put a trim on each stroke. [Pen-lift and closed-loop fixture](https://github.com/gridaco/nothing/blob/main/ncf/conformance/compound-trim.nothing)

Diagnose trim on text and boolean geometry, whose generated contours have no authored traversal order. Stroke trimming leaves the geometry used by fills, masks, boolean operands, and sibling strokes intact. [Shape and stroke contract](https://nothing.graphics/docs/format/spec/graphics#paint)

## Coverage [#coverage]

* On an open contour both positions must be in \[0, 1]. Diagnose an
  out-of-range base or key at its authored location. The full interval
  from 0 to 1 paints the ordinary open stroke, including its endpoint caps.
* On a single closed contour both positions are unwrapped. Integer values mark
  successive visits to the seam, with one unit per lap. Negative positions
  count back from the authored origin. Sample both tracks in these coordinates
  before wrapping. A path that returns to its start with a line or curve
  follows the open rule unless it closes with `Z`.
* If `end <= start`, the stroke has zero coverage, including when
  `start=0.8` and `end=0.2` on a closed contour. On a closed contour a
  span of one lap or more paints the full closed stroke exactly once,
  preserving its seam join and paint opacity.
* For a closed span strictly between zero and one lap, begin at the
  fractional part of `start` (its value minus its floor) and travel
  forward by `end - start` laps. Crossing the seam continues the same
  stroke through the original join. Caps occur only at the two trim ends.
* For a proper subinterval of positive length, extract the curve segment
  and apply the stroke's caps to its two ends. Interior vertices keep their
  joins. When a cut coincides with a corner, the endpoint cap follows the
  tangent on the retained side. Zero-length pieces contribute no distance.
  A partial interval on a contour of total length zero gives empty coverage.
* Centered strokes use the width centered on the selected curve. For
  inside or outside alignment on a simple closed source, construct the
  centered band of twice the stroke width around the selected segment,
  using the stroke's caps and joins. Intersect that band with the original
  filled region for inside alignment, or with its exterior for outside
  alignment. An open source contour follows the usual center-alignment rule.
* Compute paint coordinates and image fitting from the same shape geometry
  used by the full stroke. Trimming changes coverage. A gradient highlight
  keeps its position as the stripe passes through it.

For example, `<trim start="0.875" end="1.125"/>` paints the last eighth
and first eighth of a closed rail as one band. Add 0.25 to both coordinates
to move the band a quarter lap. To retime just its head crossing the seam,
move the frame of its `end` key with value 1. The `start` track still
controls the tail independently.

Rive calls the per-stroke operation Trim. GSAP’s `sliceRawPath` uses
unwrapped positions for a path interval, allowing either end to cross
the origin continuously. NCF applies that convention to stroke coverage.
[Rive Trim](https://rive.app/docs/editor/manipulating-shapes/trim-path#trim) · [GSAP path slicing](https://gsap.com/docs/v3/Plugins/MotionPathPlugin/static.sliceRawPath%28%29/)

Lottie Trim Path defines selection by fractional distance and includes
rules for applying one operation to multiple paths. Skia's path measurement
API extracts a segment along one contour. SVG defines the direction and
equivalent path for basic shapes, the length of a path, and the effect of
caps on stroke ends. A stroke-local owner in NCF lets the artist key one
stripe while the other stroke still follows the complete road.
[Lottie shape modifiers](https://lottie.github.io/lottie-spec/latest/specs/shapes/) · [Skia path measurement](https://api.skia.org/classSkPathMeasure.html) · [SVG basic shapes](https://www.w3.org/TR/SVG2/shapes.html) · [SVG path distance](https://www.w3.org/TR/SVG2/paths.html#DistanceAlongAPath) · [SVG stroke caps](https://www.w3.org/TR/SVG2/painting.html#LineCaps)

## Checks for implementation and review [#checks-for-implementation-and-review]

| Change or sample                                                                                                                                  | Expected result                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| Sample the road at frames 18, 0, and 18 again.                                                                                                    | Both frame-18 samples end at local (60, 15). At frame 0 the stripe contributes no coverage, including at its start.                             |
| Change the stripe's final key value to `0.8`.                                                                                                     | At frame 12 it ends at local (40, 0). Only that key value changes in source.                                                                    |
| Use `M 0 0 C 0 0 0 0 80 0` as an open path with a centered stroke and trim from 0 to 0.5.                                                         | The segment ends at (40, 0). Its curve parameter is the cube root of 0.5.                                                                       |
| On that cubic, set stroke width to 8. Sample (42, 0) with butt caps, then round caps.                                                             | The butt cap ends at x = 40 and leaves the sample clear. The round cap extends to x = 44 and covers the sample.                                 |
| On a 100 × 100 rectangle at the origin, use a centered miter stroke of width 6 with butt caps. Set the trim end to 1, then 0.25, with start at 0. | The seam join covers (−1, −1) at full length. The partial stroke starts with a butt cap and leaves that point clear.                            |
| On the same rectangle, use start 0.875 and end 1.125. Add one lap to both positions, then try subtracting one lap from both instead.              | Each sample follows the left edge from (0, 50) to (0, 0) and the top edge to (50, 0). The join covers (−1, −1) and only the two tips have caps. |
| On a 100 × 100 rectangle at the origin, trim an inside stroke of width 6 from 0 to 0.25, with butt caps.                                          | The top edge is selected. Point (50, 5) is covered and (50, −2) is clear. Changing only the alignment to outside reverses those samples.        |
| Put a second paint on the stripe and move the penultimate vertex of the polyline.                                                                 | Both paints follow one trimmed band. The dark casing follows the same edited contour over its full length.                                      |
