Paper
Paper is the core of the Psiagram framework. It provides somewhere to input your settings, and an API to manipulate your Paper element once it's mounted in the DOM.
You can control the Paper using the following:
The PaperProperties passed into Paper during initialization
The API of the Paper object (methods and listeners)
This documentation will take a look into some of the basics of setting up and using Paper.
Paper Set-Up
In order to use Paper, you need to initialize it. To do this, you will create a new instance of Paper and give it a Paper Properties object. This contains some mandatory properties, as well as many optional ones that allow elements and settings to be added to the Paper during initialization.
Paper Properties
Here is the Paper Properties interface defined in TypeScript:
This defines the shape of the object that must be given to initialize Paper. Let's take a look into what each of those potential properties mean.
width - number
number
This is the physical width of the Paper surface in pixels. It is one of the two required properties to initialize Paper.
height - number
number
This is the physical height of the Paper surface in pixels. Like width, it is required to initialize Paper.
plugins (optional) - PsiagramPlugin[]
PsiagramPlugin[]
attributes (optional) - Object
Object
An optional object where you can define attributes of the Paper. These are all optional, and are the following:
gridSize -
number
: Grid size in px. If given, elements will snap to a grid.paperWrapperClass -
string
: Class for div that encompasses Paper.paperClass -
string
: Class for Paper SVG component.uniqueId -
string
: Unique ID for paper (self-generated if none given).
initialConditions (optional) - Object
Object
An optional object where you can define any initial Nodes or Edges to render onto Paper when it's initialized. In order to input these, object has the following optional properties:
nodes -
IPaperInputNode[]
: Initial Node data needed to render out Nodes.nodeComponentMap -
INodeComponentMap
: Object to map component strings to anextended BaseNode class. Once you've built some Nodes, they can be included
here and selected by giving the key string to the appropriate initial Nodes.
edges -
IPaperInputEdge[]
: Initial Edge data needed to render out Edges.edgeComponentMap -
IEdgeComponentMap
: Object to map component strings to anextended Edge class. Once you've built some Edges, they can be included here
and selected by giving the key string to the appropriate initial Edges.
Initialization
Now that you have an idea of building a Paper Properties object, let's look at initializing a Paper instance.
Congratulations! You now have a fully functional Paper component initialized to myPaper. While you are able to call methods and make updates to this component, the actual paper element has not yet been rendered into the DOM, so those changes won't be reflected anywhere. In order to render the Paper, you may choose to do something like this:
In the next section we will discuss manipulating the Paper using its methods.
Paper Methods
Now that you have your Paper mounted in the DOM, you can manipulate it. While Psiagram would work just fine as a static diagram, it really shines in the many ways you can move or alter the elements drawn in Paper.
Let's examine some of the methods that you can call on your Paper instance.
getPaperElement
Returns the Paper wrapper, which contains the Paper SVG element, as well as all rendered components. This is the method you would call to retrieve the wrapper to render into your DOM. Here's an example of how to mount your paper.
addNode
getNode
removeNode
Remove a Node from the Paper with the given ID.
addEdge
getEdge
removeEdge
Remove an Edge from the Paper that matches the given ID.
getActiveItem
updateActiveItem
addListener
removeListener
Last updated