ClipPath和SVG Scaling

时间:2017-12-21 14:41:31

标签: javascript svg

我试图获得一个缩放的svg图像来剪裁图案,如果它在蓝色容器旁边。但是当我将剪辑路径应用于完全相同位置的模式时。图案的X和Y位置会发生变化,并且在下面显示的情况下,它会在容器外部结束,以确保应用完全相同的位置和变换。我还应用了一个feMorphology过滤器来显示剪切路径的绘制位置。

SVG (非剪辑)

Non Clipped

XML

<svg id="SvgjsSvg1006" width="550" height="650" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs">
    <defs id="SvgjsDefs1007">
        <clipPath id="SvgjsClipPath1019">
            <rect id="SvgjsRect1016" width="315" height="600" x="120" y="27"></rect>
        </clipPath>
        <filter id="dilate_shape">
            <feMorphology operator="dilate" in="SourceGraphic" radius="5" />
        </filter>
    </defs><!--?xml version="1.0" encoding="UTF-8" standalone="no" ?-->
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="550" height="650" viewBox="0 0 550 650" xml:space="preserve">
        <g >
            <g filter="url(&quot;#dilate_shape&quot;)">
                <rect width="315" height="600" x="120" y="27" fill="blue" fill-opacity="0.2" clip-path="url(&quot;#SvgjsClipPath1019&quot;)"></rect>
            </g>
            <image xlink:href="https://www.dropbox.com/pri/get/697%20%5BConverted%5D.svg?_subject_uid=360738345&amp;raw=1&amp;size=1280x960&amp;size_mode=3&amp;w=AADJZ7-5-jq5Qyh2urbHo_G1FCn0ADHB-Li1KOFGuAEEQQ" transform="translate(278.34 410.34) scale(1.66 1.66)"  x="-75" y="-75" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" width="150" height="150"  ></image>
        </g>
    </svg>

SVG (已剪辑)

Clipped

XML

<svg id="SvgjsSvg1006" width="550" height="650" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs">
    <defs id="SvgjsDefs1007">
        <clipPath id="SvgjsClipPath1019">
            <rect id="SvgjsRect1016" width="315" height="600" x="120" y="27"></rect>
        </clipPath>
        <filter id="dilate_shape">
            <feMorphology operator="dilate" in="SourceGraphic" radius="5" />
        </filter>
    </defs><!--?xml version="1.0" encoding="UTF-8" standalone="no" ?-->
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="550" height="650" viewBox="0 0 550 650" xml:space="preserve">
        <g >
            <g filter="url(&quot;#dilate_shape&quot;)">
                <rect width="315" height="600" x="120" y="27" fill="blue" fill-opacity="0.2" clip-path="url(&quot;#SvgjsClipPath1019&quot;)"></rect>
            </g>
            <image xlink:href="https://www.dropbox.com/pri/get/697%20%5BConverted%5D.svg?_subject_uid=360738345&amp;raw=1&amp;size=1280x960&amp;size_mode=3&amp;w=AADJZ7-5-jq5Qyh2urbHo_G1FCn0ADHB-Li1KOFGuAEEQQ" transform="translate(278.34 410.34) scale(1.66 1.66)"  x="-75" y="-75" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" width="150" height="150"  clip-path="url(&quot;#SvgjsClipPath1019&quot;)"></image>
        </g>
   </svg>

唯一不同的是我将 clip-path =“url(”#SvgjsClipPath1019“)”添加到第二个svg的图像标记中

1 个答案:

答案 0 :(得分:2)

错误在于transformclip-path属性的应用顺序。 transform始终是最后一个操作,clip-path应用于未转换的元素。

此代码段

<image xlink:href="..." x="-75" y="-75"
       transform="translate(278.34 410.34) scale(1.66 1.66)"
       clip-path="url(#SvgjsClipPath1019)" />

相当于

<g transform="translate(278.34 410.34) scale(1.66 1.66)">
    <image xlink:href="..." x="-75" y="-75"
           clip-path="url(#SvgjsClipPath1019)" />
</g>

虽然你想实现这个目标:

<g clip-path="url(#SvgjsClipPath1019)">
    <image xlink:href="..." x="-75" y="-75"
       transform="translate(278.34 410.34) scale(1.66 1.66)"> />
</g>