Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add image into center of svg circle?

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.

like image 564
shay levi Avatar asked Sep 03 '13 08:09

shay levi


2 Answers

Clipping should do what you are looking for: 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

Up to you to center the images in javascript according to their sizes, via x and y attributes.

like image 138
Mehdi Avatar answered Oct 15 '22 05:10

Mehdi


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)');
like image 21
shay levi Avatar answered Oct 15 '22 04:10

shay levi