How to use uploader for a changing image in shape and diagram background image in Javascript Diagram?
In Syncfusion® JavaScript Diagram , you can easily customize both the diagram background and node shapes. To change the diagram’s background, set the image path using the background source property in the pageSettings. This allows you to apply custom colors or images. The node shapes can be dynamically updated at runtime using the shape property. This feature enables you to assign custom images to nodes via an image uploader.
To customize, select a node and click the Change Node Image button, which opens the uploader dialog. You can then select an image to place inside the node. Similarly, click the Change Background button to update the diagram’s background image.
Refer to the below code snippet
function handleImageUpload(event) {
const inputElement = event.target ;
if (inputElement.files && inputElement.files[0]) {
const file = inputElement.files[0];
const reader = new FileReader();
reader.onloadend = () => {
const imageSource = reader.result ;
if (imageFor === 'Background') {
// Set the image as background for the diagram
diagram.pageSettings.background.source = imageSource;
diagram.pageSettings.background.color = 'transparent';
diagram.dataBind();
} else if (imageFor === 'Shape' && diagram.selectedItems.nodes.length > 0) {
let selectedNode = diagram.selectedItems.nodes[0];
selectedNode.shape = { type: 'Image', source: imageSource };
diagram.dataBind();
}
};
reader.readAsDataURL(file);
}
}
Sample : Click here for sample
Conclusion
I hope you enjoyed learning how to use uploader for a changing image in shape and diagram background image
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, Direct-Trac, or feedback portal. We are always happy to assist you!