Is there a way to cut one shape out of another in SVG? For example, I have a rect and an ellipse and I want to make a rect with a transparent hole in the middle. I would imagine it would look something like this:
<set operation="difference" fill="black"> <rect x="10" y="10" width="50" height="50/> <ellipse cx="35" cy="35" rx=10 ry=10/> </set>
The closest thing I can find is clipping, which will give me the intersection of two shapes. In my example that would result in just the hole being solid and the rest of the rect being transparent.
I looked through Inkscape and there is a difference option in the path menu, but this converts the shapes to paths and then creates a new path. The identity of the shapes is lost so there is no easy way to, for example go into the svg file and change the radius of the ellipse.
Is there any ideas for how I might do this?
Select both shapes using the “Selection Tool”. With both shapes selected, go to the “Pathfinder Tool” (displayed below in red). Select the “Minus Front” selection (displayed below in green). Your bottom shape should now have a cutout of the top shape.
You must use path element to cut a hole.
See the example from the SVG specification: (you can click this link or the following image to view the real svg file)
<g fill-rule="evenodd" fill="red" stroke="black" stroke-width="3"> <path d="M 250,75 L 323,301 131,161 369,161 177,301 z"/> <path d="M 600,81 A 107,107 0 0,1 600,295 A 107,107 0 0,1 600,81 z M 600,139 A 49,49 0 0,1 600,237 A 49,49 0 0,1 600,139 z"/> <path d="M 950,81 A 107,107 0 0,1 950,295 A 107,107 0 0,1 950,81 z M 950,139 A 49,49 0 0,0 950,237 A 49,49 0 0,0 950,139 z"/> </g>
For your case:
<path d="M10 10h50v50h-50z M23 35a14 10 0 1 1 0 0.0001 z" stroke="blue" stroke-width="2" fill="red" fill-rule="evenodd" />
M10 10h50v50h-50z
will draw a rect.
M25 35a10 10 0 1 1 0 0.0001 z
will draw a ellipse.
fill-rule="evenodd"
will make the hole.
The key point is draw outer shape and inner shapes(holes) in different direction (clockwise vs anti-clockwise).
You can cut more holes by concat more hole path data.
This image explain how to cut a hole:
See w3c docs: SVG Arc Commands and SVG fill-rule Property.
You should be able to use the fill-rule: evenodd
property to achieve this effect, if you want to prevent the fill of the rectangle from painting where the circle is. See this example from the SVG specification (image below will only display if your browser supports SVG):
For some reason, I can't seem to get this to work with shapes like the rect
and ellipse
that you provided in the question. Here's my attempt:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With