How to add a hollow rect on image with svg

To add a hollow (stroke-only) rectangle on an image using SVG, you can follow these steps:

  1. Create an SVG: Start by creating an SVG element that contains both the image and the rectangle. Use the tag to define the SVG element and specify its width and height.

  2. Add the Image: Use the tag to add the image to the SVG. Specify the image source (xlink:href) and its position (x and y attributes) within the SVG.

  3. Add the Hollow Rectangle: Use the tag to add the hollow rectangle to the SVG. Specify the rectangle’s position (x and y attributes), width (width attribute), height (height attribute), and any other styling properties you want to apply. Set the fill attribute to "none" to make the rectangle transparent.

  4. Add the Stroke: Set the stroke attribute of the rectangle to the desired color for the stroke. You can also adjust the stroke-width attribute to control the thickness of the stroke.

Here’s an example of how you can add a hollow rectangle on an image using SVG:

<svg width="500" height="300">
  <image xlink:href="path/to/image.jpg" x="0" y="0" width="500" height="300" />

  <rect x="100" y="100" width="200" height="100" fill="none" stroke="green" stroke-width="2" />
svg>

In the example above, the SVG has a width of 500 and a height of 300. The tag adds the image at position (0, 0) with a width of 500 and a height of 300. The tag adds a hollow rectangle at position (100, 100) with a width of 200 and a height of 100. The rectangle has a transparent fill (fill="none") and a green stroke (stroke="green") with a thickness of 2 pixels (stroke-width="2").

You can adjust the positions, dimensions, stroke color, and stroke width of the image and rectangle to fit your specific requirements.

Remember to replace "path/to/image.jpg" with the actual path to your image file.

你可能感兴趣的:(前端)