如何为 SVG 圆内的图像添加边框

How to add a border to an image inside an SVG circle

我正在使用 GSAP 围绕圆圈制作笔画动画。这是 Codepen.

我想要做的是在外部粉红色和(应该是)内部圆形图像之间留出间距,如下所示:

这是我的 SVG 代码:

      <svg id='svg1' width='48' height='48' viewBox='0 0 48 48'>
        <defs>
          <clipPath id='circleView'>
            <circle cx='24' cy='24' r='22' fill='none' stroke='#FF62E1' strokeWidth='2' />
          </clipPath>
        </defs>
        <image
          width='100%'
          height='100%'
          xlinkHref={'https://source.unsplash.com/random'}
          clipPath='url(#circleView)'
          />
        <circle
          cx='24'
          cy='24'
          r='22'
          fill='none'
          stroke='#FF62E1'
          strokeWidth='2'
          strokeDasharray='100.48'
          strokeDashoffset='100.48'
          // @ts-ignore
          ref={(circle) => { this.circle = circle }} >
          >
        </circle>
      </svg>

我试过 filter 并使用多个圆圈来创建效果,但无济于事。

关于如何实现这一点有什么想法吗?

您可以将 clipPath 圆的半径更改为比另一个圆小一点,并使用正方形图像以使其完全适合。

render() {
  return (
    <svg id='svg1'  viewBox='0 0 48 48'>
      <defs>
        <clipPath id='circleView'>
          <circle cx='24' cy='24' r='18' fill='none' stroke='#FF62E1' strokeWidth='2' />
        </clipPath>
      </defs>
      <image
        x="0"
        y="0"
        width='48'
        height='48'
        xlinkHref={'https://source.unsplash.com/random/1500x1500/'}
        clipPath='url(#circleView)'
      />
      <circle
        cx='24'
        cy='24'
        r='22'
        fill='none'
        stroke='#FF62E1'
        strokeWidth='2'
        // @ts-ignore
        ref={(circle) => { this.circle = circle }} >
      </circle>
    </svg>
    )
  }
}

Codepen

此选项会留下一个可以看到背景的透明间隙,因此可能正是您想要的,也可能不是。另一种方法是创建另一个带有笔划但顶部没有填充的圆,再次使用不同的半径。

Alternative Codepen