PathItem
Extends Item
The PathItem class is the base for any items that describe paths and offer standardised methods for drawing and path manipulation, such as [Path](Path%20f91b7d17f1f747e1950d655ad8aaf2c5.md)
and [CompoundPath](CompoundPath%200e46476761cb45a0ab9ed15d5e6dfffb.md)
.
Properties
interiorPoint
Returns a point that is guaranteed to be inside the path.
Read only.
Type:
Point
clockwise
Specifies whether the path as a whole is oriented clock-wise, by looking at the path’s area. Note that self-intersecting paths and sub-paths of different orientation can result in areas that cancel each other out.
Type:
Boolean
See also:
path.area
compoundPath.area
pathData
The path’s geometry, formatted as SVG style path data.
Type:
String
Methods
Boolean Path Operations
unite(path[, options])
Unites the geometry of the specified path with this path’s geometry and returns the result as a new path item.
Options:
options.insert: Boolean
— whether the resulting item should be inserted back into the scene graph, above both paths involved in the operation — default:true
Parameters:
path:
PathItem
— the path to unite withoptions:
Object
— the boolean operation options — optionalReturns:
PathItem
— the resulting path item
intersect(path[, options])
Intersects the geometry of the specified path with this path’s geometry and returns the result as a new path item.
Options:
options.insert: Boolean
— whether the resulting item should be inserted back into the scene graph, above both paths involved in the operation — default:true
options.trace: Boolean
— whether the tracing method is used, treating both paths as areas when determining which parts of the paths are to be kept in the result, or whether the first path is only to be split at intersections, keeping the parts of the curves that intersect with the area of the second path. — default:true
Parameters:
path:
PathItem
— the path to intersect withoptions:
Object
— the boolean operation options — optionalReturns:
PathItem
— the resulting path item
subtract(path[, options])
Subtracts the geometry of the specified path from this path’s geometry and returns the result as a new path item.
Options:
options.insert: Boolean
— whether the resulting item should be inserted back into the scene graph, above both paths involved in the operation — default:true
options.trace: Boolean
— whether the tracing method is used, treating both paths as areas when determining which parts of the paths are to be kept in the result, or whether the first path is only to be split at intersections, removing the parts of the curves that intersect with the area of the second path. — default:true
Parameters:
path:
PathItem
— the path to subtractoptions:
Object
— the boolean operation options — optionalReturns:
PathItem
— the resulting path item
exclude(path[, options])
Excludes the intersection of the geometry of the specified path with this path’s geometry and returns the result as a new path item.
Options:
options.insert: Boolean
— whether the resulting item should be inserted back into the scene graph, above both paths involved in the operation — default:true
Parameters:
path:
PathItem
— the path to exclude the intersection ofoptions:
Object
— the boolean operation options — optionalReturns:
PathItem
— the resulting path item
divide(path[, options])
Splits the geometry of this path along the geometry of the specified path returns the result as a new group item. This is equivalent to calling
subtract(path)
andintersect(path)
and putting the results into a new group.Options:
options.insert: Boolean
— whether the resulting item should be inserted back into the scene graph, above both paths involved in the operation — default:true
options.trace: Boolean
— whether the tracing method is used, treating both paths as areas when determining which parts of the paths are to be kept in the result, or whether the first path is only to be split at intersections. — default:true
Parameters:
path:
PathItem
— the path to divide byoptions:
Object
— the boolean operation options — optionalReturns:
PathItem
— the resulting path item
reorient([nonZero[, clockwise]])
Fixes the orientation of the sub-paths of a compound-path, assuming that non of its sub-paths intersect, by reorienting them so that they are of different winding direction than their containing paths, except for disjoint sub-paths, i.e. islands, which are oriented so that they have the same winding direction as the the biggest path.
Parameters:
nonZero:
Boolean
— controls if the non-zero fill-rule is to be applied, by counting the winding of each nested path and discarding sub-paths that do not contribute to the final result — optional, default:false
clockwise:
Boolean
— if provided, the orientation of the root paths will be set to the orientation specified byclockwise
, otherwise the orientation of the largest root child is used. — optionalReturns:
PathItem
— a reference to the item itself, reoriented
Path Intersections and Locations
getIntersections(path[, include])
Returns all intersections between two
PathItem
items as an array ofCurveLocation
objects.CompoundPath
items are also supported.Parameters:
path:
PathItem
— the other item to find the intersections withinclude:
Function
— a callback function that can be used to filter out undesired locations right while they are collected. When defined, it shall returntrue
to include a location,false
otherwise. — optionalReturns:
Array of CurveLocation
objects — the locations of all intersection between the pathsSee also:
getCrossings(path)
Example:Finding the intersections between two paths
getCrossings(path)
Returns all crossings between two
PathItem
items as an array ofCurveLocation
objects.CompoundPath
items are also supported. Crossings are intersections where the paths actually are crossing each other, as opposed to simply touching.Parameters:
path:
PathItem
— the other item to find the crossings withReturns:
Array of CurveLocation
objects — the locations of all crossings between the pathsSee also:
getIntersections(path)
getNearestLocation(point)
Returns the nearest location on the path item to the specified point.
Parameters:
point:
Point
— the point for which we search the nearest locationReturns:
CurveLocation
— the location on the path that’s the closest to the specified point
getNearestPoint(point)
Returns the nearest point on the path item to the specified point.
Parameters:
point:
Point
— the point for which we search the nearest pointReturns:
Point
— the point on the path that’s the closest to the specified point
Example:
Path Manipulation
reverse()
Reverses the orientation of the path item. When called on
CompoundPath
items, each of the nested paths is reversed. OnPath
items, the sequence ofpath.segments
is reversed.flatten([flatness])
Flattens the curves in path items to a sequence of straight lines, by subdividing them enough times until the specified maximum error is met.
Parameters:
flatness:
Number
— the maximum error between the flattened lines and the original curves — optional, default:0.25
Example:Flattening a circle shaped path:
smooth([options])
Smooths the path item without changing the amount of segments in the path or moving the segments’ locations, by smoothing and adjusting the angle and length of the segments’ handles based on the position and distance of neighboring segments.
Smoothing works both for open paths and closed paths, and can be applied to the full path, as well as a sub-range of it. If a range is defined using the
options.from
andoptions.to
properties, only the curve handles inside that range are touched. If one or both limits of the range are specified in negative indices, the indices are wrapped around the end of the curve. That way, a smoothing range in a close path can even wrap around the connection between the last and the first segment.Four different smoothing methods are available:
'continuous'
smooths the path item by adjusting its curve handles so that the first and second derivatives of all involved curves are continuous across their boundaries.This method tends to result in the smoothest results, but does not allow for further parametrization of the handles.
'asymmetric'
is based on the same principle as'continuous'
but uses different factors so that the result is asymmetric. This used to the only method available until v0.10.0, and is currently still the default when no method is specified, for reasons of backward compatibility. It will eventually be removed.'catmull-rom'
uses the Catmull-Rom spline to smooth the segment.The optionally passed factor controls the knot parametrization of the algorithm:
0.0
: the standard, uniform Catmull-Rom spline0.5
: the centripetal Catmull-Rom spline, guaranteeing no self-intersections1.0
: the chordal Catmull-Rom spline
'geometric'
use a simple heuristic and empiric geometric method to smooth the segment’s handles. The handles were weighted, meaning that big differences in distances between the segments will lead to probably undesired results.The optionally passed factor defines the tension parameter (
0…1
), controlling the amount of smoothing as a factor by which to scale each handle.Options:
options.type: String
— the type of smoothing method:‘continuous’
,‘asymmetric’
,‘catmull-rom’
,‘geometric’
— default:‘asymmetric’
options.factor: Number
— the factor parameterizing the smoothing method — default:0.5
for'catmull-rom'
,0.4
for'geometric'
options.from: Number
⟋Segment
⟋Curve
— the segment or curve at which to start smoothing, if not the full path shall be smoothed (inclusive). This can either be a segment index, or a segment or curve object that is part of the path. If the passed number is negative, the index is wrapped around the end of the path.options.to: Number
⟋Segment
⟋Curve
— the segment or curve to which the handles of the path shall be processed (inclusive). This can either be a segment index, or a segment or curve object that is part of the path. If the passed number is negative, the index is wrapped around the end of the path.Parameters:
options:
Object
— the smoothing options — optionalSee also:
segment.smooth([options])
Example:Smoothing a closed shape:
Example:
Example:Smoothing ranges of paths, using segments, curves or indices:
simplify([tolerance])
Fits a sequence of as few curves as possible through the path’s anchor points, ignoring the path items’s curve-handles, with an allowed maximum error. When called on
CompoundPath
items, each of the nested paths is simplified. OnPath
items, thepath.segments
array is processed and replaced by the resulting sequence of fitted curves.This method can be used to process and simplify the point data received from a mouse or touch device.
Parameters:
tolerance:
Number
— the allowed maximum error when fitting the curves through the segment points — optional, default:2.5
Returns:
Boolean
—true
if the method was capable of fitting curves through the path’s segment points,false
otherwise
Example:Click and drag below to draw to draw a line, when you release the mouse, the is made smooth using path.simplify():
interpolate(from, to, factor)
Interpolates between the two specified path items and uses the result as the geometry for this path item. The number of children and segments in the two paths involved in the operation should be the same.
Parameters:
from:
PathItem
— the path item defining the geometry whenfactor
is0
to:
PathItem
— the path item defining the geometry whenfactor
is1
factor:
Number
— the interpolation coefficient, typically between0
and1
, but extrapolation is possible too
compare(path)
Compares the geometry of two paths to see if they describe the same shape, detecting cases where paths start in different segments or even use different amounts of curves to describe the same shape, as long as their orientation is the same, and their segments and handles really result in the same visual appearance of curves.
Parameters:
path:
PathItem
— the path to compare this path’s geometry withReturns:
Boolean
—true
if two paths describe the same shape,false
otherwise
Postscript Style Drawing Commands
moveTo(point)
On a normal empty
Path
, the point is simply added as the path’s first segment. If called on aCompoundPath
, a newPath
is created as a child and the point is added as its first segment.Parameters:
point:
Point
— the point in which to start the path
lineTo(point)
Adds a straight curve to the path, from the the last segment in the path to the specified point.
Parameters:
point:
Point
— the destination point of the newly added straight curve
arcTo(through, to)
Adds an arc from the position of the last segment in the path, passing through the specified
through
point, to the specifiedto
point, by adding one or more segments to the path.Parameters:
through:
Point
— the point where the arc should pass throughto:
Point
— the point where the arc should end
Example:
Example:Interactive example. Click and drag in the view below:
arcTo(to[, clockwise])
Adds an arc from the position of the last segment in the path to the specified point, by adding one or more segments to the path.
Parameters:
to:
Point
— the point where the arc should endclockwise:
Boolean
— specifies whether the arc should be drawn in clockwise direction — optional, default:true
Example:
Example:Interactive example. Click and drag in the view below:
curveTo(through, to[, time])
Adds a curve from the last segment in the path through the specified
through
point, to the specified destination point by adding one segment to the path.Parameters:
through:
Point
— the point through which the curve should passto:
Point
— the destination point of the newly added curvetime:
Number
— the curve-time parameter at which thethrough
point is to be located — optional, default:0.5
Example:Interactive example. Move your mouse around the view below:
cubicCurveTo(handle1, handle2, to)
Adds a cubic bezier curve to the path, from the last segment to the specified destination point, with the curve itself defined by two specified handles.
Parameters:
handle1:
Point
— the location of the first handle of the newly added curve in absolute coordinates, out of which the relative values forsegment.handleOut
of its first segment are calculatedhandle2:
Point
— the location of the second handle of the newly added curve in absolute coordinates, out of which the relative values forsegment.handleIn
of its second segment are calculatedto:
Point
— the destination point of the newly added curve
quadraticCurveTo(handle, to)
Adds a quadratic bezier curve to the path, from the last segment to the specified destination point, with the curve itself defined by the specified handle.
Note that Paper.js only stores cubic curves, so the handle is actually converted.
Parameters:
handle:
Point
— the location of the handle of the newly added quadratic curve in absolute coordinates, out of which the relative values forsegment.handleOut
of the resulting cubic curve’s first segment andsegment.handleIn
of its second segment are calculatedto:
Point
— the destination point of the newly added curve
closePath()
Closes the path. When closed, Paper.js connects the first and last segment of the path with an additional curve. The difference to setting
path.closed
totrue
is that this will also merge the first segment with the last if they lie in the same location.See also:
path.closed
Relative Drawing Commands
moveBy(to)
If called on a
CompoundPath
, a newPath
is created as a child and a point is added as its first segment relative to the position of the last segment of the current path.Parameters:
to:
Point
lineBy(point)
Adds a straight curve to the path, from the the last segment in the path to the
to
vector specified relatively to it.Parameters:
point:
Point
— the vector describing the destination of the newly added straight curve
Example:
Example:Drawing a spiral using lineBy:
arcBy(through, to)
Adds an arc from the position of the last segment in the path, passing through the specified
through
vector, to the specifiedto
vector, all specified relatively to it by these given vectors, by adding one or more segments to the path.Parameters:
through:
Point
— the vector where the arc should pass throughto:
Point
— the vector where the arc should end
arcBy(to[, clockwise])
Adds an arc from the position of the last segment in the path to the
to
vector specified relatively to it, by adding one or more segments to the path.Parameters:
to:
Point
— the vector where the arc should endclockwise:
Boolean
— specifies whether the arc should be drawn in clockwise direction — optional, default:true
curveBy(through, to[, time])
Adds a curve from the last segment in the path through the specified
through
vector, to the specifiedto
vector, all specified relatively to it by these given vectors, by adding one segment to the path.Parameters:
through:
Point
— the vector through which the curve should passto:
Point
— the destination vector of the newly added curvetime:
Number
— the curve-time parameter at which thethrough
point is to be located — optional, default:0.5
cubicCurveBy(handle1, handle2, to)
Adds a cubic bezier curve to the path, from the last segment to the to the specified
to
vector, with the curve itself defined by two specified handles.Parameters:
handle1:
Point
— the location of the first handle of the newly added curvehandle2:
Point
— the location of the second handle of the newly added curveto:
Point
— the destination point of the newly added curve
quadraticCurveBy(handle, to)
Adds a quadratic bezier curve to the path, from the last segment to the specified destination point, with the curve itself defined by the specified handle.
Note that Paper.js only stores cubic curves, so the handle is actually converted.
Parameters:
handle:
Point
— the handle of the newly added quadratic curve out of which the values forsegment.handleOut
of the resulting cubic curve’s first segment andsegment.handleIn
of its second segment are calculatedto:
Point
— the destination point of the newly added curve
Static Methods
PathItem.create(pathData)
Creates a path item from the given SVG path-data, determining if the data describes a plain path or a compound-path with multiple sub-paths.
Parameters:
pathData:
String
— the SVG path-data to parseReturns:
Path
⟋CompoundPath
— the newly created path item
PathItem.create(segments)
Creates a path item from the given segments array, determining if the array describes a plain path or a compound-path with multiple sub-paths.
Parameters:
segments:
Array ofNumber[]
objects[] — the segments array to parseReturns:
Path
⟋CompoundPath
— the newly created path item
PathItem.create(object)
Creates a path item from the given object, determining if the contained information describes a plain path or a compound-path with multiple sub-paths.
Parameters:
object:
Object
— an object containing the properties describing the item to be createdReturns:
Path
⟋CompoundPath
— the newly created path item
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:
Example:Changing the x coordinate of an item's position:
pivot
The item’s pivot point specified in the item coordinate system, defining the point around which all transformations are hinging. This is also the reference point for
position
. By default, it is set tonull
, meaning therectangle.center
of the item’sbounds
rectangle is used as pivot.Default:
null
Type:
Point
bounds
The bounding rectangle of the item excluding stroke width.
Type:
Rectangle
strokeBounds
The bounding rectangle of the item including stroke width.
Type:
Rectangle
handleBounds
The bounding rectangle of the item including handles.
Type:
Rectangle
internalBounds
The bounding rectangle of the item without any matrix transformations.
Typical use case would be drawing a frame around the object where you want to draw something of the same size, position, rotation, and scaling, like a selection frame.
Type:
Rectangle
rotation
The current rotation angle of the item, as described by its
matrix
. Please note that this only returns meaningful values for items withapplyMatrix
set tofalse
, meaning they do not directly bake transformations into their content.Type:
Number
scaling
The current scale factor of the item, as described by its
matrix
. Please note that this only returns meaningful values for items withapplyMatrix
set tofalse
, meaning they do not directly bake transformations into their content.Type:
Point
matrix
The item’s transformation matrix, defining position and dimensions in relation to its parent item in which it is contained.
Type:
Matrix
globalMatrix
The item’s global transformation matrix in relation to the global project coordinate space. Note that the view’s transformations resulting from zooming and panning are not factored in.
Read only.
Type:
Matrix
viewMatrix
The item’s global matrix in relation to the view coordinate space. This means that the view’s transformations resulting from zooming and panning are factored in.
Read only.
Type:
Matrix
applyMatrix
Controls whether the transformations applied to the item (e.g. through
transform(matrix)
,rotate(angle)
,scale(scale)
, etc.) are stored in itsmatrix
property, or whether they are directly applied to its contents or children (passed on to the segments inPath
items, the children ofGroup
items, etc.).Default:
true
Type:
Boolean
Project Hierarchy
project
The project that this item belongs to.
Read only.
Type:
Project
view
The view that this item belongs to.
Read only.
Type:
View
layer
The layer that this item is contained within.
Read only.
Type:
Layer
parent
The item that this item is contained within.
Type:
Item
Example:
Example:Setting the parent of the item to another item
Example:Setting the parent of an item in the constructor
children
The children items contained within this item. Items that define a
name
can also be accessed by name.Please note: The children array should not be modified directly using array functions. To remove single items from the children list, use
item.remove
(), to remove all items from the children list, useitem.removeChildren
(). To add items to the children list, useitem.addChild(item)
oritem.insertChild(index, item)
.Type:
Array of
Item
objects
Example:Accessing items in the children array:
Example:Accessing children by name:
Example:Passing an array of items to item.children:
firstChild
The first item contained within this item. This is a shortcut for accessing
item.children[0]
.Read only.
Type:
Item
lastChild
The last item contained within this item.This is a shortcut for accessing
item.children[item.children.length - 1]
.Read only.
Type:
Item
nextSibling
The next item on the same level as this item.
Read only.
Type:
Item
previousSibling
The previous item on the same level as this item.
Read only.
Type:
Item
index
The index of this item within the list of its parent’s children.
Read only.
Type:
Number
Stroke Style
strokeColor
The color of the stroke.
Type:
Color
⟋null
Example:Setting the stroke color of a path:
strokeWidth
The width of the stroke.
Type:
Number
Example:Setting an item's stroke width:
strokeCap
The shape to be used at the beginning and end of open
Path
items, when they have a stroke.Values:
'round'
,'square'
,'butt'
Default:
'butt'
Type:
String
Example:A look at the different stroke caps:
strokeJoin
The shape to be used at the segments and corners of
Path
items when they have a stroke.Values:
'miter'
,'round'
,'bevel'
Default:
'miter'
Type:
String
Example:A look at the different stroke joins:
dashOffset
The dash offset of the stroke.
Default:
0
Type:
Number
strokeScaling
Specifies whether the stroke is to be drawn taking the current affine transformation into account (the default behavior), or whether it should appear as a non-scaling stroke.
Default:
true
Type:
Boolean
dashArray
Specifies an array containing the dash and gap lengths of the stroke.
Default:
[]
Type:
Array of
Numbers
Example:
miterLimit
The miter limit of the stroke. When two line segments meet at a sharp angle and miter joins have been specified for
item.strokeJoin
, it is possible for the miter to extend far beyond theitem.strokeWidth
of the path. The miterLimit imposes a limit on the ratio of the miter length to theitem.strokeWidth
.Default:
10
Type:
Number
Fill Style
fillColor
The fill color of the item.
Type:
Color
⟋null
Example:Setting the fill color of a path to red:
fillRule
The fill-rule with which the shape gets filled. Please note that only modern browsers support fill-rules other than
'nonzero'
.Values:
'nonzero'
,'evenodd'
Default:
'nonzero'
Type:
String
Shadow Style
shadowColor
The shadow color.
Type:
Color
⟋null
Example:Creating a circle with a black shadow:
shadowBlur
The shadow’s blur radius.
Default:
0