adds a custom SVG-based shape to Diagram
id | string | shape ID |
obj | object | shape data object |
$$("diagram1").addShape("internet", {
template:'<svg width="60" height="60" viewBox="0 0 60 60">...</svg>',
// other data
});
The ID of the shape must be unique and should not coincide with any existing one. Possible fields are listed below:
true
, an item will have the same width and height when its size is changed via form or drag resizetemplate
fieldThe template
field can be used as an HTML string, a function that returns a template, or as a name of a built-in template.
You can pass any HTML string as a value for template (e.g. define it as an image):
$$("diagram1").addShape("laptop", {
template: `<img class="custom-image" src="svg/laptop.svg"></img>`,
// other data
});
Related sample: Diagram Editor: Extra Shapes
Template as a function can be useful when you need to calculate shape size depending on outer size (item size). The function accepts the only parameter:
$$("diagram1").addShape("myshape", {
template: function(obj) {
const rx = obj.width / 2;
const ry = obj.height / 2;
const d = (obj.lineWidth || 1) / 2;
return `
<svg
width='100%'
height='100%'>
<ellipse cx='${rx}' cy='${ry}' rx='${rx -d}' ry='${ry - d}' ></ellipse>
</svg>`;
},
//...
});
Related sample: Diagram Editor: Template as a Function
This option can be useful when you want to use one template for multiple shapes that may have different colors or sizes. For example, "rrect" template (rounded rectangle) can be used as "join" shape with small width/height and as "action" shape with standard size:
shapes:[
{
id: "action",
template: "rrect",
backgroundColor: "#b5d461",
lineColor: "#b5d461",
},
{
id: "join",
template: "rrect",
height: 80,
width: 10,
backgroundColor: "#ffb955",
lineColor: "#ffb955",
},
// ...
]
Related sample: Diagram Editor: Styled Shapes