TextItem
Extends Item
The TextItem type allows you to create typography. Its functionality is inherited by different text item types such as [PointText](PointText%20b65facc515f74b5ca4d27a8229b32789.md), and AreaText (coming soon). They each add a layer of functionality that is unique to their type, but share the underlying properties and functions that they inherit from TextItem.
Properties
contentThe text contents of the text item.
Type:
String
Example:Setting the content of a PointText item:
// Create a point-text item at {x: 30, y: 30}: var text = new PointText(new Point(30, 30)); text.fillColor = 'black'; // Set the content of the text item: text.content = 'Hello world';Example:Interactive example, move your mouse over the view below:
// Create a point-text item at {x: 30, y: 30}: var text = new PointText(new Point(30, 30)); text.fillColor = 'black'; text.content = 'Move your mouse over the view, to see its position'; function onMouseMove(event) { // Each time the mouse is moved, set the content of // the point text to describe the position of the mouse: text.content = 'Your position is: ' + event.point.toString(); }
Character Style
fontFamilyThe font-family to be used in text content.
Default:
'sans-serif'Type:
String
fontWeightThe font-weight to be used in text content.
Default:
'normal'Type:
String⟋Number
fontSizeThe font size of text content, as a number in pixels, or as a string with optional units
'px','pt'and'em'.Default:
10Type:
Number⟋String
leadingThe text leading of text content.
Default:
fontSize * 1.2Type:
Number⟋String
Paragraph Style
justificationThe justification of text paragraphs.
Values:
'left','right','center'Default:
'left'Type:
String
Properties inherited from Item
ItemidThe unique id of the item.
Read only.
Type:
Number
classNameThe class name of the item as a string.
Values:
'Group','Layer','Path','CompoundPath','Shape','Raster','SymbolItem','PointText'Type:
String
nameThe 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:
styleThe 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:
lockedSpecifies whether the item is locked. When set to
true, item interactions with the mouse are disabled.Default:
falseType:
Boolean
Example:
visibleSpecifies whether the item is visible. When set to
false, the item won’t be drawn.Default:
trueType:
Boolean
Example:Hiding an item:
blendModeThe 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:
opacityThe opacity of the item as a value between
0and1.Default:
1Type:
Number
Example:Making an item 50% transparent:
selectedSpecifies whether the item is selected. This will also return
trueforGroupitems 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:
falseType:
BooleanSee also:
project.selectedItemssegment.selectedcurve.selectedpoint.selected
Example:Selecting an item:
clipMaskSpecifies 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:
falseType:
Boolean
dataA plain javascript object which can be used to store arbitrary data on the item.
Type:
Object
Example:
Example:
Example:
Example:
Position and Bounding Boxes
positionThe item’s position within the parent item’s coordinate system. By default, this is the
rectangle.centerof the item’sboundsrectangle.Type:
Point
Example:Changing the position of a path:
Example:Changing the x coordinate of an item's position:
pivotThe 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.centerof the item’sboundsrectangle is used as pivot.Default:
nullType:
Point
boundsThe bounding rectangle of the item excluding stroke width.
Type:
Rectangle
strokeBoundsThe bounding rectangle of the item including stroke width.
Type:
Rectangle
handleBoundsThe bounding rectangle of the item including handles.
Type:
Rectangle
internalBoundsThe 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
rotationThe current rotation angle of the item, as described by its
matrix. Please note that this only returns meaningful values for items withapplyMatrixset tofalse, meaning they do not directly bake transformations into their content.Type:
Number
scalingThe current scale factor of the item, as described by its
matrix. Please note that this only returns meaningful values for items withapplyMatrixset tofalse, meaning they do not directly bake transformations into their content.Type:
Point
matrixThe item’s transformation matrix, defining position and dimensions in relation to its parent item in which it is contained.
Type:
Matrix
globalMatrixThe 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
viewMatrixThe 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
applyMatrixControls whether the transformations applied to the item (e.g. through
transform(matrix),rotate(angle),scale(scale), etc.) are stored in itsmatrixproperty, or whether they are directly applied to its contents or children (passed on to the segments inPathitems, the children ofGroupitems, etc.).Default:
trueType:
Boolean
Project Hierarchy
projectThe project that this item belongs to.
Read only.
Type:
Project
viewThe view that this item belongs to.
Read only.
Type:
View
layerThe layer that this item is contained within.
Read only.
Type:
Layer
parentThe 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
childrenThe children items contained within this item. Items that define a
namecan 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
Itemobjects
Example:Accessing items in the children array:
Example:Accessing children by name:
Example:Passing an array of items to item.children:
firstChildThe first item contained within this item. This is a shortcut for accessing
item.children[0].Read only.
Type:
Item
lastChildThe last item contained within this item.This is a shortcut for accessing
item.children[item.children.length - 1].Read only.
Type:
Item
nextSiblingThe next item on the same level as this item.
Read only.
Type:
Item
previousSiblingThe previous item on the same level as this item.
Read only.
Type:
Item
indexThe index of this item within the list of its parent’s children.
Read only.
Type:
Number
Stroke Style
strokeColorThe color of the stroke.
Type:
Color⟋null
Example:Setting the stroke color of a path:
strokeWidthThe width of the stroke.
Type:
Number
Example:Setting an item's stroke width:
strokeCapThe shape to be used at the beginning and end of open
Pathitems, when they have a stroke.Values:
'round','square','butt'Default:
'butt'Type:
String
Example:A look at the different stroke caps:
strokeJoinThe shape to be used at the segments and corners of
Pathitems when they have a stroke.Values:
'miter','round','bevel'Default:
'miter'Type:
String
Example:A look at the different stroke joins:
dashOffsetThe dash offset of the stroke.
Default:
0Type:
Number
strokeScalingSpecifies 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:
trueType:
Boolean
dashArraySpecifies an array containing the dash and gap lengths of the stroke.
Default:
[]Type:
Array of
Numbers
Example:
miterLimitThe 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.strokeWidthof the path. The miterLimit imposes a limit on the ratio of the miter length to theitem.strokeWidth.Default:
10Type:
Number
Fill Style
fillColorThe fill color of the item.
Type:
Color⟋null
Example:Setting the fill color of a path to red:
fillRuleThe 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
shadowColorThe shadow color.
Type:
Color⟋null
Example:Creating a circle with a black shadow:
shadowBlurThe shadow’s blur radius.
Default:
0Type:
Number
shadowOffsetThe shadow’s offset.
Default:
0Type:
Point
Selection Style
selectedColorThe 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
onFrameItem 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⟋nullOptions:
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:
onMouseDownThe function to be called when the mouse button is pushed down on the item. The function receives a
MouseEventobject 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 returningfalsefrom the handler.Type:
Function⟋nullSee 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:
onMouseDragThe function to be called when the mouse position changes while the mouse is being dragged over the item. The function receives a
MouseEventobject 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 returningfalsefrom the handler.Type:
Function⟋nullSee also:
view.onMouseDrag
Example:Press and drag the mouse on the blue circle to move it:
onMouseUpThe function to be called when the mouse button is released over the item. The function receives a
MouseEventobject 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 returningfalsefrom the handler.Type:
Function⟋nullSee also:
view.onMouseUp
Example:Release the mouse button over the circle shaped path, to make it red:
onClickThe function to be called when the mouse clicks on the item. The function receives a
MouseEventobject 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 returningfalsefrom the handler.Type:
Function⟋nullSee also:
view.onClick
Example:Click on the circle shaped path, to make it red:
Example:Click on the circle shaped paths to remove them:
onDoubleClickThe function to be called when the mouse double clicks on the item. The function receives a
MouseEventobject 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 returningfalsefrom the handler.Type:
Function⟋nullSee 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:
onMouseMoveThe function to be called repeatedly while the mouse moves over the item. The function receives a
MouseEventobject 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 returningfalsefrom the handler.Type:
Function⟋nullSee also:
view.onMouseMove
Example:Move over the circle shaped path, to change its opacity:
onMouseEnterThe 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
MouseEventobject 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 returningfalsefrom the handler.Type:
Function⟋nullSee 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.
onMouseLeaveThe function to be called when the mouse moves out of the item. The function receives a
MouseEventobject 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 returningfalsefrom the handler.Type:
Function⟋nullSee 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
Itemset(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:ObjectReturns:
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:truedeep: undefined— specifies whether the item’s children should also be cloned — default:trueParameters:
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.resolutionis used. — optional, default:view.resolutioninsert:Boolean— specifies whether the raster should be inserted into the scene graph. When set totrue, it is inserted above the original — optional, default:trueReturns:
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
GrouporLayer) 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 aninstanceofcheck is performed:Group,Layer,Path,CompoundPath,Shape,Raster,SymbolItem,PointText, …options.match: Function— a match function to be called for each found hit result: Returntrueto return the result,falseto keep searchingoptions.fill: Boolean— hit-test the fill of items — default:trueoptions.stroke: Boolean— hit-test the stroke of path items, taking into account the setting of stroke color and width — default:trueoptions.segments: Boolean— hit-test forsegment.pointofPathitems — default:trueoptions.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.positionof of items, which depends on the setting ofitem.pivotoptions.center: Boolean— hit-test therectangle.centerof 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#guideset totrueoptions.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 ornullif nothing was hit
hitTestAll(point[, options])Performs a hit-test on the item and its children (if it is a
GrouporLayer) 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 HitResultobjects — hit result objects for all hits, describing what exactly was hit ornullif 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—trueif the item matches all the criteria,falseotherwiseSee also:
getItems(options)
matches(name, compare)Checks whether the item matches the given criteria. Extended matching is possible by providing a compare function or a regular expression. Matching points, colors only work as a comparison of the full object, not partial matching (e.g. only providing the x-coordinate to match all points with that x-value). Partial matching does work for
item.data.See
project.getItems(options)for a selection of illustrated examples.Parameters:
name:String— the name of the state to match againstcompare:Object— the value, function or regular expression to compare againstReturns:
Boolean—trueif the item matches the state,falseotherwiseSee also:
getItems(options)
getItems(options)Fetch the descendants (children or children of children) of this item that match the properties in the specified object. Extended matching is possible by providing a compare function or regular expression. Matching points, colors only work as a comparison of the full object, not partial matching (e.g. only providing the x- coordinate to match all points with that x-value). Partial matching does work for
item.data.Matching items against a rectangular area is also possible, by setting either
options.insideoroptions.overlappingto a rectangle describing the area in which the items either have to be fully or partly contained.See
project.getItems(options)for a selection of illustrated examples.Options:
options.recursive: Boolean— whether to loop recursively through all children, or stop at the current level — default:trueoptions.match: Function— a match function to be called for each item, allowing the definition of more flexible item checks that are not bound to properties. If no other match properties are defined, this function can also be passed instead of theoptionsobjectoptions.class: Function— the constructor function of the item type to match againstoptions.inside: Rectangle— the rectangle in which the items need to be fully containedoptions.overlapping: Rectangle— the rectangle with which the items need to at least partly overlapParameters:
options:Object⟋Function— the criteria to match againstReturns:
Array of Itemobjects — the list of matching descendant itemsSee also:
matches(options)
getItem(options)Fetch the first descendant (child or child of child) of this item that matches the properties in the specified object. Extended matching is possible by providing a compare function or regular expression. Matching points, colors only work as a comparison of the full object, not partial matching (e.g. only providing the x- coordinate to match all points with that x-value). Partial matching does work for
item.data. Seeproject.getItems(match)for a selection of illustrated examples.Parameters:
options:Object⟋Function— the criteria to match againstReturns:
Item— the first descendant item matching the given criteriaSee also:
getItems(options)
Importing / Exporting JSON and SVG
exportJSON([options])Exports (serializes) the item with its content and child items to a JSON data string.
Options:
options.asString: Boolean— whether the JSON is returned as aObjector aString— default:trueoptions.precision: Number— the amount of fractional digits in numbers used in JSON data — default:5Parameters:
options:Object— the serialization options — optionalReturns:
String— the exported JSON data
importJSON(json)Imports (deserializes) the stored JSON data into this item. If the data describes an item of the same class or a parent class of the item, the data is imported into the item itself. If not, the imported item is added to this item’s
item.childrenlist. Note that not all type of items can have children.Parameters:
json:String— the JSON data to import fromReturns:
Item
exportSVG([options])Exports the item with its content and child items as an SVG DOM.
Options:
options.bounds: String⟋Rectangle— the bounds of the area to export, either as a string (‘view’,content’), or aRectangleobject:'view'uses the view bounds,'content'uses the stroke bounds of all content — default:‘view’options.matrix: Matrix— the matrix with which to transform the exported content: Ifoptions.boundsis set to'view',paper.view.matrixis used, for all other settings ofoptions.boundsthe identity matrix is used. — default:paper.view.matrixoptions.asString: Boolean— whether a SVG node or aStringis to be returned — default:falseoptions.precision: Number— the amount of fractional digits in numbers used in SVG data — default:5options.matchShapes: Boolean— whether path items should tried to be converted to SVG shape items (rect, circle, ellipse, line, polyline, polygon), if their geometries match — default:falseoptions.embedImages: Boolean— whether raster images should be embedded as base64 data inlined in the xlink:href attribute, or kept as a link to their external URL. — default:trueParameters:
options:Object— the export options — optionalReturns:
SVGElement⟋String— the item converted to an SVG node or aStringdepending onoption.asStringvalue
importSVG(svg[, options])Converts the provided SVG content into Paper.js items and adds them to the this item’s children list. Note that the item is not cleared first. You can call
item.removeChildren() to do so.Options:
options.expandShapes: Boolean— whether imported shape items should be expanded to path items — default:falseoptions.onLoad: Function— the callback function to call once the SVG content is loaded from the given URL receiving two arguments: the converteditemand the originalsvgdata as a string. Only required when loading from external resources.options.onError: Function— the callback function to call if an error occurs during loading. Only required when loading from external resources.options.insert: Boolean— whether the imported items should be added to the item thatimportSVG()is called on — default:trueoptions.applyMatrix: Boolean— whether the imported items should have their transformation matrices applied to their contents or not — default:paperScope.settings.applyMatrixParameters:
svg:SVGElement⟋String— the SVG content to import, either as a SVG DOM node, a string containing SVG content, or a string describing the URL of the SVG file to fetch.options:Object— the import options — optionalReturns:
Item— the newly created Paper.js item containing the converted SVG content
importSVG(svg, onLoad)Imports the provided external SVG file, converts it into Paper.js items and adds them to the this item’s children list. Note that the item is not cleared first. You can call
item.removeChildren() to do so.Parameters:
svg:SVGElement⟋String— the URL of the SVG file to fetch.onLoad:Function— the callback function to call once the SVG content is loaded from the given URL receiving two arguments: the converteditemand the originalsvgdata as a string. Only required when loading from external files.Returns:
Item— the newly created Paper.js item containing the converted SVG content
Hierarchy Operations
addChild(item)Adds the specified item as a child of this item at the end of the its
childrenlist. You can use this function for groups, compound paths and layers.Parameters:
item:Item— the item to be added as a childReturns:
Item— the added item, ornullif adding was not possible
insertChild(index, item)Inserts the specified item as a child of this item at the specified index in its
childrenlist. You can use this function for groups, compound paths and layers.Parameters:
index:Number— the index at which to insert the itemitem:Item— the item to be inserted as a childReturns:
Item— the inserted item, ornullif inserting was not possible
addChildren(items)Adds the specified items as children of this item at the end of the its children list. You can use this function for groups, compound paths and layers.
Parameters:
items:Array ofItemobjects — the items to be added as childrenReturns:
Array of Itemobjects — the added items, ornullif adding was not possible
insertChildren(index, items)Inserts the specified items as children of this item at the specified index in its
childrenlist. You can use this function for groups, compound paths and layers.Parameters:
index:Numberitems:Array ofItemobjects — the items to be appended as childrenReturns:
Array of Itemobjects — the inserted items, ornullif inserted was not possible
insertAbove(item)Inserts this item above the specified item.
Parameters:
item:Item— the item above which it should be insertedReturns:
Item— the inserted item, ornullif inserting was not possible
insertBelow(item)Inserts this item below the specified item.
Parameters:
item:Item— the item below which it should be insertedReturns:
Item— the inserted item, ornullif inserting was not possible
sendToBack()Sends this item to the back of all other items within the same parent.
bringToFront()Brings this item to the front of all other items within the same parent.
addTo(owner)Adds it to the specified owner, which can be either a
Itemor aProject.Parameters:
owner:Project⟋Layer⟋Group⟋CompoundPath— the item or project to add the item toReturns:
Item— the item itself, if it was successfully added
copyTo(owner)Clones the item and adds it to the specified owner, which can be either a
Itemor aProject.Parameters:
owner:Project⟋Layer⟋Group⟋CompoundPath— the item or project to copy the item toReturns:
Item— the new copy of the item, if it was successfully added
reduce(options)If this is a group, layer or compound-path with only one child-item, the child-item is moved outside and the parent is erased. Otherwise, the item itself is returned unmodified.
Parameters:
options:Returns:
Item— the reduced item
remove()Removes the item and all its children from the project. The item is not destroyed and can be inserted again after removal.
Returns:
Boolean—trueif the item was removed,falseotherwise
replaceWith(item)Replaces this item with the provided new item which will takes its place in the project hierarchy instead.
Parameters:
item:Item— the item that will replace this itemReturns:
Boolean—trueif the item was replaced,falseotherwise
removeChildren()Removes all of the item’s
children(if any).Returns:
Array of Itemobjects — an array containing the removed items
removeChildren(start[, end])Removes the children from the specified
startindex to and excluding theendindex from the parent’schildrenarray.Parameters:
start:Number— the beginning index, inclusiveend:Number— the ending index, exclusive — optional, default:children.lengthReturns:
Array of Itemobjects — an array containing the removed items
reverseChildren()Reverses the order of the item’s children
Tests
isEmpty([recursively])Specifies whether the item has any content or not. The meaning of what content is differs from type to type. For example, a
Groupwith no children, aTextItemwith no text content and aPathwith no segments all are considered empty.Parameters:
recursively:Boolean— whether an item with children should be considered empty if all its descendants are empty — optional, default:falseReturns:
Boolean
Style Tests
hasFill()Checks whether the item has a fill.
Returns:
Boolean—trueif the item has a fill,falseotherwise
hasStroke()Checks whether the item has a stroke.
Returns:
Boolean—trueif the item has a stroke,falseotherwise
hasShadow()Checks whether the item has a shadow.
Returns:
Boolean—trueif the item has a shadow,falseotherwise
Hierarchy Tests
hasChildren()Checks if the item contains any children items.
Returns:
Boolean—trueit has one or more children,falseotherwise
isInserted()Checks whether the item and all its parents are inserted into scene graph or not.
Returns:
Boolean—trueif the item is inserted into the scene graph,falseotherwise
isAbove(item)Checks if this item is above the specified item in the stacking order of the project.
Parameters:
item:Item— the item to check againstReturns:
Boolean—trueif it is above the specified item,falseotherwise
isBelow(item)Checks if the item is below the specified item in the stacking order of the project.
Parameters:
item:Item— the item to check againstReturns:
Boolean—trueif it is below the specified item,falseotherwise
isParent(item)Checks whether the specified item is the parent of the item.
Parameters:
item:Item— the item to check againstReturns:
Boolean—trueif it is the parent of the item,falseotherwise
isChild(item)Checks whether the specified item is a child of the item.
Parameters:
item:Item— the item to check againstReturns:
Boolean—trueit is a child of the item,falseotherwise
isDescendant(item)Checks if the item is contained within the specified item.
Parameters:
item:Item— the item to check againstReturns:
Boolean—trueif it is inside the specified item,falseotherwise
isAncestor(item)Checks if the item is an ancestor of the specified item.
Parameters:
item:Item— the item to check againstReturns:
Boolean—trueif the item is an ancestor of the specified item,falseotherwise
isSibling(item)Checks if the item is an a sibling of the specified item.
Parameters:
item:Item— the item to check againstReturns:
Boolean—trueif the item is aa sibling of the specified item,falseotherwise
isGroupedWith(item)Checks whether the item is grouped with the specified item.
Parameters:
item:ItemReturns:
Boolean—trueif the items are grouped together,falseotherwise
Transform Functions
translate(delta)Translates (moves) the item by the given offset views.
Parameters:
delta:Point— the offset to translate the item by
rotate(angle[, center])Rotates the item by a given angle around the given center point.
Angles are oriented clockwise and measured in degrees.
Parameters:
angle:Number— the rotation anglecenter:Point— optional, default:item.positionSee also:
matrix.rotate(angle[, center])
Example:Rotating an item:
Example:Rotating an item around a specific point:
scale(scale[, center])Scales the item by the given value from its center point, or optionally from a supplied point.
Parameters:
scale:Number— the scale factorcenter:Point— optional, default:item.position
Example:Scaling an item from its center point:
Example:Scaling an item from a specific point:
scale(hor, ver[, center])Scales the item by the given values from its center point, or optionally from a supplied point.
Parameters:
hor:Number— the horizontal scale factorver:Number— the vertical scale factorcenter:Point— optional, default:item.position
Example:Scaling an item horizontally by 300%:
shear(shear[, center])Shears the item by the given value from its center point, or optionally by a supplied point.
Parameters:
shear:Point— the horizontal and vertical shear factors as a pointcenter:Point— optional, default:item.positionSee also:
matrix.shear(shear[, center])
shear(hor, ver[, center])Shears the item by the given values from its center point, or optionally by a supplied point.
Parameters:
hor:Number— the horizontal shear factorver:Number— the vertical shear factorcenter:Point— optional, default:item.positionSee also:
matrix.shear(hor, ver[, center])
skew(skew[, center])Skews the item by the given angles from its center point, or optionally by a supplied point.
Parameters:
skew:Point— the horizontal and vertical skew angles in degreescenter:Point— optional, default:item.positionSee also:
matrix.shear(skew[, center])
skew(hor, ver[, center])Skews the item by the given angles from its center point, or optionally by a supplied point.
Parameters:
hor:Number— the horizontal skew angle in degreesver:Number— the vertical sskew angle in degreescenter:Point— optional, default:item.positionSee also:
matrix.shear(hor, ver[, center])
transform(matrix)Transform the item.
Parameters:
matrix:Matrix— the matrix by which the item shall be transformed
globalToLocal(point)Converts the specified point from global project coordinate space to the item’s own local coordinate space.
Parameters:
point:Point— the point to be transformedReturns:
Point— the transformed point as a new instance
localToGlobal(point)Converts the specified point from the item’s own local coordinate space to the global project coordinate space.
Parameters:
point:Point— the point to be transformedReturns:
Point— the transformed point as a new instance
parentToLocal(point)Converts the specified point from the parent’s coordinate space to item’s own local coordinate space.
Parameters:
point:Point— the point to be transformedReturns:
Point— the transformed point as a new instance
localToParent(point)Converts the specified point from the item’s own local coordinate space to the parent’s coordinate space.
Parameters:
point:Point— the point to be transformedReturns:
Point— the transformed point as a new instance
fitBounds(rectangle[, fill])Transform the item so that its
boundsfit within the specified rectangle, without changing its aspect ratio.Parameters:
rectangle:Rectanglefill:Boolean— optional, default:false
Example:Fitting an item to the bounding rectangle of another item's bounding rectangle:
Example:Fitting an item to the bounding rectangle of another item's bounding rectangle with the fill parameter set to true:
Example:Fitting an item to the bounding rectangle of the view
Event Handling
on(type, function)Attaches an event handler to the item.
Parameters:
type:String— the type of event:‘frame’,mousedown’,‘mouseup’,‘mousedrag’,‘click’,‘doubleclick’,‘mousemove’,‘mouseenter’,‘mouseleave’function:Function— the function to be called when the event occurs, receiving aMouseEventorEventobject as its sole argumentReturns:
Item— this item itself, so calls can be chained
Example:Change the fill color of the path to red when the mouse enters its shape and back to black again, when it leaves its shape.
on(object)Attaches one or more event handlers to the item.
Parameters:
object:Object— an object containing one or more of the following properties:frame,mousedown,mouseup,mousedrag,click,doubleclick,mousemove,mouseenter,mouseleaveReturns:
Item— this item itself, so calls can be chained
Example:Change the fill color of the path to red when the mouse enters its shape and back to black again, when it leaves its shape.
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 black.
off(type, function)Detach an event handler from the item.
Parameters:
type:String— the type of event:‘frame’,mousedown’,‘mouseup’,‘mousedrag’,‘click’,‘doubleclick’,‘mousemove’,‘mouseenter’,‘mouseleave’function:Function— the function to be detachedReturns:
Item— this item itself, so calls can be chained
off(object)Detach one or more event handlers to the item.
Parameters:
object:Object— an object containing one or more of the following properties:frame,mousedown,mouseup,mousedrag,click,doubleclick,mousemove,mouseenter,mouseleaveReturns:
Item— this item itself, so calls can be chained
emit(type, event)Emit an event on the item.
Parameters:
type:String— the type of event:‘frame’,mousedown’,‘mouseup’,‘mousedrag’,‘click’,‘doubleclick’,‘mousemove’,‘mouseenter’,‘mouseleave’event:Object— an object literal containing properties describing the eventReturns:
Boolean—trueif the event had listeners,falseotherwise
responds(type)Check if the item has one or more event handlers of the specified type.
Parameters:
type:String— the type of event:‘frame’,mousedown’,‘mouseup’,‘mousedrag’,‘click’,‘doubleclick’,‘mousemove’,‘mouseenter’,‘mouseleave’Returns:
Boolean—trueif the item has one or more event handlers of the specified type,falseotherwise
Remove On Event
removeOn(options)Removes the item when the events specified in the passed options object occur.
Options:
options.move: undefined— {Boolean) remove the item when the nexttool.onMouseMoveevent is fired.options.drag: undefined— {Boolena) remove the item when the nexttool.onMouseDragevent is fired.options.down: undefined— {Boolean) remove the item when the nexttool.onMouseDownevent is fired.options.up: undefined— {Boolean) remove the item when the nexttool.onMouseUpevent is fired.Parameters:
options:Object
Example:Click and drag below:
removeOnMove()Removes the item when the next
tool.onMouseMoveevent is fired.Example:Move your mouse below:
removeOnDown()Removes the item when the next
tool.onMouseDownevent is fired.Example:Click a few times below:
removeOnDrag()Removes the item when the next
tool.onMouseDragevent is fired.Example:Click and drag below:
removeOnUp()Removes the item when the next
tool.onMouseUpevent is fired.Example:Click a few times below:
Tweening Functions
tween(from, to, options)Tween item between two states.
Options:
options.duration: Number— the duration of the tweeningoptions.easing: Function⟋String— an easing function or the type of the easing:‘linear’ ‘easeInQuad’ ‘easeOutQuad’ ‘easeInOutQuad’ ‘easeInCubic’ ‘easeOutCubic’ ‘easeInOutCubic’ ‘easeInQuart’ ‘easeOutQuart’ ‘easeInOutQuart’ ‘easeInQuint’ ‘easeOutQuint’ ‘easeInOutQuint’— default:‘linear’options.start: Boolean— whether to start tweening automatically — default:trueParameters:
from:Object— the state at the start of the tweeningto:Object— the state at the end of the tweeningoptions:Object⟋Number— the options or the durationReturns:
Tween
Example:Tween fillColor:
Example:Tween rotation:
tween(to, options)Tween item to a state.
Parameters:
to:Object— the state at the end of the tweeningoptions:Object⟋Number— the options or the durationReturns:
TweenSee also:
item.tween(from, to, options)
Example:Tween a nested property with relative values
tween(options)Tween item.
Parameters:
options:Object⟋Number— the options or the durationReturns:
TweenSee also:
item.tween(from, to, options)
Example:Start an empty tween and just use the update callback:
tweenTo(to, options)Tween item to a state.
Parameters:
to:Object— the state at the end of the tweeningoptions:Object⟋Number— the options or the durationReturns:
TweenSee also:
item.tween(to, options)
tweenFrom(from, options)Tween item from a state to its state before the tweening.
Parameters:
from:Object— the state at the start of the tweeningoptions:Object⟋Number— the options or the durationReturns:
TweenSee also:
item.tween(from, to, options)
Example:Tween fillColor from red to the path's initial fillColor:
Last updated
Was this helpful?