How to Animate Connectors in JavaScript Diagram?
This article explains how to animate connectors in the JavaScript Diagram component using the annotationTemplate. By default, connectors in the diagram do not support direct animation through built-in properties. However, we can achieve a visual animation effect by dynamically updating the position of annotations rendered through a template.
To simulate the animation, we add one or more annotations to each connector. Each annotation uses an annotationTemplate to visually represent an animation element. At regular intervals, we update the offset value of each annotation to make it move along the path of the connector. As a result, it appears as if the connector is animated.
Refer to the following code example that shows how to update the annotation position periodically:
function startWorkflow(args) {
connectorCollection = [];
var level = args ? args : 1;
var connectors = diagram.connectors;
for (var i = 0; i < connectors.length; i++) {
if (connectors[i].addInfo && connectors[i].addInfo.level === level) {
connectorCollection.push(connectors[i]);
}
}
var intervalDuration = 120;
flowInterval = setInterval(function () {
for (var j = 0; j < connectorCollection.length; j++) {
var connector = connectorCollection[j];
for (var k = 0; k < connector.annotations.length; k++) {
if (connector.annotations[k].offset < 0.99) {
connector.annotations[k].offset += 0.025;
connector.style.strokeColor = '#F8FC02';
}
if (connector.annotations[k].offset >= 0.95) {
connector.annotations[k].offset = 0;
}
}
}
diagram.dataBind();
}, intervalDuration);
}
Call this startWorkflow function once the diagram has been rendered. This setup will continuously update the annotation position and visually simulate an animation along the connector path.
Refer to the working sample for additional details and implementation: Sample
Conclusion
We hope you enjoyed learning on how to animate connectors using annotationTemplate in JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!