Articles in this section

How to use uploader for a changing image in shape and diagram background image in React Diagram?

In Syncfusion® React 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

reader.onloadend = () => {
       const imageSource = reader.result;

       console.log('Image uploaded:', imageSource);

       const diagram = diagramRef.current?.ej2_instances?.[0];

       if (diagramInstance) {
         if (imageFor === 'Background') {
           // Set the image as background for the diagram
           diagramInstance.pageSettings.background.source = imageSource;
           diagramInstance.pageSettings.background.color = 'transparent'; // Set the background to transparent
           diagramInstance.dataBind(); // Force diagram to re-render with updated background
           console.log('Background set to image:', imageSource);
         } else if (imageFor === 'Shape') {
           // Ensure there is a selected node before applying the image as the shape
           if (diagramInstance.selectedItems.nodes.length > 0) {
             const selectedNode = diagramInstance.selectedItems.nodes[0];

             // Set the node's shape to the selected image
             selectedNode.shape = { type: 'Image', source: imageSource };
             diagramInstance.dataBind(); // Force diagram to re-render with updated node shape
             console.log('Node shape set to image:', imageSource);
           } else {
             console.error('No node selected.');
           }
         }
       } else {
         console.error("Diagram instance not found.");
       }
     }; 

Sample : Click here for sample

Conclusion

I hope you enjoyed about how to use uploader for a changing image in shape and diagram background image in React Diagram

You can refer to our React 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 React 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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied