Project
A Project object in Paper.js is what usually is referred to as the document: The top level object that holds all the items contained in the scene graph. As the term document is already taken in the browser context, it is called Project.
Projects allow the manipulation of the styles that are applied to all newly created items, give access to the selected items, and will in future versions offer ways to query for items in the scene graph defining specific requirements, and means to persist and load from different formats, such as SVG and PDF.
The currently active project can be accessed through the paperScope.project variable.
An array of all open projects is accessible through the paperScope.projects variable.
Constructors
Creates a Paper.js project containing one empty Layer, referenced by project.activeLayer.
Note that when working with PaperScript, a project is automatically created for us and the paperScope.project variable points to it.
- element: HTMLCanvasElement⟋String⟋Size — the HTML canvas element that should be used as the element for the view, or an ID string by which to find the element, or the size of the canvas to be created for usage in a web worker.
Parameters:
- Project
Returns:
Properties
The currently active path style. All selected items and newly created items will be styled with this style.
- Style
Type:
Example:
Example:
Project Content
The layer which is currently active. New items will be created on this layer by default.
Read only.
- Layer
Type:
The symbol definitions shared by all symbol items contained place ind project.
Read only.
- Array of SymbolDefinition objects
Type:
Methods
Activates this project, so all newly created items will be placed in it.
Clears the project by removing all project.layers.
Checks whether the project has any content or not.
- Boolean
Returns:
Removes this project from the paperScope.projects list, and also removes its view, if one was defined.
Selects all items in the project.
Deselects all selected items in the project.
Hierarchy Operations
Hit-testing, Fetching and Matching Items
Performs a hit-test on the items contained within the project at the location of the specified point.
The options object allows you to control the specifics of the hit-test and may contain a combination of the following values:
- options.tolerance: Number — the tolerance of the hit-test — default: paperScope.settings.hitTolerance
- options.class: Function — only hit-test against a specific item class, or any of its sub-classes, by providing the constructor function against which an
instanceof
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: Return
true
to return the result,false
to keep searching - options.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 for segment.point of Path items — default: true
- options.curves: Boolean — hit-test the curves of path items, without taking the stroke color or width into account
- options.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 items
- options.position: Boolean — hit-test the item.position of of items, which depends on the setting of item.pivot
- options.center: Boolean — hit-test the rectangle.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 have Item#guide set to
true
- options.selected: Boolean — only hit selected items
Options:
- point: Point — the point where the hit-test should be performed
- options: Object — optional, default: { fill: true, stroke: true, segments: true, tolerance: settings.hitTolerance }
Parameters:
-
HitResult — a hit result object that contains more information about what exactly was hit or
null
if nothing was hit
Returns:
Performs a hit-test on the item and its children (if it is a Group or Layer) 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.
- point: Point — the point where the hit-test should be performed
- options: Object — optional, default: { fill: true, stroke: true, segments: true, tolerance: settings.hitTolerance }
Parameters:
-
Array of HitResult objects — hit result objects for all hits, describing what exactly was hit or
null
if nothing was hit
Returns:
- hitTest(point[, options]);
See also:
Fetch items contained within the project whose properties match the criteria in the specified object.
Extended matching of properties is possible by providing a comparator 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.inside
or options.overlapping
to a rectangle describing the area in which the items either have to be fully or partly contained.
- options.recursive: Boolean — whether to loop recursively through all children, or stop at the current level — default: true
- options.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 the
match
object - options.class: Function — the constructor function of the item type to match against
- options.inside: Rectangle — the rectangle in which the items need to be fully contained
- options.overlapping: Rectangle — the rectangle with which the items need to at least partly overlap
Options:
- options: Object⟋Function — the criteria to match against
Parameters:
- Array of Item objects — the list of matching items contained in the project
Returns:
- item.matches(options)
- item.getItems(options)
See also:
Example:Fetch all selected path items:
Example:Fetch all items with a specific fill color:
Example:Fetch items at a specific position:
Example:Fetch items using a comparator function:
Example:Fetch items using a comparator function (2):
Example:Match (nested) properties of the data property:
Example:Match strings using regular expressions:
Fetch the first item contained within the project whose properties match the criteria 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.
See getItems(options) for a selection of illustrated examples.
- options: Object⟋Function — the criteria to match against
Parameters:
- Item — the first item in the project matching the given criteria
Returns:
Importing / Exporting JSON and SVG
Exports (serializes) the project with all its layers and child items to a JSON data object or string.
- options.asString: Boolean — whether the JSON is returned as a
Object
or aString
— default: true - options.precision: Number — the amount of fractional digits in numbers used in JSON data — default: 5
Options:
- options: Object — the serialization options — optional
Parameters:
- String — the exported JSON data
Returns:
Imports (deserializes) the stored JSON data into the project. Note that the project is not cleared first. You can call project.clear() to do so.
- json: String — the JSON data to import from
Parameters:
- Item — the imported item
Returns:
Exports the project with all its layers and child items as an SVG DOM, all contained in one top level SVG group node.
- options.bounds: String⟋Rectangle — the bounds of the area to export, either as a string (‘view’, content’), or a Rectangle object:
'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: If
options.bounds
is set to'view'
,paper.view.matrix
is used, for all other settings ofoptions.bounds
the identity matrix is used. — default: paper.view.matrix - options.asString: Boolean — whether a SVG node or a
String
is to be returned — default: false - options.precision: Number — the amount of fractional digits in numbers used in SVG data — default: 5
- options.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: false
- options.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: true
Options:
- options: Object — the export options — optional
Parameters:
-
SVGElement⟋String — the project converted to an SVG node or a
String
depending onoption.asString
value
Returns:
Converts the provided SVG content into Paper.js items and adds them to the active layer of this project. Note that the project is not cleared first. You can call project.clear() to do so.
- options.expandShapes: Boolean — whether imported shape items should be expanded to path items — default: false
- options.onLoad: Function — the callback function to call once the SVG content is loaded from the given URL receiving two arguments: the converted
item
and the originalsvg
data 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 project that
importSVG()
is called on — default: true - options.applyMatrix: Boolean — whether the imported items should have their transformation matrices applied to their contents or not — default: paperScope.settings.applyMatrix
Options:
- 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 — optional
Parameters:
- Item — the newly created Paper.js item containing the converted SVG content
Returns:
Imports the provided external SVG file, converts it into Paper.js items and adds them to the active layer of this project. Note that the project is not cleared first. You can call project.clear() to do so.
- 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 converted
item
and the originalsvg
data as a string. Only required when loading from external files.
Parameters:
- Item — the newly created Paper.js item containing the converted SVG content