0

I want to create a simple UI where the users hovers over a SVG circle and the circle gets bigger (increases radius). Inside each circle I want to have an image/icon. The best way I've experimented with so far is to layer it; the image is appended first, then circle on top with fill:'none'. For the non-svg div case, it's a bit simpler, because we can style the div to crop the picture like this:

<div class="cropper">
    <img class="picture" src="https:a picturejpg">
</div>

CSS

.cropper {
    width: 100px;
    height: 100px;
    position: relative;
    border: 2px solid white;
    -webkit-border-radius: 50px;
    -moz-border-radius: 50px;
    border-radius: 50px;
    overflow:hidden;
}
.picture {
    display: block;
    margin: 0 auto;
    height: auto;
    width: 100%;
}

Question: How can I adapt a similar circular crop implementation as seen above to the SVG case? Here is a rough visual reference illustrating my problem:

enter image description here

  • important note: for the moment I'm just trying to do one svg circular crop as a proof of concept, but soon I will need to append them programmatically via d3.js or jquery. Answers that are programmatically robust are preferred.
Arash Howaida
  • 2,575
  • 2
  • 19
  • 50

1 Answers1

2

Maybe you can use clip-path which references an inline svg <clipPath>.. Something like this...

img {
  width: 300px;
  clip-path: url(#clipping);
}
<img src="https://i.imgur.com/pqggrK0.jpg" />
<svg>
  <clipPath id="clipping">
    <circle cx="150" cy="150" r="50" />
    <rect x="150" y="150" width="100" height="100" />
  </clipPath>
</svg>

More on clip paths here: https://css-tricks.com/clipping-masking-css/

Devansh J
  • 4,006
  • 11
  • 23