aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/svg/element/clippath/index.html
blob: 4922bd1e93e858dd5842123388dcd49d2144a53d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
title: clipPath
slug: Web/SVG/Element/clipPath
tags:
  - Clip
  - SVG
  - 元素
  - 参考
translation_of: Web/SVG/Element/clipPath
---
<div>{{SVGRef}}</div>

<p><a href="/zh-CN/docs/Web/SVG">SVG</a> 元素 <strong><code>&lt;clipPath&gt;</code></strong> 定义一条剪切路径,可作为其他元素的 {{SVGAttr("clip-path")}} 属性的值。</p>

<p>剪切路径限制了图形的可见范围。从概念上来说,如果图形超出了当前剪切路径所包围的区域,那么超出部分将不会绘制。</p>

<div id="Example">
<div class="hidden">
<pre class="brush: css">html,body,svg { height:100% }</pre>
</div>

<pre class="brush: html">&lt;svg viewBox="0 0 100 100"&gt;
  &lt;clipPath id="myClip"&gt;
    &lt;!--
      圆圈外的所有东西都会被裁剪掉,因此不可见。
    --&gt;
    &lt;circle cx="40" cy="35" r="35" /&gt;
  &lt;/clipPath&gt;

  &lt;!-- 作为引用元素(英文原文:<code>for reference</code>)的黑色心形 --&gt;
  &lt;path id="heart" d="M10,30 A20,20,0,0,1,50,30 A20,20,0,0,1,90,30 Q90,60,50,90 Q10,60,10,30 Z" /&gt;

  &lt;!--
    和上述黑色心形形状相同的红色心形,剪切路径是上面定义的圆;
    红色心形只有在圆内的部分可见。
  --&gt;
  &lt;use clip-path="url(#myClip)" xlink:href="#heart" fill="red" /&gt;
&lt;/svg&gt;</pre>

<pre class="brush: css">/* 如果浏览器支持几何属性 r,可以加一点 css */

@keyframes openYourHeart {from {r: 0} to {r: 60px}}

#myClip circle {
  animation: openYourHeart 15s infinite;
}</pre>

<p>{{EmbedLiveSample('Example', 100, 100)}}</p>
</div>

<p>从概念上讲,剪切路径等于给引用元素设置了一个自定义的可视区域。因此,它虽然会影响一个元素的绘制,但不会影响这个元素本身的几何形状,比如被剪切元素(通过 {{SVGAttr("clip-path")}} 属性引用了 <code>&lt;clipPath&gt;</code> 的元素及其子元素)的包围盒和没有被剪切时相同。</p>

<p>默认情况下,{{cssxref("pointer-events")}} 不会在被剪切掉的区域(不可见的区域)内触发。举个例子,如果一个半径为10的圆形被剪切成半径为5的圆形,那么这个圆在半径为5以外的区域不会收到“click”事件。</p>

<h2 id="属性">属性</h2>

<dl>
 <dt>{{SVGAttr("clipPathUnits")}}</dt>
 <dd>为 <code>&lt;clipPath&gt;</code> 元素的内容定义坐标系。<br>
 <small><em>Value type</em><code>userSpaceOnUse</code>|<code>objectBoundingBox</code> ; <em>Default value</em><code>userSpaceOnUse</code><em>Animatable</em><strong>yes</strong></small></dd>
</dl>

<h3 id="全局属性">全局属性</h3>

<dl>
 <dt><a href="https://developer.mozilla.org/docs/Web/SVG/Attribute/Core">Core Attributes</a></dt>
 <dd><small>Most notably: {{SVGAttr('id')}}</small></dd>
 <dt><a href="https://developer.mozilla.org/docs/Web/SVG/Attribute/Styling">Styling Attributes</a></dt>
 <dd><small>{{SVGAttr('class')}}, {{SVGAttr('style')}}</small></dd>
 <dt><a href="https://developer.mozilla.org/docs/Web/SVG/Attribute/Conditional_Processing">Conditional Processing Attributes</a></dt>
 <dd><small>Most notably: {{SVGAttr('requiredExtensions')}}, {{SVGAttr('systemLanguage')}}</small></dd>
 <dt><a href="https://developer.mozilla.org/docs/Web/SVG/Attribute/Presentation">Presentation Attributes</a></dt>
 <dd><small>Most notably: {{SVGAttr('clip-path')}}, {{SVGAttr('clip-rule')}}, {{SVGAttr('color')}}, {{SVGAttr('display')}}, {{SVGAttr('fill')}}, {{SVGAttr('fill-opacity')}}, {{SVGAttr('fill-rule')}}, {{SVGAttr('filter')}}, {{SVGAttr('mask')}}, {{SVGAttr('opacity')}}, {{SVGAttr('shape-rendering')}}, {{SVGAttr('stroke')}}, {{SVGAttr('stroke-dasharray')}}, {{SVGAttr('stroke-dashoffset')}}, {{SVGAttr('stroke-linecap')}}, {{SVGAttr('stroke-linejoin')}}, {{SVGAttr('stroke-miterlimit')}}, {{SVGAttr('stroke-opacity')}}, {{SVGAttr('stroke-width')}}, {{SVGAttr("transform")}}, {{SVGAttr('vector-effect')}}, {{SVGAttr('visibility')}}</small></dd>
</dl>

<h2 id="用法">用法</h2>

<p>{{svginfo}}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("CSS Masks", "#ClipPathElement", "&lt;clipPath&gt;")}}</td>
   <td>{{Spec2("CSS Masks")}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("SVG1.1", "masking.html#EstablishingANewClippingPath", "&lt;clipPath&gt;")}}</td>
   <td>{{Spec2("SVG1.1")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("svg.elements.clipPath")}}</p>

<h2 id="相关内容">相关内容</h2>

<ul>
 <li>Other clipping and masking SVG elements: {{SVGElement("mask")}}</li>
 <li>Some CSS properties: {{cssxref("clip-path")}}, {{cssxref("pointer-events")}}</li>
</ul>