I'd like to do something very much like Fill SVG path element with a background-image except that the solution offered there will tile the image to fill the the entire background area. I don't want that. If the image doesn't fill either the entire height or the entire width, then I'd just like it to center within the path shape and let that be that. If I try to restrict the behavior by changing the height/width attributes, then the rendering just scales the image until it fills at least one of the dimensions.
I guess this behavior somewhat makes sense since it uses patterns. I can see that what I want is not really a "pattern" per se. I just want to slap an image in the middle of my shape just as it is, not make a pattern out of it. However, I do want the shape boundaries defined by my SVG path to cut off the rendering of the image, whenever the image's size extends past those boundaries. I don't know else to get that behavior besides the use of a pattern for the fill attribute, as is done in the answer to that question.
In a similar question: Add a background image (.png) to a SVG circle shape , the user at the very bottom seems to indicate that he used a filter rather than a pattern to achieve what I'm seeking to achieve. However, when I try that, no regard is paid to the actual shape during rendering. The image is rendered without any regard to the shape boundaries, which seems pretty much useless.
Is there any way in SVG to get that pattern-like background image behavior but without actually tiling the image into a pattern?
Just make the
x
,y
,width
andheight
of the pattern match the bounding box of your path. You can just use "0","0","1" & "1" respectively here because thepatternUnits
defaults toobjectBoundingBox
, so the units are expressed relative to the bounding box. Then make the image in your pattern have thewidth
andheight
of the path bounding box also. This time, you'll need to use "real" sizes though.The image will be centred in the pattern automatically because the default value of
preserveAspectRatio
for<image>
does exactly what you want.