【发布时间】:2021-03-01 05:45:46
【问题描述】:
以下代码不起作用:
<svg>
<defs>
<clipPath id="test">
<g>
<rect x="0" y="0" width="10" height="10"></rect>
</g>
</clipPath>
</defs>
<rect x="0" y="0" width="100" height="100" clip-path="url(#test)"></rect>
</svg>
但这确实有效:
<svg>
<defs>
<clipPath id="test">
<rect x="0" y="0" width="10" height="10"></rect>
</clipPath>
</defs>
<rect x="0" y="0" width="100" height="100" clip-path="url(#test)"></rect>
</svg>
在我的项目中,组标签中有一些路径,我想将组重用为 clipPath 目标并同时显示路径。例如:
<svg>
<defs>
<g id="group">
<path d="..."></path>
<path d="..."></path>
<path d="..."></path>
<path d="..."></path>
</g>
<clipPath id="test">
<use xlink:href="#group" fill="#000"></use>
</clipPath>
</defs>
<!-- show the stroke of the group -->
<use xlink:href="#group" stroke="#000"></use>
<!-- at same time, clip the rect to make some animation as the background -->
<rect x="0" y="0" width="100" height="100" clip-path="url(#test)"></rect>
</svg>
【问题讨论】:
-
根据w3c,clipPath 允许的内容可以是任意数量的以下元素,按任意顺序:descriptive — desc、title、metadata animation — animate, animateColor, animateMotion, animateTransform, set shape — 圆形、椭圆、直线、路径、多边形、折线、矩形文本使用脚本
标签: svg