admin管理员组文章数量:1343351
I am trying to add an image into the center of a SVG circle. I tried with patterns
<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.
I am trying to add an image into the center of a SVG circle. I tried with patterns
<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.
Share Improve this question edited Nov 23, 2015 at 5:16 Fawzan 4,8498 gold badges43 silver badges87 bronze badges asked Sep 3, 2013 at 8:06 shay levishay levi 3193 silver badges12 bronze badges 02 Answers
Reset to default 7Clipping should do what you are looking for: https://developer.mozilla/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./EKUTUco/1/edit?html,output
Up to you to center the images in javascript according to their sizes, via x
and y
attributes.
Ok I found the answer. What I did is adding a filter to my 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)');
本文标签: javascriptHow to add image into center of svg circleStack Overflow
版权声明:本文标题:javascript - How to add image into center of svg circle? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743695873a2523493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论