SVG viewbox overflow: hidden / crop

后端 未结 1 2042
不思量自难忘°
不思量自难忘° 2020-12-06 05:45

Is there a way to make anything outside the viewBox invisible? As if the viewBox itself were an element with overflow: hidden

相关标签:
1条回答
  • 2020-12-06 06:19

    You can use the rectangle as a <clipPath>:

    <defs>
        <rect id="rect" width="100%" height="100%" fill="none" stroke="blue" />
        <clipPath id="clip">
            <use xlink:href="#rect"/>
        </clipPath>
    </defs>
    

    and then apply it to a <g> element which contains your text (and anything else you want to clip:

    <g clip-path="url(#clip)">
        <text y="10" x="10%" width="10%" height="200%" fill="#000" font-size="30">Only the part inside the viewBox should be visible</text>
    </g>
    

    Since the <rect> was used only to shape the clipPath, you have to redraw it:

    <use xlink:href="#rect"/>
    

    Updated fiddle

    0 讨论(0)
提交回复
热议问题