Layer
The Layer item represents a layer in a Paper.js project.
The layer which is currently active can be accessed through project.activeLayer
. An array of all layers in a project can be accessed through project.layers
.
Constructors
Layer([children])
Creates a new Layer item and places it at the end of the
project.layers
array. The newly created layer will be activated, so all newly created items will be placed within it.Parameters:
children:
Array ofItem
objects — An array of items that will be added to the newly created layer — optionalReturns:
Layer
Example:
Layer(object)
Creates a new Layer item and places it at the end of the
project.layers
array. The newly created layer will be activated, so all newly created items will be placed within it.Parameters:
object:
Object
— an object containing the properties to be set on the layerReturns:
Layer
Example:
Methods
activate()
Activates the layer.
Example:
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
Type:
Number
shadowOffset
The shadow’s offset.
Default:
0
Type:
Point
Selection Style
selectedColor
The color the item is highlighted with when selected. If the item does not specify its own color, the color defined by its layer is used instead.
Type:
Color
⟋null
Event Handlers
onFrame
Item level handler function to be called on each frame of an animation. The function receives an event object which contains information about the frame event:
Type:
Function
⟋null
Options:
event.count: Number
— the number of times the frame event was firedevent.time: Number
— the total amount of time passed since the first frame event in secondsevent.delta: Number
— the time passed in seconds since the last frame eventSee also:
view.onFrame
Example:Creating an animation:
onMouseDown
The function to be called when the mouse button is pushed down on the item. The function receives a
MouseEvent
object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy and will reach the view, unless they are stopped withevent.stopPropagation
() or by returningfalse
from the handler.Type:
Function
⟋null
See also:
view.onMouseDown
Example:Press the mouse button down on the circle shaped path, to make it red:
Example:Press the mouse on the circle shaped paths to remove them:
onMouseDrag
The function to be called when the mouse position changes while the mouse is being dragged over the item. The function receives a
MouseEvent
object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy and will reach the view, unless they are stopped withevent.stopPropagation
() or by returningfalse
from the handler.Type:
Function
⟋null
See also:
view.onMouseDrag
Example:Press and drag the mouse on the blue circle to move it:
onMouseUp
The function to be called when the mouse button is released over the item. The function receives a
MouseEvent
object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy and will reach the view, unless they are stopped withevent.stopPropagation
() or by returningfalse
from the handler.Type:
Function
⟋null
See also:
view.onMouseUp
Example:Release the mouse button over the circle shaped path, to make it red:
onClick
The function to be called when the mouse clicks on the item. The function receives a
MouseEvent
object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy and will reach the view, unless they are stopped withevent.stopPropagation
() or by returningfalse
from the handler.Type:
Function
⟋null
See also:
view.onClick
Example:Click on the circle shaped path, to make it red:
Example:Click on the circle shaped paths to remove them:
onDoubleClick
The function to be called when the mouse double clicks on the item. The function receives a
MouseEvent
object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy and will reach the view, unless they are stopped withevent.stopPropagation
() or by returningfalse
from the handler.Type:
Function
⟋null
See also:
view.onDoubleClick
Example:Double click on the circle shaped path, to make it red:
Example:Double click on the circle shaped paths to remove them:
onMouseMove
The function to be called repeatedly while the mouse moves over the item. The function receives a
MouseEvent
object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy and will reach the view, unless they are stopped withevent.stopPropagation
() or by returningfalse
from the handler.Type:
Function
⟋null
See also:
view.onMouseMove
Example:Move over the circle shaped path, to change its opacity:
onMouseEnter
The function to be called when the mouse moves over the item. This function will only be called again, once the mouse moved outside of the item first. The function receives a
MouseEvent
object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy and will reach the view, unless they are stopped withevent.stopPropagation
() or by returningfalse
from the handler.Type:
Function
⟋null
See also:
view.onMouseEnter
Example:When you move the mouse over the item, its fill color is set to red. When you move the mouse outside again, its fill color is set back to black.
Example:When you click the mouse, you create new circle shaped items. When you move the mouse over the item, its fill color is set to red. When you move the mouse outside again, its fill color is set back to black.
onMouseLeave
The function to be called when the mouse moves out of the item. The function receives a
MouseEvent
object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy and will reach the view, unless they are stopped withevent.stopPropagation
() or by returningfalse
from the handler.Type:
Function
⟋null
See also:
view.onMouseLeave
Example:Move the mouse over the circle shaped path and then move it out of it again to set its fill color to red:
Methods inherited from Item
Item
set(props)
Sets the properties of the passed object literal on this item to the values defined in the object literal, if the item has property of the given name (or a setter defined for it).
Parameters:
props:
Object
Returns:
Item
— the item itself
Example:Setting properties through an object literal
clone([options])
Clones the item within the same project and places the copy above the item.
Options:
insert: undefined
— specifies whether the copy should be inserted into the scene graph. When set totrue
, it is inserted above the original — default:true
deep: undefined
— specifies whether the item’s children should also be cloned — default:true
Parameters:
options:
Object
— optional, default:{ insert: true, deep: true }
Returns:
Item
— the newly cloned item
Example:Cloning items:
copyContent(source)
Copies the content of the specified item over to this item.
Parameters:
source:
Item
— the item to copy the content from
copyAttributes(source, excludeMatrix)
Copies all attributes of the specified item over to this item. This includes its style, visibility, matrix, pivot, blend-mode, opacity, selection state, data, name, etc.
Parameters:
source:
Item
— the item to copy the attributes fromexcludeMatrix:
Boolean
— whether to exclude the transformation matrix when copying all attributes
rasterize([resolution[, insert]])
Rasterizes the item into a newly created Raster object. The item itself is not removed after rasterization.
Parameters:
resolution:
Number
— the resolution of the raster in pixels per inch (DPI). If not specified, the value ofview.resolution
is used. — optional, default:view.resolution
insert:
Boolean
— specifies whether the raster should be inserted into the scene graph. When set totrue
, it is inserted above the original — optional, default:true
Returns:
Raster
— the newly created raster item
Example:Rasterizing an item:
Geometric Tests
contains(point)
Checks whether the item’s geometry contains the given point.
Parameters:
point:
Point
— the point to check forReturns:
Boolean
Example:Click within and outside the star below Create a star shaped path:
isInside(rect)
Parameters:
rect:
Rectangle
— the rectangle to check againstReturns:
Boolean
intersects(item)
Parameters:
item:
Item
— the item to check againstReturns:
Boolean
Hit-testing, Fetching and Matching Items
hitTest(point[, options])
Performs a hit-test on the item and its children (if it is a
Group
orLayer
) at the location of the specified point, returning the first found hit.The options object allows you to control the specifics of the hit- test and may contain a combination of the following values:
Options:
options.tolerance: Number
— the tolerance of the hit-test — default:paperScope.settings
.hitToleranceoptions.class: Function
— only hit-test against a specific item class, or any of its sub-classes, by providing the constructor function against which aninstanceof
check is performed:Group
,Layer
,Path
,CompoundPath
,Shape
,Raster
,SymbolItem
,PointText
, …options.match: Function
— a match function to be called for each found hit result: Returntrue
to return the result,false
to keep searchingoptions.fill: Boolean
— hit-test the fill of items — default:true
options.stroke: Boolean
— hit-test the stroke of path items, taking into account the setting of stroke color and width — default:true
options.segments: Boolean
— hit-test forsegment.point
ofPath
items — default:true
options.curves: Boolean
— hit-test the curves of path items, without taking the stroke color or width into accountoptions.handles: Boolean
— hit-test for the handles (segment.handleIn
/segment.handleOut
) of path segments.options.ends: Boolean
— only hit-test for the first or last segment points of open path itemsoptions.position: Boolean
— hit-test theitem.position
of of items, which depends on the setting ofitem.pivot
options.center: Boolean
— hit-test therectangle.center
of the bounding rectangle of items (item.bounds
)options.bounds: Boolean
— hit-test the corners and side-centers of the bounding rectangle of items (item.bounds
)options.guides: Boolean
— hit-test items that haveItem#guide
set totrue
options.selected: Boolean
— only hit selected itemsParameters:
point:
Point
— the point where the hit-test should be performed (in global coordinates system).options:
Object
— optional, default:{ fill: true, stroke: true, segments: true, tolerance: settings.hitTolerance }
Returns:
HitResult
— a hit result object describing what exactly was hit ornull
if nothing was hit
hitTestAll(point[, options])
Performs a hit-test on the item and its children (if it is a
Group
orLayer
) at the location of the specified point, returning all found hits.The options object allows you to control the specifics of the hit- test. See
hitTest(point[, options])
for a list of all options.Parameters:
point:
Point
— the point where the hit-test should be performed (in global coordinates system).options:
Object
— optional, default:{ fill: true, stroke: true, segments: true, tolerance: settings.hitTolerance }
Returns:
Array of HitResult
objects — hit result objects for all hits, describing what exactly was hit ornull
if nothing was hitSee also:
hitTest(point[, options])
;
matches(options)
Checks whether the item matches the criteria described by the given object, by iterating over all of its properties and matching against their values through
matches(name, compare)
.See
project.getItems(options)
for a selection of illustrated examples.Parameters:
options:
Object
⟋Function
— the criteria to match againstReturns:
Boolean
—true
if the item matches all the criteria,false
otherwiseSee also: