【问题标题】:Clip Path not working in an SVG element剪辑路径在 SVG 元素中不起作用
【发布时间】:2019-01-02 21:51:08
【问题描述】:

我正在尝试让一个矩形 <clipPath> 剪辑一个 SVG 元素中的 <circle>,但我似乎无法让它工作。

有谁知道问题出在哪里?

非常感谢您的任何帮助。

Codepen:https://codepen.io/emilychews/pen/jpLMaV

#circle {width: 10rem;}
<defs>
  <clipPath id="myClipPath">
    <rect x="30" y="30" width="100" height="100"/>
  </clipPath>
</defs>

<svg id="circle" style="clip-path: url(#myClipPath)" viewBox="0 0 391 391">
  <circle fill="#000" cx="195.5" cy="195.5" r="195"/>
</svg>

【问题讨论】:

    标签: svg clip clipping


    【解决方案1】:

    您忘记了剪辑路径声明周围的&lt;svg&gt;

    <svg width=0 height=0 >
    <defs>
      <clipPath id="myClipPath">
       <rect x="50px" y="50px" width="200px" height="200px"/>
      </clipPath>
    </defs>
    </svg>
    
    <svg style="clip-path: url(#myClipPath)" id="circle" width="400" height="400">  
      <circle fill="#000" cx="100px" cy="100px" r="100px"/> </svg>
    

    编辑:我简化并添加了 witdh 和高度属性

    【讨论】:

    • 这似乎仍然没有按预期工作?它将圆圈推得更远,似乎是添加到圆圈中而不是剪裁它——我已经更新了 codepen。
    • 您需要将第一个 SVG 的宽度和高度设置为 0,就像 @EchoMike444 所做的那样。否则 SVG 会占用页面上的空间。 codepen.io/PaulLeBeau/pen/bjoPrM
    猜你喜欢
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 2015-09-21
    • 2019-09-03
    • 1970-01-01
    • 2018-10-29
    相关资源
    最近更新 更多