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

标签 reactjs animation svg geometry gsap

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

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

enter image description here

这是我的 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

关于reactjs - 如何为 SVG 圆圈内的图像添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54455607/

相关文章:

reactjs - react-router-dom 条件渲染

javascript - 如何将范围/输入 slider 附加到 Canvas 动画上?

html - CSS 动画在一定时间后开始

css - 更新描边颜色 CSS

css - svg - 调整路径大小时保持填充纹理的原始大小

node.js - 如何修复 Windows 10 上的 "expo-cli"问题?

javascript - 如果单击链接后解决了某些异步条件,是否有一种可访问的方式告诉用户界面的某些部分可以重定向到其他页面?

java - 如何即时将 SVG 转换为 PNG

javascript - 状态为空,在 useEffect Hook 之后

swift - 如何让两个或多个 UIView 动画 block 同时运行?