let canvas = new Canvas({
// the attribute below
root: dom, // canvas root dom (require)
layout: 'ForceLayout', // layout setting , integrated or custom , (optional)
zoomable: true, // enable zoom canvas (optional)
moveable: true, // enable move canvas (optional)
draggable: true, // enable drag nodes (optional)
linkable: true, // enable connect edges (optional)
disLinkable: true, // enable disConnect edges (optional)
layout: {}, // initialize auto layout (optional)
theme: { // theme (optional)
group: {
type: 'normal' // Node group type: normal (drag in and drag out), inner (can only be dragged in and not out)
},
edge: {
type: 'Bezier', // edge type:Bezier curve,Polyline ,Straight,Manhattan line,Improved Bezier curve。values : Bezier/Flow/Straight/Manhattan/AdvancedBezier
label: 'test', // edge label
arrow: true, // whether to show arrow
arrowPosition: 0.5, // arrow position (0 ~ 1)
arrowOffset: 0.0, // arrow offset
arrowShapeType: '', // custom arrow style
Class: XXClass, // custom Class
isExpandWidth: false,// expand line interaction area
defaultAnimate: false// turn on line animation by default
},
endpoint: {
position: [], // limit endpoint position ['Top', 'Bottom', 'Left', 'Right'],
linkableHighlight: true,// point.linkable method is triggered when connecting, can be highlighted
limitNum: 10, // limit the number of anchor connections
expandArea: { // when the anchor point is too small, the connection hot zone can be expanded.
left: 10,
right: 10,
top: 10,
botton: 10
}
},
zoomGap: 0.001, // mouse zoom in and out gap settings
autoFixCanvas: { // auto expand canvas when drag nodes or edges near the edge of canvas.
enable: false,
autoMovePadding: [20, 20, 20, 20]
},
autoResizeRootSize: true // automatically adapt to the root size, the default is true
},
global: { // custom configuration, will run through all canvas, group, node, edge, endpoint objects
isScopeStrict: false // whether scope is strict mode (default is false)
}
});
canvas.draw({
// data
})
container: a dom element with width and height, canvas root node
whether the canvas is scalable; value type boolean
, Default value false
whether the canvas is movable; value type boolean
, Default value false
whether the canvas is draggable; value type boolean
, Default value false
whether the nodes in canvas can be dragged to add connection; value type boolean
, Default value false
whether the nodes in canvas can be dragged to delete connection; value type boolean
, Default value false
canvas initialization automatically arranges the layout according to what you set, reference
canvas theme configuration, default initialization style and interaction, mainly:
-
edge connection configuration: Default style and interaction of all edges. The dragged edge will also use this configuration.
params:
-
type
<String>
whether the edge is connected to a node or to an endpoint. Default valuenode
-
shapeType
<String>
edge type: Bezier, AdvancedBezier, Flow, Straight, Manhattan, Bezier2-1, Bezier2-2, Bezier2-3, BrokenLine; Default valueStraight
-
label
<String/Dom>
edge label -
labelPosition
<Number>
edge label position: the value is between 0-1, 0 represents the beginning of the egde, and 1 represents the end of the egde. Default value0.5
-
labelOffset
<Number>
the position offset of edge label: the offset value from the label position of edge. The default value is 0, and the unit ispx
// labelPosition & labelOffset: the label position is in the middle of edge,offset 20px to the end { labelPosition: 0.5, labelOffset: 20 }
-
arrow
<Boolean>
whether to add arrow configuration: Default valuetrue
-
arrowPosition
<Number>
arrow position: value between 0-1, 0 represents the beginning of the edge, and 1 represents the end of the edge. Default value0.5
-
arrowOffset
<Number>
the position offset of arrow: the offset value from the arrow position of edge. The default value is 0, and the unit ispx
-
isExpandWidth
<Boolean>
expand the edge interaction area, default valuefalse
. Iftrue
, geteventHandlerDom
for attach events -
defaultAnimate
<Boolean>
_ turn on line animation; Default valuefalse
-
Class
<Class>
custom extend class
-
-
endpoint endpoint config: default style and interaction of all endpoints
params:
-
linkableHighlight
<Boolean>
point.linkable
method will be triggered when connecting, which can be used for line highlighting; Defualt valuetrue
-
limitNum
<Number>
limit the number of endpoing connections; Default value10
-
expandArea
<Object>
the hot zone connected of endpoint: Since the endpoint area may be too small, it provides the property of expanding the hot zone;Default value{left: 10, top: 10, right: 10, bottom: 10}
-
-
group group config
params:
-
type
<String>
group type:normal
(can be dragged in and out),inner
(can only be dragged in but not out);Default valuenormal
-
includeGroups
<Boolean>
whether the node group allows included node groups
-
-
zoomGap
<Number>
mouse zoom in and out gap setting; Value between[0-1], Defualt0.001
-
autoFixCanvas When the node is dragged or the edge is dragged to around the edge of the canvas, the canvas is automatically extended
params:
-
enable
<Boolean>
whether the canvas is automatically extended; Default valuefalse
-
autoMovePadding
<Array>
inner margin of the canvas that triggers automatic extension; Default value[20,20,20,20]
-
- autoResizeRootSize
<Boolean>
Automatically adapt to the Root container size; Default valuetrue
global Global attributes; object (Option)
, Default value undefined
descripition:the rendering method of the canvas, please note that the canvas rendering is asynchronous rendering
params
{object} data
include: groups, nodes, edges{function} calllback
*The rendering process is asynchronous, please pay attention to the callback if you need it
draw = (data, calllback) => {}
descripition:the re-rendering method will delete all previous elements and re-render, note that the canvas rendering is asynchronous rendering
params
{object} data
new groups, new nodes and new edges{function} calllback
*The rendering process is asynchronous, please pay attention to the callback if you need it
redraw = (data, calllback) => {}
descripition:get all the data of the canvas: nodes, edges, groups
return
{object} data
groups, nodes, and edges data
getDataMap = () => {}
descripition:set whether all nodes of the canvas can be dragged to connect edge
params
{true|false} boolean
whether to support all nodes can be dragged to connect edge
setLinkable = (boolean) => {}
descripition:set whether all nodes of the canvas can be disconnected connecttion
params
{true|false} boolean
whether all nodes of the canvas can be disconnected connecttion
setDisLinkable = (boolean) => {}
descripition:set whether all nodes of the canvas can be dragged
params
{true|false} boolean
whether to support all nodes can be dragged
setDraggable = (boolean) => {}
descripition:get group by id
params
{string} id
group id
return
{Group}
Group instance
getGroup = (string) => {}
canvas.addGroup (object | Group, nodes, options)
descripition:Add groups. If the group does not exist, create the group and add nodes into the group; If the group exists, nodes will be add into the current group.
params
{object | Group} object
Group: group information or group instance{array< object | Node >} object
(Optional) Node information: these nodes will be added to the group. If the node does not exist, it will create these nodes{object} options
params{string} options.posType
'absolute or relative' , Identifies whether the coordinates of the node are absolute positioning relative to the canvas or relative to the node group{number} options.padding
group padding
addGroup = (object|Group, nodes, options) => {}
This API can not only create new groups, but also select multiple nodes into groups:
descripition delete the group, but not delete the nodes inside
params
{string | Group} id
group id / Group instance
return
{Group}
delete Group instance
removeGroup = (string | Group) => {}
descripition:get node by id
params
{string} id
node id
return
{Node}
Node instance
getNode = (string) => {}
descripition:add node
params
{object|Node} object
node infomation;Node - Node Basic Class
addNode = ([object](./node.md#node-attr) | Node) => {}
canvas.addNodes (array< object | Node >)
descripition:add multiple nodes
params
{array<object|Node>}
Node infomation;Node - Node Basic Class
addNodes = (array<object|Node>) => {}
descripition:delete node
params
nodeId string
- node id
removeNode = (string) => {}
descripition:delete multiple node
params
nodeIds array
- multiple node id
removeNodes = (array) => {}
canvas.addEdge ( object | Edge )
descripition:add edge
params
{object|Edge} object
- Edge infomation;Edge - Edge Basic Class
addEdge = (object|Edge) => {}
canvas.addEdges (array< object | Edge >)
descripition:add multiple edge
params
{array<object | Edge>}
- Edge infomation;Edge - Edge Basic Class
addEdges = (array<object|Edge>) => {}
descripition:delete edge by id or Edge instance
params
{string | Edge} id or Edge
- edge id or Edge instance
return
{Edge}
- delete Edge instance
removeEdge = (param) => {}
descripition:delete multiple edge by id or Edge instance
params
{array} string or Edge
- edge id array or Edge instance array
return
{array} Edge
- delete Edge instance array
removeEdges = (param) => {}
descripition:get neighbor edges by node id
params
{string} nodeId
- node id
return
{Edges}
- Edge instance array
getNeighborEdges = (string) => {}
descripition:get neighbor edges by endpoint id
params
{string} nodeId
- node id{string} endpointId
- endpoint id
return
{Edges}
- Edge instance array
getNeighborEdgesByEndpoint = (string, string) => {}
descripition:find N-level association nodes and edges
params
{Object} options
- parameters{Node} options.node
- options.node - starting node{Endpoint} options.endpoint
- options.endpoint - starting endpoint(Optional){String} options.type
- find direction , optional value all\in\out, default valueall
(Optional){Number} options.level
- level,starting level is 0 level , default valueInfinity
{Function} options.iteratee
- whether to continue traversing the decision function, return boolean value
return
{Object<nodes: Node, edges: Edge>} filteredGraph
- filteredGraph - lookup result
getNeighborNodesAndEdgesByLevel = (options) => {}
descripition:set edge z-index attribute
params
{Array<Edge>} edges
- edges array{number} zIndex
- z-index
setEdgeZIndex = (edges, zIndex) => {}
descripition:set whether the canvas can be zoomable
params
{true|false} boolean
- whether the canvas can be zoomable{true|false} boolean
- the direction of zoom。Now it defaults to the two finger direction of MAC, but it is opposite to the mouse wheel direction of Window. Default value: false. If true, the direction is opposite
setZoomable = (boolean, boolean) => {}}
descripition: set whether the canvas can be movable by dragging blank area
params
{true|false} boolean
- whether the canvas can be movable
setMoveable = (boolean) => {}
descripition:set canvas offset
params
{[x, y]} array
- x,y
move = (postion) => {}
descripition:set canvas zoom value
params
{float} scale
- zoom value between 0-1{function} callback
- zoom callback
zoom = (scale) => {}
descripition:get canvas zoom value
return
{float}
- zoom value between 0-1
getZoom = () => {}
descripition:get canvas offset value which by dragging canvas
return
{[x, y]}
- offset value
getOffset = () => {}
descripition:get the center point of the canvas scaling, generally following the position of the mouse
return
{[x, y]}
- the center point of the canvas's zoom (percentage)
getOrigin = () => {}
descripition:set the center point of the canvas zoom, generally follow the position of the mouse
params
{[x, y]} array
- the center point of the canvas's zoom (percentage)
setOrigin = ([x ,y]) => {}
descripition:focus on some node or group
params
{string} nodeId/groupId
- node/group id{string} type
- type,node
orgroup
{object} options {offset: [0,0]}
- focus attribute , such as offset{function} callback
- finish callabck
focusNodeWithAnimate = (string, type, options, callback) => {}
descripition:focus on multiple node/ group
params
{object} {nodes: [], groups: []}
- node/group id array{array} type
- type,node
orgroup
{object} options {offset: [0,0]}
- focus attribute , such as offset{function} callback
- finish callabck
focusNodesWithAnimate = (objs, type, options, callback) => {}
descripition:focus on the entire canvas and automatically adjust the position and scale of the canvas
params
{object} options {offset: [0,0]}
- focus attribute , such as offset{function} callback
- finish callabck
focusCenterWithAnimate = (options, callback) => {}
descripition:redo
redo = () => {}
descripition:undo
undo = () => {}
descripition:add the topmost element to the action queue (undo / redo queue)
params
{Object} options
- params{String} options.type
- element type{Object} options.data
- element data
pushActionQueue = (options) => {}
descripition:delete the topmost element from the action queue (undo / redo queue)
popActionQueue = (options) => {}
descripition:delete all elements from the action queue (undo / redo queue)
clearActionQueue = (options) => {}
descripition:onvert the coordinates from screen to canvas
params
{array<number>} coordinates
- origin coordinates([x,y])
return
{number}
- converted coordinates
terminal2canvas = (coordinates) => {}
descripition:convert the coordinates from canvas to screen
params
{array<number>} coordinates
- origin coordinates([x,y])
return
{number}
- converted coordinates
canvas2terminal = (coordinates) => {}
descripition
- As shown in the figure, the canvas is scaled, and the coordinates after the movement do not match the coordinates of the original canvas. This method is needed to convert. Special Note: Users who drag and drop nodes pay attention to these two
e.clientX
ande.clientY
, and need to call this method to convert.
descripition: set select mode: Note that select mode and normal drag canvas mode are mutually exclusive and cannot be set at the same time
params
{true|false} boolean
- enable multiple select{array} contents
- accept select contents(node/endpoint/edge, defaultnode
){string} selecMode
- accept selec mode(include|touch|senior),default 'include',include:You can select only if the element all included; touch: You can select only if you touch the element; senior: needs to include all from left to right,select only touch from right to left)
setSelectMode = (boolean, contents , selecMode) => {}
descripition:get union by name
params
{name} string
- union name
getUnion = (name) => {}
descripition:get all unions
getAllUnion = () => {}
descripition:add some union or add union item , used in multiple selection mode
params
{name} string
- union name.If not exist, add a new union; if it already exists, add item to union{obj} object
- union item
add2Union = (name, obj) => {}
this.canvas.add2Union('my union', {
nodes: [] // Node instance or nodeId
groups: [] // Group instance or groupId
edges: [] // Edge instance or edgeId
endpoints: [] // Endpoint instance
});
descripition:remove union
params
{name} string
- union name
removeUnion = (name) => {}
descripition:remove all union
removeAllUnion = () => {}
let canvas = new Canvas({...});
canvas.on('type key', (data) => {
//data
});
event key
system.canvas.click
click on the blank space of the canvas eventsystem.canvas.zoom
canvas zoom eventsystem.nodes.delete
delete node eventsystem.node.move
move node eventsystem.nodes.add
add multiple nodes eventsystem.links.delete
delete edge eventsystem.link.connect
connect edge eventsystem.link.reconnect
edge reconnect eventsystem.link.click
click edge eventsystem.group.add
add group eventsystem.group.delete
delete group eventsystem.group.move
move group eventsystem.group.addMembers
add node to group eventsystem.group.removeMembers
delete node from group eventsystem.multiple.select
multiple select callback eventsystem.drag.start
drag start eventsystem.drag.move
drag move eventsystem.drag.end
drag end event
/**
* emit events
*/
emit = (string, obj) => {}
/**
* accept events
*/
on = (string, callback) => {}
descripition:set the grid background
params
{true|false} boolean
- whether to open{array} options
- parameters for grid background
setGridMode = (show, options) => {}
this.canvas.setGridMode(true, {
isAdsorb: false, // Whether to automatically adsorb, default value is false
theme: {
shapeType: 'line', // show type,support line & circle
gap: 23, // grid gap
adsorbGap: 8, // adsorb gap
backgroud: '#fff', // grid backgroud
lineColor: '#000', // grid line color
lineWidth: 1, // grid line thickness
circleRadiu: 1, // grid circle radiu
circleColor: '#000' // grid circle color
}
});
descripition:enable minimap
params
{true|false} boolean
- whether to enable minimap{Object}
please refer to the minimap document for details
setMinimap = (show, options) => {}
descripition:save canvas to iamge
params
{object} options
- saved image parameters (Optional){string} options.type
- image type (png/jpeg/svg , default png) , (Optional){number} options.quality
- image quality (0~1 , default 1) , (Optional){number} options.width
- image width (default canvas width) , (Optional){number} options.height
- image height (default canvas height) , (Optional)
return
{Promise}
save2img = (options) => {}
this.canvas.save2img({type: 'png', width: 1920, height: 1080, quality: 1})
.then(dataUrl => {
var link = document.createElement('a');
link.download = 'XXX.png';
link.href = dataUrl;
link.click();
});
descripition:automatically align nodes / groups on the canvas(must be effective under the grid background)
justifyCoordinate = () => {}
descripition:set guide line
params
{true|false} boolean
- whether to open{array} options
- parameters for guide line
setGuideLine = (show, options) => {}
this.canvas.setGuideLine(true, {
limit: 1, // limit guide line number
theme: {
lineColor: 'red', // guide line color
lineWidth: 1, // guide line thickness
}
});
descripition:need to update location when root canvas moves or size changes
updateRootResize = () => {}