【问题标题】:How to add a border to an image inside an SVG circle如何为 SVG 圆圈内的图像添加边框
【发布时间】:2019-06-24 14:53:24
【问题描述】:

我正在使用 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 并使用多个圆圈来创建效果,但无济于事。

关于如何实现这一点的任何想法?

【问题讨论】:

  • 第二个圆需要更大的半径。如果更改半径,则需要重新计算 stroke-dasharray 和 stroke-dashoffset 的路径长度。您还需要重新计算 SVG 画布的大小
  • @enxaneta 或更小的clipPath 圈子?

标签: reactjs animation svg geometry gsap


【解决方案1】:

您可以将您的 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

【讨论】:

  • 感谢@ksav,我离开了一个星期,但这对我帮助很大。非常感谢!
猜你喜欢
  • 2015-06-09
  • 2013-01-22
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
相关资源
最近更新 更多