SVG笔画未显示在clipPath内部

A7D*_*7DC 2 svg border stroke reactjs

我有以下Codepen,我试图在其中为图像周围的圆圈设置动画。

到目前为止,我有一个圆形 SVG 正在剪辑图像,但它没有显示 clipPath 内部的笔划。

如何让边框显示?

class App extends React.Component {
  render() {
    return (
      <svg width='48' height='48'>
        <defs>
          <clipPath id='circleView'>
            <circle cx='24' cy='24' r='23' fill='none' stroke='red' strokeWidth='2' />
          </clipPath>
        </defs>
        <image width='48' height='48' xlinkHref={'https://source.unsplash.com/random'} clipPath='url(#circleView)' />
      </svg>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*_TT 5

正如罗伯特所说,该clip-path行中的子 SVG 图形不会显示。

因此,对于动画,您需要在 clip-path

<circle cx="25" cy="24" r="14" fill="none" stroke="red" strokeWidth="2" />

.container {
    width:25%;
	height:25%;
   }
Run Code Online (Sandbox Code Playgroud)
<div class="container">
	  <svg  viewBox="0 0 48 48" >
        <defs>
          <clipPath id='circleView'>
            <circle cx='24' cy='22' r='16' fill='none' stroke='red' stroke-width='2' />
          </clipPath>
        </defs>
        <image width="100%" height="100%"   xlink:href='https://i.stack.imgur.com/O9eO8.jpg' 
     		clip-path='url(#circleView)' />
  <circle cx='24' cy='22' r='16' fill='none' stroke='red' strokeWidth='2' /> 
</svg>
  </div>
Run Code Online (Sandbox Code Playgroud)

要为圆设置动画,请使用stroke-dashoffset从最大到零的属性更改。values="(100.48;0)"

单击后动画开始

.container {
    width:25%;
	height:25%;
   }
Run Code Online (Sandbox Code Playgroud)
<div class="container">
	  <svg id="svg1" viewBox="0 0 48 48">
        <defs>
          <clipPath id='circleView'>
     <circle cx='24' cy='22' r='16' fill='none' stroke='red' stroke-width='2' />
          </clipPath>
        </defs>
        <image width="100%" height="100%" xlink:href='https://i.stack.imgur.com/O9eO8.jpg' clip-path='url(#circleView)' />
	<circle  transform="rotate(-90 24 22)" cx="24" cy="22" r="16" fill='none' stroke='red' strokeWidth='2' 
    		stroke-dasharray="100.48"   stroke-dashoffset="100.48" >
            <animate
              attributeName="stroke-dashoffset"
              dur="1s"
              begin="svg1.click"
              values="100.48;0"
              fill="freeze"/>
	</circle>		
      </svg>

  </div>
Run Code Online (Sandbox Code Playgroud)

动画的变体 CSS

我在圆形动画中添加了透明动画。

当您悬停鼠标光标时动画开始。

.container {
    width:25%;
	height:25%;
   }  
   #crc1 {
   fill:skyblue;
   stroke-width:1;
   stroke:red;
   stroke-dasharray:100.48;
   stroke-dashoffset:100.48;
    fill-opacity:0.9;
     }
   
   #crc1:hover {
    animation: dash 1.5s ease-out forwards;
      }
	  
    @keyframes dash {
  0% { stroke-dashoffset: 100.48; fill-opacity:0.9; }
  50% { fill-opacity:0.45;}
  100% { stroke-dashoffset: 0; fill-opacity:0; }
   }
   
   #img1 {
   clip-path: url(#circleView);
     }
Run Code Online (Sandbox Code Playgroud)
<div class="container">
	  <svg id="svg1" viewBox="0 0 48 48">
        <defs>
          <clipPath id='circleView'>
            <circle cx='24' cy='22' r='16'/>
          </clipPath>
        </defs>
        <image width="100%" height="100%" xlink:href='https://i.stack.imgur.com/O9eO8.jpg' 
     		clip-path='url(#circleView)' />
         <circle id="crc1"   cx="24" cy="22" r="16" />
        		
      </svg>

  </div>
Run Code Online (Sandbox Code Playgroud)