I have some SVG objects with a tiled image background using fill="url(#background)
, where I'm defining #background
as a pattern
containing just an image
.
However, when the object is relocated by settings its x
/y
attributes (or cx
/cy
, or whatever else defines the offset), the background does not move with it.
How do I make the background move as well? From How would I move an SVG pattern with an element I tried to set the patternContentUnits='objectBoundingBox'
but that just turns the image into a solid color blob (oddly, though, colored according to the image, as if it were just the first pixel or some such).
Here is a jsfiddle illustrating all of this - the top rect
has been turned into a solid color, and then bottom rect
has the background image but it doesn't move with the rect
:
I know that if you were using transform="translate(...)"
instead of setting the x
/y
attributes, the background does get translated as well, but the existing codebase I'm working within doesn't use translate for positioning (and it would have other unintended consequences on the rest of the application).
Thanks in advance for any help.
I actually encountered the same problem and found a solution. I've forked your fiddle to show the changes. Basically, you remove the patternContentUnits attribute as it doesn't help here and update the x attribute of the image to match that of the rect object after each update.
http://jsfiddle.net/RteBM/2/
Updated HTML:
Updated JS:
Try to set value of patternUnits to "objectBoundingBox".
If you use objectBoundingBox units then a width of 1 (or 100%) is the width of the object so 178 would be 178 times that. You likely want
That won't make the pattern move though. You are going to need a transform of some sort to do that. If you can't put it on the
<rect>
maybe you can make the rect a child of a<g>
element and transform the<g>
element instead.