I’d like to capture clicks on regions of an image of the human body as selections on a multi select picklist.
Please write me a salesforce lightning web component to accomplish the above requirement.
Assistant
User
Do I record coordinates in “d” on the path tags?
Assistant
Yes, the "d" attribute of a SVG path tag takes a series of commands and parameters, which are indeed a sort of Cartesian coordinate system for SVG. Here's a simple overview of the most common commands:
M x y: move to the point (x, y) without drawing anything.
L x y: draw a line from the current point to the point (x, y).
C x1 y1, x2 y2, x1 y1, x2 y2, x y: draw a cubic curve from the current point to (x,y) using () as the control point at the beginning of the curve and (x2,y2,x2,y2) as the control point at the end of the curve.
Z: close the current path by drawing a line back to where the current path was started with a "M" command.
M x y: move to the point (x, y) without drawing anything.
L x y: draw a line from the current point to the point (x, y).
Sure, I can guide you through creating a basic Salesforce Lightning Web Component (LWC) to accomplish this task. However, I'd like to point out that detecting clicks on different regions of an image is usually achieved with JavaScript libraries like ImageMapster or similar. Salesforce LWC's security policies do not allow the use of third-party libraries that manipulate DOM directly, such as ImageMapster.
Therefore, this method I'm about to share with you is a more general way of creating clickable regions over an image, using SVG and LWC. The idea here is to overlay the image with SVG elements which can detect where the user has clicked.
To implement this, you would need to manually determine the regions on the image by defining SVG paths. These paths would correspond to the regions on the image.
The SVG paths are overlaid over the image. They're made transparent so that the image can be seen.
When a path is clicked, thehandleRegionClickmethod is called. Thedata-idattribute of the path is used to identify the clicked region.
The clicked region is added to or removed from theselectedRegionsarray, which is tracked so that the component re-renders when it changes.
The selected regions are displayed below the image usinglightning-pillcomponents.
The SVG paths are overlaid over the image. They're made transparent so that the image can be seen.
When a path is clicked, thehandleRegionClickmethod is called. Thedata-idattribute of the path is used to identify the clicked region.
The clicked region is added to or removed from theselectedRegionsarray, which is tracked so that the component re-renders when it changes.
The selected regions are displayed below the image usinglightning-pillcomponents.
Please note that this is a very simple example and may not cover all your requirements. You would need to replace the"..."in the path elements with your actual path data. To get these path coordinates, you can use a tool like an online SVG path editor to draw paths over your image.
C x1 y1, x2 y2, x1 y1, x2 y2, x y: draw a cubic curve from the current point to (x,y) using () as the control point at the beginning of the curve and (x2,y2,x2,y2) as the control point at the end of the curve.
Z: close the current path by drawing a line back to where the current path was started with a "M" command.
This will draw a square by moving to (10, 10), then drawing lines to (90, 10), (90, 90), and (10, 90), and then closing the path.
For complex images, manually plotting the coordinates can be labor-intensive. There are tools available online to assist in creating these path coordinates for SVG.
Please note that (0,0) in SVG coordinate system is the top-left corner and y values increase as you go down the image, which might be different from what you learned in school.