Path
The path item represents a path in a Paper.js project.
Constructors
Path([segments])
Creates a new path item and places it at the top of the active layer.
Parameters:
segments:
Array ofSegment
objects — An array of segments (or points to be converted to segments) that will be added to the path — optionalReturns:
Path
— the newly created path
Example:Create an empty path and add segments to it:
Example:Create a path with two segments:
Path(object)
Creates a new path item from an object description and places it at the top of the active layer.
Parameters:
object:
Object
— an object containing properties to be set on the pathReturns:
Path
— the newly created path
Example:
Example:
Path(pathData)
Creates a new path item from SVG path-data and places it at the top of the active layer.
Parameters:
pathData:
String
— the SVG path-data that describes the geometry of this pathReturns:
Path
— the newly created path
Example:
Shaped Paths
Path.Line(from, to)
Creates a linear path item from two points describing a line.
Parameters:
from:
Point
— the line’s starting pointto:
Point
— the line’s ending pointReturns:
Path
— the newly created path
Example:
Path.Line(object)
Creates a linear path item from the properties described by an object literal.
Parameters:
object:
Object
— an object containing properties describing the path’s attributesReturns:
Path
— the newly created path
Example:
Path.Circle(center, radius)
Creates a circular path item.
Parameters:
center:
Point
— the center point of the circleradius:
Number
— the radius of the circleReturns:
Path
— the newly created path
Example:
Path.Circle(object)
Creates a circular path item from the properties described by an object literal.
Parameters:
object:
Object
— an object containing properties describing the path’s attributesReturns:
Path
— the newly created path
Example:
Path.Rectangle(rectangle[, radius])
Creates a rectangular path item, with optionally rounded corners.
Parameters:
rectangle:
Rectangle
— the rectangle object describing the geometry of the rectangular path to be createdradius:
Size
— the size of the rounded corners — optional, default:null
Returns:
Path
— the newly created path
Example:
Example:The same, with rounder corners
Path.Rectangle(point, size)
Creates a rectangular path item from a point and a size object.
Parameters:
point:
Point
— the rectangle’s top-left corner.size:
Size
— the rectangle’s size.Returns:
Path
— the newly created path
Example:
Path.Rectangle(from, to)
Creates a rectangular path item from the passed points. These do not necessarily need to be the top left and bottom right corners, the constructor figures out how to fit a rectangle between them.
Parameters:
from:
Point
— the first point defining the rectangleto:
Point
— the second point defining the rectangleReturns:
Path
— the newly created path
Example:
Path.Rectangle(object)
Creates a rectangular path item from the properties described by an object literal.
Parameters:
object:
Object
— an object containing properties describing the path’s attributesReturns:
Path
— the newly created path
Example:
Example:
Example:
Example:
Path.Ellipse(rectangle)
Creates an elliptical path item.
Parameters:
rectangle:
Rectangle
— the rectangle circumscribing the ellipseReturns:
Path
— the newly created path
Example:
Path.Ellipse(object)
Creates an elliptical path item from the properties described by an object literal.
Parameters:
object:
Object
— an object containing properties describing the path’s attributesReturns:
Path
— the newly created path
Example:
Example:Placing by center and radius
Path.Arc(from, through, to)
Creates a circular arc path item.
Parameters:
from:
Point
— the starting point of the circular arcthrough:
Point
— the point the arc passes throughto:
Point
— the end point of the arcReturns:
Path
— the newly created path
Example:
Path.Arc(object)
Creates an circular arc path item from the properties described by an object literal.
Parameters:
object:
Object
— an object containing properties describing the path’s attributesReturns:
Path
— the newly created path
Example:
Path.RegularPolygon(center, sides, radius)
Creates a regular polygon shaped path item.
Parameters:
center:
Point
— the center point of the polygonsides:
Number
— the number of sides of the polygonradius:
Number
— the radius of the polygonReturns:
Path
— the newly created path
Example:
Path.RegularPolygon(object)
Creates a regular polygon shaped path item from the properties described by an object literal.
Parameters:
object:
Object
— an object containing properties describing the path’s attributesReturns:
Path
— the newly created path
Example:
Path.Star(center, points, radius1, radius2)
Creates a star shaped path item.
The largest of
radius1
andradius2
will be the outer radius of the star. The smallest of radius1 and radius2 will be the inner radius.Parameters:
center:
Point
— the center point of the starpoints:
Number
— the number of points of the starradius1:
Number
radius2:
Number
Returns:
Path
— the newly created path
Example:
Path.Star(object)
Creates a star shaped path item from the properties described by an object literal.
Parameters:
object:
Object
— an object containing properties describing the path’s attributesReturns:
Path
— the newly created path
Example:
Properties
segments
The segments contained within the path.
Type:
Array of
Segment
objects
firstSegment
The first Segment contained within the path.
Read only.
Type:
Segment
lastSegment
The last Segment contained within the path.
Read only.
Type:
Segment
curves
The curves contained within the path.
Read only.
Type:
Array of
Curve
objects
firstCurve
The first Curve contained within the path.
Read only.
Type:
Curve
lastCurve
The last Curve contained within the path.
Read only.
Type:
Curve
closed
Specifies whether the path is closed. If it is closed, Paper.js connects the first and last segments.
Type:
Boolean
Example:
length
The approximate length of the path.
Read only.
Type:
Number
area
The area that the path’s geometry is covering. Self-intersecting paths can contain sub-areas that cancel each other out.
Read only.
Type:
Number
fullySelected
Specifies whether the path and all its segments are selected. Cannot be
true
on an empty path.Type:
Boolean
Example:A path is fully selected, if all of its segments are selected:
Methods
add(...segment)
Adds one or more segments to the end of the
segments
array of this path.Parameters:
segment:
Segment
⟋Point
⟋Array ofNumbers
— the segment or point to be added.Returns:
Segment
⟋Array ofSegment
objects — the added segment(s). This is not necessarily the same object, e.g. if the segment to be added already belongs to another path.
Example:Adding segments to a path using point objects:
Example:Adding segments to a path using arrays containing number pairs:
Example:Adding segments to a path using objects:
Example:Adding a segment with handles to a path:
insert(index, segment)
Inserts one or more segments at a given index in the list of this path’s segments.
Parameters:
index:
Number
— the index at which to insert the segmentsegment:
Segment
⟋Point
— the segment or point to be inserted.Returns:
Segment
— the added segment. This is not necessarily the same object, e.g. if the segment to be added already belongs to another path
Example:Inserting a segment:
Example:Inserting multiple segments:
addSegments(segments)
Adds an array of segments (or types that can be converted to segments) to the end of the
segments
array.Parameters:
segments:
Array ofSegment
objectsReturns:
Array of Segment
objects — an array of the added segments. These segments are not necessarily the same objects, e.g. if the segment to be added already belongs to another path
Example:Adding an array of Point objects:
Example:Adding an array of [x, y] arrays:
Example:Adding segments from one path to another:
insertSegments(index, segments)
Inserts an array of segments at a given index in the path’s
segments
array.Parameters:
index:
Number
— the index at which to insert the segmentssegments:
Array ofSegment
objects — the segments to be insertedReturns:
Array of Segment
objects — an array of the added segments. These segments are not necessarily the same objects, e.g. if the segment to be added already belongs to another path
removeSegment(index)
Removes the segment at the specified index of the path’s
segments
array.Parameters:
index:
Number
— the index of the segment to be removedReturns:
Segment
— the removed segment
Example:Removing a segment from a path:
removeSegments()
Removes all segments from the path’s
segments
array.Returns:
Array of Segment
objects — an array containing the removed segments
removeSegments(from[, to])
Removes the segments from the specified
from
index to theto
index from the path’ssegments
array.Parameters:
from:
Number
— the beginning index, inclusiveto:
Number
— the ending index, exclusive — optional, default:segments.length
Returns:
Array of Segment
objects — an array containing the removed segments
Example:Removing segments from a path:
hasHandles()
Checks if any of the curves in the path have curve handles set.
Returns:
Boolean
—true
if the path has curve handles set,false
otherwiseSee also:
segment.hasHandles()
curve.hasHandles()
clearHandles()
Clears the path’s handles by setting their coordinates to zero, turning the path into a polygon (or a polyline if it isn’t closed).
divideAt(location)
Divides the path on the curve at the given offset or location into two curves, by inserting a new segment at the given location.
Parameters:
location:
Number
⟋CurveLocation
— the offset or location on the path at which to divide the existing curve by inserting a new segmentReturns:
Segment
— the newly inserted segment if the location is valid,null
otherwiseSee also:
curve.divideAt(location)
splitAt(location)
Splits the path at the given offset or location. After splitting, the path will be open. If the path was open already, splitting will result in two paths.
Parameters:
location:
Number
⟋CurveLocation
— the offset or location at which to split the pathReturns:
Path
— the newly created path after splitting, if any
Example:
Example:Splitting an open path Draw a V shaped path:
Example:Splitting a closed path
join(path[, tolerance])
Joins the path with the other specified path, which will be removed in the process. They can be joined if the first or last segments of either path lie in the same location. Locations are optionally compare with a provide
tolerance
value.If
null
orthis
is passed as the other path, the path will be joined with itself if the first and last segment are in the same location.Parameters:
path:
Path
— the path to join this path with;null
orthis
to join the path with itselftolerance:
Number
— the tolerance with which to decide if two segments are to be considered the same location when joining — optional, default:0
Example:Joining two paths:
Example:Joining two paths that share a point at the start or end of their segments array:
Example:Joining two paths that connect at two points:
reduce(options)
Reduces the path by removing curves that have a length of 0, and unnecessary segments between two collinear flat curves.
Parameters:
options:
Returns:
Path
— the reduced path
toShape([insert])
Attempts to create a new shape item with same geometry as this path item, and inherits all settings from it, similar to
item.clone
().Parameters:
insert:
Boolean
— specifies whether the new shape should be inserted into the scene graph. When set totrue
, it is inserted above the path item — optional, default:true
Returns:
Shape
— the newly created shape item with the same geometry as this path item if it can be matched,null
otherwiseSee also:
shape.toPath(insert)
Positions on Paths and Curves
getLocationOf(point)
Returns the curve location of the specified point if it lies on the path,
null
otherwise.Parameters:
point:
Point
— the point on the pathReturns:
CurveLocation
— the curve location of the specified point
getOffsetOf(point)
Returns the length of the path from its beginning up to up to the specified point if it lies on the path,
null
otherwise.Parameters:
point:
Point
— the point on the pathReturns:
Number
— the length of the path up to the specified point
getLocationAt(offset)
Returns the curve location of the specified offset on the path.
Parameters:
offset:
Number
— the offset on the path, where0
is at the beginning of the path andpath.length
at the endReturns:
CurveLocation
— the curve location at the specified offset
getPointAt(offset)
Calculates the point on the path at the given offset.
Parameters:
offset:
Number
— the offset on the path, where0
is at the beginning of the path andpath.length
at the endReturns:
Point
— the point at the given offset
Example:Finding the point on a path at a given offset:
Example:Iterating over the length of a path:
getTangentAt(offset)
Calculates the normalized tangent vector of the path at the given offset.
Parameters:
offset:
Number
— the offset on the path, where0
is at the beginning of the path andpath.length
at the endReturns:
Point
— the normalized tangent vector at the given offset
Example:Working with the tangent vector at a given offset:
Example:Iterating over the length of a path:
getNormalAt(offset)
Calculates the normal vector of the path at the given offset.
Parameters:
offset:
Number
— the offset on the path, where0
is at the beginning of the path andpath.length
at the endReturns:
Point
— the normal vector at the given offset
Example:Working with the normal vector at a given offset:
Example:Iterating over the length of a path:
getWeightedTangentAt(offset)
Calculates the weighted tangent vector of the path at the given offset.
Parameters:
offset:
Number
— the offset on the path, where0
is at the beginning of the path andpath.length
at the endReturns:
Point
— the weighted tangent vector at the given offset
getWeightedNormalAt(offset)
Calculates the weighted normal vector of the path at the given offset.
Parameters:
offset:
Number
— the offset on the path, where0
is at the beginning of the path andpath.length
at the endReturns:
Point
— the weighted normal vector at the given offset
getCurvatureAt(offset)
Calculates the curvature of the path at the given offset. Curvatures indicate how sharply a path changes direction. A straight line has zero curvature, where as a circle has a constant curvature. The path’s radius at the given offset is the reciprocal value of its curvature.
Parameters:
offset:
Number
— the offset on the path, where0
is at the beginning of the path andpath.length
at the endReturns:
Number
— the normal vector at the given offset
getOffsetsWithTangent(tangent)
Calculates path offsets where the path is tangential to the provided tangent. Note that tangents at the start or end are included. Tangents at segment points are returned even if only one of their handles is collinear with the provided tangent.
Parameters:
tangent:
Point
— the tangent to which the path must be tangentialReturns:
Array of Numbers
— path offsets where the path is tangential to the provided tangent
Properties inherited from Item
Item
id
The unique id of the item.
Read only.
Type:
Number
className
The class name of the item as a string.
Values:
'Group'
,'Layer'
,'Path'
,'CompoundPath'
,'Shape'
,'Raster'
,'SymbolItem'
,'PointText'
Type:
String
name
The name of the item. If the item has a name, it can be accessed by name through its parent’s children list.
Type:
String
Example:
style
The path style of the item.
Type:
Style
Example:Applying several styles to an item in one go, by passing an object to its style property:
Example:Copying the style of another item:
Example:Applying the same style object to multiple items:
locked
Specifies whether the item is locked. When set to
true
, item interactions with the mouse are disabled.Default:
false
Type:
Boolean
Example:
visible
Specifies whether the item is visible. When set to
false
, the item won’t be drawn.Default:
true
Type:
Boolean
Example:Hiding an item:
blendMode
The blend mode with which the item is composited onto the canvas. Both the standard canvas compositing modes, as well as the new CSS blend modes are supported. If blend-modes cannot be rendered natively, they are emulated. Be aware that emulation can have an impact on performance.
Values:
'normal'
,'multiply'
,'screen'
,'overlay'
,'soft-light'
,'hard- light'
,'color-dodge'
,'color-burn'
,'darken'
,'lighten'
,'difference'
,'exclusion'
,'hue'
,'saturation'
,'luminosity'
,'color'
,'add'
,'subtract'
,'average'
,'pin-light'
,'negation'
,'source-over'
,'source-in'
,'source-out'
,'source-atop'
,'destination-over'
,'destination-in'
,'destination-out'
,'destination-atop'
,'lighter'
,'darker'
,'copy'
,'xor'
Default:
'normal'
Type:
String
Example:Setting an item's blend mode:
opacity
The opacity of the item as a value between
0
and1
.Default:
1
Type:
Number
Example:Making an item 50% transparent:
selected
Specifies whether the item is selected. This will also return
true
forGroup
items if they are partially selected, e.g. groups containing selected or partially selected paths.Paper.js draws the visual outlines of selected items on top of your project. This can be useful for debugging, as it allows you to see the construction of paths, position of path curves, individual segment points and bounding boxes of symbol and raster items.
Default:
false
Type:
Boolean
See also:
project.selectedItems
segment.selected
curve.selected
point.selected
Example:Selecting an item:
clipMask
Specifies whether the item defines a clip mask. This can only be set on paths and compound paths, and only if the item is already contained within a clipping group.
Default:
false
Type:
Boolean
data
A plain javascript object which can be used to store arbitrary data on the item.
Type:
Object
Example:
Example:
Example:
Example:
Position and Bounding Boxes
position
The item’s position within the parent item’s coordinate system. By default, this is the
rectangle.center
of the item’sbounds
rectangle.Type:
Point
Example:Changing the position of a path: