attributes
attributes​
The attributes in JointJS define how the graphics elements are to be rendered inside of the element and link views. All the standard SVG styling properties are available (both kebab-case and camelCase styles).
The JointJS ready-to-use shapes use the camelCase style for attribute names.
Only one style should be used within an application. Mixing styles can lead to unexpected behavior.
// Setting attributes on the model
link.attr('line/stroke-width', 2);
link.attr('line/strokeWidth', 3);
// Both attributes are set.
// It's not clear which attribute will be used
link.attr('line'); // => { stroke-width: 2, strokeWidth: 3 }
In addition JointJS modifies the behavior of existing attributes (the use of calc() for specifying attribute values) and defines new so-called "special" attributes and allows programmers to define their own.
Here is the list of all built-in attributes in the attributes namespace.
atConnectionLengthIgnoreGradient​
Valid within the LinkView context.
Move the subelement to the point at a given distance along the connection path but do not auto-orient it according to the path's gradient. Use a positive number to define the distance from the source of the link and a negative number from the target of the link.
link.attr('rectSelector', { atConnectionLengthIgnoreGradient: 30, width: 10, height: 10, fill: 'red' });
atConnectionLengthKeepGradient​
alias: atConnectionLength
Valid within the LinkView context.
Move and auto-orient the subelement to the point at a given distance along the connection path. Use a positive number to define the distance from the source of the link and a negative number from the target of the link
link.attr('rectSelector', { atConnectionLength: 30, width: 10, height: 10, fill: 'red' });
link.attr('rectSelector', { atConnectionLengthKeepGradient: 30, width: 10, height: 10, fill: 'red' });
atConnectionRatioIgnoreGradient​
Valid within the LinkView context.
Move the subelement to the point at a given ratio of the connection total length but do not auto-orient it according to the path's gradient. It accepts a number in the [0,1] range.
link.attr('rectSelector', { atConnectionRatioKeepGradient: .5, width: 10, height: 10, fill: 'red' });
atConnectionRatioKeepGradient​
alias: atConnectionRatio
Valid within the LinkView context.
Move and auto-orient the subelement to the point at a given ratio of the connection total length. It accepts a number in the [0,1] range.
link.attr('rectSelector', { atConnectionRatio: .5, width: 10, height: 10, fill: 'red' });
link.attr('rectSelector', { atConnectionRatioKeepGradient: .5, width: 10, height: 10, fill: 'red' });
connection​
applies to: <path/>
Valid within the LinkView context.
If true, set the shape of the subelement to match the shape of the LinkView (set the 'd' attribute to the result of the link connector).
You can also provide an object with the following options:
| Name | Type | Description |
|---|---|---|
| stubs | Number | If provided, display only the beginning and end stubs of the connection and hide the remaining central section. A positive value determines the length of each stub. If the value is negative, it determines the length of the hidden section of the connection between the two stubs. |
link1.attr('pathSelector', { connection: true, stroke: 'red', fill: 'none' });
link2.attr('pathSelector', { connection: { stubs: -20 }});
containerSelector​
Designate another subelement of the cell as a proxy target for the cell's container, whenever this subelement (the one on which this attribute is defined) becomes the target of an embedding. (Usually, this attribute needs to be defined on the `'root'` subelement of the cell, since that is the default target for the cell's container.) Expects a selector.
A change in the cell's container affects the following characteristics of the cell view:
- embedding highlighter
// `standard.Rectangle` has `root`, `body` and `label` subelements
const rect = new shapes.standard.Rectangle();
// we want to have the `label` on the outside and under the `body` of the rectangle:
rect.attr(['label'], { y: 'calc(h + 10)', textVerticalAnchor: 'top' });
// normally, when a user tries to embed a child to the rectangle,
// the `root` subelement would be highlighted (the wrapper of `body` and `label`)
// however, we want only the `body` to be highlighted,
// so we need to specify it as a proxy for `root`:
rect.attr(['root', 'containerSelector'], 'body');
cx​
applies to: <circle/>, <ellipse/>
The same as the SVG cx attribute, but it can be a calc() expression.
{ cx: 'calc(w / 2)' }
cy​
applies to: <circle/>, <ellipse/>
The same as the SVG cy attribute, but it can be a calc() expression.
{ cy: 'calc(h / 2)' }
d​
applies to: <path/>
The same as the SVG d attribute, but it can be a calc() expression.
{ d: 'M 0 0 calc(w) 0 calc(h) 0 z' }
displayEmpty​
applies to: <text/>
If set to true, the SVGTextElement with empty content will be rendered. It's useful when the text needs to be editable.
event​
The event attribute makes the selected node and its descendants trigger an arbitrary event when clicked (mousedown/touchstart). This event is triggered on the view itself and the paper. The paper handler is called with the signature cellView, evt, x, y, while the cell view handler is called only with evt, x, y.
element.attr({
image: {
// pointerdown on the image SVG node will trigger the `element:delete` event
event: 'element:delete',
xlinkHref: 'trash.png'
width: 20,
height: 20
}
});
// Binding handler to the event
paper.on('element:delete', function(elementView, evt) {
// Stop any further actions with the element view e.g. dragging
evt.stopPropagation();
if (confirm('Are you sure you want to delete this element?')) {
elementView.model.remove();
}
});
fill​
applies to: <circle/>, <ellipse/>, <path/>, <polygon/>, <polyline/>, <rect/>, <text/>
The fill attribute becomes a special attribute only in case it's defined as an object, instead of the usual SVG syntax
(e.g. "#ffaabb"). If it's defined as an object, it is assumed to be either a gradient or pattern definition. It must
have the form defined by the defineGradient()
(resp. definePattern()) paper method.
Gradient​
element.attr('rect/fill', {
type: 'linearGradient',
stops: [
{ offset: '0%', color: '#E67E22' },
{ offset: '20%', color: '#D35400' },
{ offset: '40%', color: '#E74C3C' },
{ offset: '60%', color: '#C0392B' },
{ offset: '80%', color: '#F39C12' }
]
});
Pattern​
element.attr('rect/fill', {
type: 'pattern',
attrs: {
width: 10,
height: 10
},
markup: util.svg`<polygon points="0,0 2,5 0,10 5,8 10,10 8,5 10,0 5,2"/>`
});
filter​
The filter attribute becomes a special attribute only in case it's defined as an object, instead of the usual SVG syntax (e.g. "url(#myfilter)"). If it's defined as an object, it must have the form defined by the defineFilter() paper method.
element.attr('rect/filter', {
name: 'dropShadow',
args: {
dx: 2,
dy: 2,
blur: 3
}
});
height​
applies to: <rect/>, <image/>, <foreignObject/>
The same as the SVG height attribute, but it can be a calc() expression.
{ height: 'calc(h + 10)' }
highlighterSelector​
Designate another subelement of the cell as a proxy target for the cell's magnet, whenever this subelement (the one on which this attribute is defined) becomes the source/target of a link. (Usually, this attribute needs to be defined on the `'root'` subelement of the cell, since that is the default target for the cell's magnet.) Expects a selector.
A change in the cell's magnet affects the following characteristics of the cell view:
- magnet highlight
It has no effect on the connection validation and link model attributes.
// `standard.Rectangle` has `root`, `body` and `label` subelements
const rect = new shapes.standard.Rectangle();
// we want to have the `label` on the outside and under the `body` of the rectangle:
rect.attr(['label'], { y: 'calc(h + 10)', textVerticalAnchor: 'top' });
// normally, highlighters would appear around
// the `root` subelement (the wrapper of `body` and `label`)
// however, we want highlighters to highlight only to the `body` subelement,
// so we need to specify it as a proxy for `root`:
rect.attr(['root','highlighterSelector'], 'body');
html​
Valid for HTML subelements (e.g a <div/> inside a <foreignObject/>).
The html attribute allows you to define an HTML content of the subelement. The value of the html attribute is a string representing serialized HTML content. The HTML content is rendered inside the subelement.
element.attr(['foreignObjectSelector', 'html'], '<div style="color: red">Hello, World!</div>');
magnet​
When set to true, the subelement can become the source/target of a link during link reconnection. Useful for so called 'ports'. When set to magnet: 'passive', a new link won't be added to the graph when a user clicks on the respective magnet. This use case is common for the creation of 'input' ports.
const paper = new dia.Paper({
// Other Paper options
// The default behaviour of magnet validation
validateMagnet: function(_cellView, magnet, _evt) {
return magnet.getAttribute('magnet') !== 'passive';
}
});
const portIn = {
label: {
// Label position and markup
},
attrs: {
portBody: {
// Other portBody attributes
magnet: 'passive' // No link added upon user click
},
label: { text: 'port' }
},
markup: util.svg`<rect @selector="portBody"/>`
};
magnetSelector​
Designate another subelement of the cell as a proxy target for the cell's magnet, whenever this subelement (the one on which this attribute is defined) becomes the source/target of a link. (Usually, this attribute needs to be defined on the `'root'` subelement of the cell, since that is the default target for the cell's magnet.) Expects a selector.
A change in the cell's magnet affects the following characteristics of the cell view:
- link anchor
- link connection point
- connector
- router
It has no effect on the connection validation, magnet highlighter and the source and target link model attributes.
// `standard.Rectangle` has `root`, `body` and `label` subelements
const rect = new shapes.standard.Rectangle();
// we want to have the `label` on the outside and under the `body` of the rectangle:
rect.attr(['label'], { y: 'calc(h + 10)', textVerticalAnchor: 'top' });
// normally, links would connect to the `root` subelement (the wrapper of `body` and `label`)
// however, we want links to connect only to the `body` subelement,
// so we need to specify it as a proxy for `root`:
rect.attr(['root','magnetSelector'], 'body');
points​
applies to: <polyline/>, <polygon/>
The same as the SVG points attribute, but it can be a calc() expression.
{ points: '0,0 0,calc(h) calc(w),calc(h) calc(w),0' }
port​
An object containing at least an id property. This property uniquely identifies the port. If a link gets connected to a magnet that has also a port object defined, the id property of the port object will be copied to the port property of the source/target of the link.
props​
Special attribute for setting the properties of the HTMLElement. The value is not set as a node attribute, but using the DOM property.
element.attr('input', {
props: { value: 'text', readonly: false },
type: 'text',
placeholder: 'Enter text'
});
props is an object with one or more of the following properties: value, checked, disabled, readOnly, contentEditable, multiple, selected, indeterminate.
For example, to set the content of the textarea element, use the following syntax:
element.attr('textarea/props/value', 'textarea content');
When setting a value of the select element, pass the value of the option element.
element.attr('select/props/value', 'option1');
For the select with multiple options, pass an array of values.
element.attr('select/props/value', ['option1', 'option2'], { rewrite: true });
r​
applies to: <circle/>
The same as the SVG r attribute, but it can be a calc() expression.
{ r: 'calc(s / 2)' }
ref​
Requires the paper to be in the render tree (e.g. not display: none).
JointJS needs to measure the size of the referenced element. If the referenced element is not in the render tree, the size cannot be measured and the relative attributes will not work as expected.
Using such attributes can affect the performance of the application. It is recommended to use them only when necessary.
A selector pointing to an element that is used as a reference for calculating relative size and position attributes.
If the ref attribute is not used, the bounding box of the reference used to calculate the relative position is the bounding box of the element model itself, i.e. el.getBBox() (which is sufficient in most cases).
refCx​
Set cx attribute of the subelement relatively to the width of the element referenced to by the selector in ref attribute. If the value is in the [0, 1] interval (or expressed in percentages, e.g. '80%'), the cx of the subelement will be set as a percentage of the width of the referenced element. If the value is <0 or >1, the height of the subelement will be smaller/bigger than the width of the referenced element by the amount specified. Note that this makes sense only for SVG elements that support rx and ry attributes, such as <ellipse>.
refCy​
Set cy attribute of the subelement relatively to the height of the element referenced to by the selector in ref attribute. If the value is in the [0, 1] interval (or expressed in percentages, e.g. '80%'), the cy of the subelement will be set as a percentage of the height of the referenced element. If the value is <0 or >1, the height of the subelement will be smaller/bigger than the height of the referenced element by the amount specified. Note that this makes sense only for SVG elements that support rx and ry attributes, such as <ellipse>.
refDKeepOffset​
Set d attribute of the <path/> subelement relatively to the dimensions and position of the element referenced by the selector in the ref attribute. The refD path data is scaled so that the path's dimensions match the reference element's dimensions, and translated so that the path's origin matches the origin of the reference element.
The original path data offset is preserved. This means that if the top-left corner of the refD bounding box does not lie at 0,0, the gap between the path and origin is preserved in the rendered shape, as well.
const Path = dia.Element.define('examples.Path', {
attrs: {
path: {
refDKeepOffset: 'M 10 10 30 10 30 30 z', // path offset is 10,10
fill: 'red',
stroke: 'black'
}
}
}, {
markup: util.svg`<path @selector="path"/>`
});
const p = (new Path()).resize(40, 40).addTo(graph);
// the rendered path's `d` attribute will be 'M 10 10 50 10 50 50 z'
// can be obtained by `p.findView(paper).findNode('path').getAttribute('d');`
refDResetOffset​
alias: refD
Set d attribute of the <path/> subelement relatively to the dimensions and position of the element referenced by the selector in the ref attribute. The refD path data is scaled so that the path's dimensions match the reference element's dimensions, and translated so that the path's origin matches the origin of the reference element.
The original path data offset is not preserved. This means that if the top-left corner of the refD bounding box does not lie at 0,0, the path is translated so that this gap disappears. The rendered path then fits perfectly into the reference element's bounding box.
const Path = dia.Element.define('examples.Path', {
attrs: {
path: {
refDResetOffset: 'M 10 10 30 10 30 30 z', // path offset of 10,10 will be discarded
fill: 'red',
stroke: 'black'
}
}
}, {
markup: util.svg`<path @selector="path"/>`
});
const p = (new Path()).resize(40, 40).addTo(graph);
// the rendered path's `d` attribute will be 'M 0 0 40 0 40 40 z'
// can be obtained by `p.findView(paper).findNode('path').getAttribute('d');`
refDx​
Make x-coordinate of the subelement relative to the right edge of the element referenced to by the selector in ref attribute.