Skip to content

Subpath

Overview

A Canvas-style stateful (mutable) subpath, which tracks segments in addition to the points.

See http://www.w3.org/TR/2dcontext/#concept-path for the path / subpath Canvas concept.

@author Jonathan Olson <jonathan.olson@colorado.edu>

Class Subpath

import { Subpath } from 'scenerystack/kite';

Constructor

new Subpath( segments? : Segment[], points? : Vector2[], closed? : boolean )

Instance Methods

getBounds() : Bounds2

Returns the bounds of this subpath. It is the bounding-box union of the bounds of each segment contained.

getArcLength( distanceEpsilon? : number, curveEpsilon? : number, maxLevels? : number ) : number

Returns the (sometimes approximate) arc length of the subpath.

copy() : Subpath

Returns an immutable copy of this subpath

invalidatePoints()

Invalidates all segments (then ourself), since some points in segments may have been changed.

addPoint( point : Vector2 ) : this

Adds a point to this subpath

addSegment( segment : Segment ) : this

Adds a segment to this subpath

addClosingSegment()

Adds a line segment from the start to end (if non-zero length) and marks the subpath as closed. NOTE: normally you just want to mark the subpath as closed, and not generate the closing segment this way?

close()

Sets this subpath to be a closed path

getLength() : number

Returns the numbers of points in this subpath

TODO: This is a confusing name! It should be getNumPoints() or something https://github.com/phetsims/kite/issues/76

getFirstPoint() : Vector2

Returns the first point of this subpath

getLastPoint() : Vector2

Returns the last point of this subpath

getFirstSegment() : Segment

Returns the first segment of this subpath

getLastSegment() : Segment

Returns the last segment of this subpath

getFillSegments() : Segment[]

Returns segments that include the "filled" area, which may include an extra closing segment if necessary.

isDrawable() : boolean

Determines if this subpath is drawable, i.e. if it contains asny segments

isClosed() : boolean

Determines if this subpath is a closed path, i.e. if the flag is set to closed

hasClosingSegment() : boolean

Determines if this subpath is a closed path, i.e. if it has a closed segment

getClosingSegment() : Line

Returns a line that would close this subpath

getClosestPoints( point : Vector2 ) : ClosestToPointResult[]

Returns an array of potential closest points on the subpath to the given point.

writeToContext( context : CanvasRenderingContext2D )

Draws the segment to the 2D Canvas context, assuming the context's current location is already at the start point

toPiecewiseLinear( options : PiecewiseLinearOptions ) : Subpath

Converts this subpath to a new subpath made of many line segments (approximating the current subpath)

transformed( matrix : Matrix3 ) : Subpath

Returns a copy of this Subpath transformed with the given matrix.

nonlinearTransformed( options : PiecewiseLinearOptions ) : Subpath

Converts this subpath to a new subpath made of many line segments (approximating the current subpath) with the transformation applied.

getBoundsWithTransform( matrix : Matrix3 ) : Bounds2

Returns the bounds of this subpath when transform by a matrix.

offset( distance : number ) : Subpath

Returns a subpath that is offset from this subpath by a distance

TODO: Resolve the bug with the inside-line-join overlap. We have the intersection handling now (potentially) https://github.com/phetsims/kite/issues/76

stroked( lineStyles : LineStyles ) : Subpath[]

Returns an array of subpaths (one if open, two if closed) that represent a stroked copy of this subpath.

dashed( lineDash : number[], lineDashOffset : number, distanceEpsilon : number, curveEpsilon : number ) : Subpath[]

Returns a copy of this subpath with the dash "holes" removed (has many subpaths usually).

@param lineDash @param lineDashOffset @param distanceEpsilon - controls level of subdivision by attempting to ensure a maximum (squared) deviation from the curve @param curveEpsilon - controls level of subdivision by attempting to ensure a maximum curvature change between segments

serialize() : SerializedSubpath

Returns an object form that can be turned back into a segment with the corresponding deserialize method.

Instance Properties

segments : Segment[]

points : Vector2[]

closed : boolean

invalidatedEmitter : TinyEmitter

(readonly)

Static Methods

deserialize( obj : SerializedSubpath ) : Subpath

Returns a Subpath from the serialized representation.

Source Code

See the source for Subpath.ts in the kite repository.