# 裁切和遮罩

擦除已经创建的元素的部分内容,最初看起来有点矛盾。但是如果你打算在 SVG 中创建一个半圆形,你将很快发现下面的属性的作用了。

Masking 允许使用透明度和灰度值遮罩计算得的软边缘。

# 裁切 clipPath

我们在一个圆形的基础上创建上面提到的半圆形:

<svg width="310" height="100" style="border:1px solid;">
  <defs>
    <clipPath id="cut-off-bottom">
      <rect x="0" y="0" width="200" height="100" />
    </clipPath>
  </defs>
  <circle cx="100" cy="100" r="100" clip-path="url(#cut-off-bottom)" />
</svg>

# 遮罩 mask

遮罩的效果最令人印象深刻的是表现为一个渐变。如果你想要让一个元素淡出,你可以利用遮罩效果实现这一点。

<svg width="310" height="100" style="border:1px solid;">
  <defs>
    <linearGradient id="Gradient">
      <stop offset="0" stop-color="white" stop-opacity="0" />
      <stop offset="1" stop-color="white" stop-opacity="1" />
    </linearGradient>
    <mask id="Mask">
      <rect x="0" y="0" width="200" height="200" fill="url(#Gradient)"  />
    </mask>
  </defs>
  <rect x="0" y="0" width="200" height="200" fill="green" />
  <rect x="0" y="0" width="200" height="200" fill="red" mask="url(#Mask)" />
</svg>