Element
The basic model for diagram elements. It inherits from dia.Cell with a few additional properties and methods specific to elements. For a quick introduction to elements, see our tutorial.
To learn about elements and their features in detail, see the Elements learn section.
constructor
constructor(
attributes?: DeepPartial<dia.Element.Attributes>,
options?: dia.Element.ConstructorOptions
);
As a descendant of dia.Cell, the dia.Element constructor accepts two arguments: attributes and options.
-
When creating an instance of an element, you can pass the initial values of its model attributes via the
attributesobject. Element-specific model attributes include geometry attributes (position,size,angle), port definitions (ports), and nesting attributes (embeds,parent). -
The
optionsargument is an object that contains options you want to set on the Element:
portLayoutNamespace
portLayoutNamespace?: { [key: string]: layout.Port.LayoutFunction };
[optional] An object mapping port layout names to their constructors, analogously to the cellNamespace Graph option and cellViewNamespace Paper option.
By default, it is defined as the layout.Port namespace, but you may add your own custom layout functions. This is important when you need to create diagrams from JSON. You can find more information in the Custom port layout section of our documentation.
import { layout, shapes } from '@joint/core';
const portLayoutNamespace = {
...layout.Port,
myPortLayout: (ports, elBBox, _opt) => {
return ports.map((_port, index) => {
const step = -Math.PI / 8;
const y = Math.sin(index * step) * 50;
return {
x: index * 12,
y: y + elBBox.height,
angle: 0
};
});
}
};
// ...
const rectangle = new shapes.standard.Rectangle({
// ...
}, {
portLayoutNamespace
});
portLabelLayoutNamespace
portLabelLayoutNamespace?: { [key: string]: layout.PortLabel.LayoutFunction };
[optional] An object mapping port label layout names to their constructors, analogously to the cellNamespace Graph option and cellViewNamespace Paper option.
By default, it is defined as the layout.PortLabel namespace, but you may add your own custom layout functions. This is important when you need to create diagrams from JSON. You can find more information in the Custom port label layout section of our documentation.
import { layout, shapes } from '@joint/core';
const portLabelLayoutNamespace = {
...layout.PortLabel,
myPortLabelLayout: (portPosition, elBBox, _opt) => ({
x: 0,
y: 10 + ((portPosition.x / elBBox.width) * 20),
angle: 0,
attrs: {
text: { textAnchor: 'middle' }
}
})
};
// ...
const rectangle = new shapes.standard.Rectangle({
// ...
}, {
portLabelLayoutNamespace
});
Methods
addPort()
element.addPort(port, [opt])
Add a single port, where port could be defined as described in section Port interface
addPorts()
element.addPorts(ports, [opt])
Add array of ports. Ports are validated for id duplicities, if there is a id collision, no new ports are added, where port could be defined as describe in section Port interface
angle()
element.angle()
Return the rotation of the element in degrees (0° - 360°).