!!!###!!!title=Layer——VisActor/VGraph tutorial documents!!!###!!!!!!###!!!description=A Layer is an abstract graphic object that can serve as a container for all graphic objects or layers, allowing you to build shapes in a tree-like structure. It does not directly participate in the rendering engine's drawing. In elements like nodes and edges, encapsulated `NodeLayer`, `EdgeLayer`, and `GroupLayer` are used to accelerate rendering and event response. In scenarios like custom nodes, `Layer` is usually used directly.!!!###!!!

Layer

A Layer is an abstract graphic object that can serve as a container for all graphic objects or layers, allowing you to build shapes in a tree-like structure. It does not directly participate in the rendering engine's drawing. In elements like nodes and edges, encapsulated NodeLayer, EdgeLayer, and GroupLayer are used to accelerate rendering and event response. In scenarios like custom nodes, Layer is usually used directly.

Example

import { Layer } from '@visactor/vgraph';

const layer = new Layer({ id: 'parent' });
const image = new Image({
    left: 300,
    top: 100,
    width: 164,
    height: 51,
    url: 'https://url/to/img.svg',
});
layer.add(image);

Basic Configuration

FieldTypeDescription
type'node' | 'edge' | 'group'Specifies the layer type as a node layer, link layer, or group layer.
xnumberThe x-coordinate of the node's center (valid for NodeLayer).
ynumberThe y-coordinate of the node's center (valid for NodeLayer).
widthnumberThe width of the layer (valid for NodeLayer).
heightnumberThe height of the layer (valid for NodeLayer).
appendSizenumber | number[]The extended width in four directions for event identification. If it is a 4-dimensional array, it represents the extended width of the top, right, bottom, and left sides in order (valid for NodeLayer).
hitWidthnumberThe event trigger identification width at the edge of the layer (valid for NodeLayer).
paddingnumber | number[]The extended width in four directions for event identification in a group layer. If it is a 4-dimensional array, it represents the extended width of the top, right, bottom, and left sides in order (valid for GroupLayer).
sourceShapeBaseThe starting key shape of the link (valid for EdgeLayer).
targetShapeBaseThe ending key shape of the link (valid for EdgeLayer).

Layer Instance Methods

Instance MethodReturn ValueDescription
add(shape: ShapeBase)voidAdds a child shape to the layer.
addBefore(shape: ShapeBase, beforeShape: ShapeBase)voidAdds a child shape before a specified child shape in the layer.
remove(shape: ShapeBase)voidRemoves the specified child shape.
findById(id: string)ShapeBase | nullQueries a child shape by ID. If found, it returns the shape; otherwise, it returns null.
find(fn: () => {})ShapeBase | nullQueries the first child shape that meets a custom function's criteria. If found, it returns the shape; otherwise, it returns null.
findAll(fn: () => {})ShapeBase[] | nullQueries all child shapes that meet a custom function's criteria. If found, it returns them; otherwise, it returns null.
shouldDraw(bbox: BBox)booleanDetermines whether this layer needs to be drawn within the specified BBox. If so, it returns true; otherwise, it returns false.
getBBoxForHit()BBoxGets the BBox of this layer used for event triggering.
clear()voidClears all child shapes within this layer.
destroy()voidRemoves this layer.

Accessible Properties

PropertyTypeDescription
childrenShapeBase[]An array of all child nodes of this layer.
typestringThe type of this layer.
capturebooleanWhether it is captured by focus.
animatingbooleanWhether it is an animation.
destroyedbooleanWhether it has been destroyed.
parentanyParent shape node.

Common Configurations

The following are common configurations for all graphic objects:

FieldTypeDescription
opacitynumberOpacity
fillStylestringFill color
strokeStylestringStroke color
lineWidthnumberStroke width
shadowBlurnumberShadow blur amount, refer to CanvasRenderingContext2D.shadowBlur
shadowColorstringShadow color
shadowOffsetXnumberHorizontal shadow offset, refer to CanvasRenderingContext2D.shadowOffsetX
shadowOffsetYnumberVertical shadow offset, refer to CanvasRenderingContext2D.shadowOffsetY
clipShapeBaseClipping shape. Sets the specified shape as the clipping path. The part within the clipping shape's path will be retained, and the rest will be culled. Refer to CanvasRenderingContext2D.clip()
cursorstringThe type of cursor when this shape gains focus. For details, refer to CSS Cursor

Note: Color strings currently support the following formats:

TypeExampleDescription
rgbrgb(255, 255, 255)White represented in RGB mode
rgbargba(255, 255, 255, 1.0)White represented in RGBA mode
hslhsl(13, 100%, 10%)Color represented in HSL mode, equivalent to rgb(56, 12, 0)
hslahsla(13, 100%, 10%, 0.4)Color represented in HSL mode, equivalent to rgba(56, 12, 0, 0.4)
hex#fffPure color represented by a three-digit hexadecimal number, case-insensitive
hex#FF00FFPure color represented by a six-digit hexadecimal number, case-insensitive
hex#3037ffffColor with transparency represented by an eight-digit hexadecimal number, case-insensitive
namecyanColor represented by its name
linear gradientl(0) 0:#fff 0.5:#7ec2f3 1:#1890ffLinear gradient, the expression is l(deg) step1:color1 step2:color2 ..., which means a linear gradient with a linear tilt angle of deg, and the colors are interpolated at step1, step2... with color1, color2...
radial gradientr(0.5,0.5,0) 0:#fff 0.5:#7ec2f3 1:#1890ffRadial gradient, the expression is r(cx, cy, r0) r1:color1 r2:color2 ..., which means a radial gradient centered at the relative coordinate (cx, cy) within the bounding box, with a starting radius r0, and the colors are interpolated at radii r1, r2... with color1, color2...
picturep(a)./pattern.pngImage, the expression is p(repeat)url. When repeat is a, it means the background repeat mode is repeat; when it is x, it means repeat-x; when it is y, it means repeat-y; when it is n, it means no-repeat. The parameter url represents the image path. Refer to CSS background-repeat

Instance Methods

Instance MethodReturn ValueDescription
getBBox()BBoxGets the bounding box position and size
translate(x: number, y: number)voidTranslates
scale(xRatio: number, yRatio?: number)voidScales
rotate(deg: number, radian: boolean)voidRotates around the center. radian is used to specify whether the parameter deg is in radians. If it is true, it is in radians; otherwise, it is in degrees. The default is false.
rawRotate(deg: number, radian: boolean)voidRotates based on the matrix origin, with the same definition as rotate
clone()IShapeGets a copy of a shape
show()voidShows the shape
hide()voidHides the shape
get(k: string)anyGets the property named k of this shape
set(k: string, v: any)IShapeSets the value of the property named k of this shape to v
set(data: Record<string, any>)IShapePasses an object to set properties in bulk
on(eventName: string, callback: () => void)voidSets a trigger for when an event occurs
off(eventName: string, callback: () => void)voidSets a trigger for when an event stops
emit(eventName: string, data: Record<string, any>)voidTriggers the specified event