I am trying to add an image into the center of a SVG circle. I tried with patterns
我试图将图像添加到SVG圆的中心。我尝试过模式
<pattern id="image_birds" x="0" y="0" patternUnits="userSpaceOnUse" height="100" width="100">
<image x="0" y="0" xlink:href="birds.png" height="50" width="50"></image>
</pattern>
But it does not center the image. I am working with Javascript.
但它并不是图像的中心。我正在使用Javascript。
2 个解决方案
#1
6
Clipping should do what you are looking for: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Clipping_and_masking
剪辑应该做你想要的:https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Clipping_and_masking
Something like:
就像是:
<clipPath id="cut-off-bottom">
<circle cx="100" cy="100" r="50" />
</clipPath>
<image x="25" y="25" xlink:href="http://placehold.it/150.png" height="150" width="150" clip-path="url(#cut-off-bottom)" ></image>
You can see the result of this example here: http://jsbin.com/EKUTUco/1/edit?html,output
你可以在这里看到这个例子的结果:http://jsbin.com/EKUTUco/1/edit?html,output
Up to you to center the images in javascript according to their sizes, via x
and y
attributes.
由您根据x尺寸,通过x和y属性将图像置于javascript中心。
#2
2
Ok I found the answer. What I did is adding a filter to my svg:
好的,我找到了答案。我做的是为我的svg添加一个过滤器:
<filter id = "i1" x = "0%" y = "0%" width = "100%" height = "100%">
<feImage xlink:href = "birds.png"/>
</filter>
and in the circle add attribute:
并在圈子中添加属性:
circle.setAttribute('filter','url(#i1)');
#1
6
Clipping should do what you are looking for: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Clipping_and_masking
剪辑应该做你想要的:https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Clipping_and_masking
Something like:
就像是:
<clipPath id="cut-off-bottom">
<circle cx="100" cy="100" r="50" />
</clipPath>
<image x="25" y="25" xlink:href="http://placehold.it/150.png" height="150" width="150" clip-path="url(#cut-off-bottom)" ></image>
You can see the result of this example here: http://jsbin.com/EKUTUco/1/edit?html,output
你可以在这里看到这个例子的结果:http://jsbin.com/EKUTUco/1/edit?html,output
Up to you to center the images in javascript according to their sizes, via x
and y
attributes.
由您根据x尺寸,通过x和y属性将图像置于javascript中心。
#2
2
Ok I found the answer. What I did is adding a filter to my svg:
好的,我找到了答案。我做的是为我的svg添加一个过滤器:
<filter id = "i1" x = "0%" y = "0%" width = "100%" height = "100%">
<feImage xlink:href = "birds.png"/>
</filter>
and in the circle add attribute:
并在圈子中添加属性:
circle.setAttribute('filter','url(#i1)');