【问题标题】:How to crop a particular area of image with SVG clipping mask如何使用 SVG 剪贴蒙版裁剪图像的特定区域
【发布时间】:2016-06-03 11:23:26
【问题描述】:

我正在通过 SVG 剪辑路径制作剪辑蒙版。我正在使用一个带有三个剪贴蒙版的 SVG 元素。

问题是我的 svg 得到了完整的窗口宽度和高度,因此图像也得到了完整的宽度和高度。

所以我想要的是将图像移动到剪切路径内,以便我可以显示图像的实际区域

这是Jsfiddle 链接。

我不想更改图像的纵横比.. 请让我知道是否有任何方法。

谢谢

【问题讨论】:

    标签: html svg clip-path image-clipping


    【解决方案1】:

    如果您不希望图像拉伸和破坏其纵横比,请不要使用preserveAspectRatio="none"。您可能想要使用"xxx slice" 变体之一。下面我为每张图片使用了不同的图片,以使它们分别与左、中和右对齐。

    *{padding:0px; margin: 0px;}
    html, body {width: 100%; height: 100%;}
    <svg version="1.1" id="triagnleSvg" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
                 xml:space="preserve">
        <clipPath id="last" clipPathUnits="objectBoundingBox">
            <polygon points="1,0 .78,0 0.58,1 1,1"/>
        </clipPath>
        <clipPath id="first" clipPathUnits="objectBoundingBox">
            <polygon points="0,0 0.4,0 0.218,1 0,1"/>
        </clipPath>
        <clipPath id="mid" clipPathUnits="objectBoundingBox">
            <polygon points="0.4,0 0.78,0 0.58,1 0.218,1"/>
        </clipPath>
        <image class="topImage" clip-path="url(#mid)" preserveAspectRatio="xMidYMid slice" width="100%" height="100%" xlink:href="http://testyourprojects.net/matrix/project/stackoverflowissue/kit3.jpg"/>
        <!-- top -->
        <image class="leftImage" clip-path="url(#first)" preserveAspectRatio="xMinYMid slice" width="100%" height="100%" xlink:href="http://testyourprojects.net/matrix/project/stackoverflowissue/kit2.jpg"/>
        <!-- right -->
        <image class="rightImage" clip-path="url(#last)" preserveAspectRatio="xMaxYMid slice" width="100%" height="100%" xlink:href="http://testyourprojects.net/matrix/project/stackoverflowissue/kit1.jpg"/>
        </svg>

    更新

    [我] 仍然无法获得第一张图片的确切视图。我想展示女孩的脸。但它没有发生

    一种解决方案是更改您的图像,使感兴趣的区域位于图像的所需部分。

    否则您需要开始使用视图框。但是,一旦涉及viewBox,您就不能再按照原始示例拉伸父 SVG 以填充页面宽度和高度。您必须对整个图像的固定纵横比感到满意。

    这是解决问题的一种方法:

    *{padding:0px; margin: 0px;}
    html, body {width: 100%; height: 100%;}
    <svg version="1.1" id="triagnleSvg" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 2 1">
      <defs>
        <clipPath id="mid" clipPathUnits="objectBoundingBox">
            <polygon points="0.3,0 1,0 0.7,1 0,1"/>
        </clipPath>
      </defs>
    
      <image class="leftImage" preserveAspectRatio="xMaxYMid slice" width="1" height="1"
             xlink:href="http://testyourprojects.net/matrix/project/stackoverflowissue/kit2.jpg"/>
    
      <image class="rightImage" preserveAspectRatio="xMidYMid slice" x="1" width="1" height="1"
             xlink:href="http://testyourprojects.net/matrix/project/stackoverflowissue/kit1.jpg"/>
    
      <image class="topImage" preserveAspectRatio="xMidYMid slice" x="0.5" width="1" height="1"
             xlink:href="http://testyourprojects.net/matrix/project/stackoverflowissue/kit3.jpg"
             clip-path="url(#mid)"/>
    
    </svg>

    【讨论】:

    • 感谢 Paul 的回复,根据您的想法对其进行了一些管理,但仍然无法获得第一张图像的确切视图。我想展示女孩的脸。但它没有发生。
    【解决方案2】:

    您可能想要更改图像的x(也可能是y),并将相反的翻译应用到它们相应的剪切路径。

    <!-- language: lang-html -->
    
    <svg version="1.1" id="triagnleSvg" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" preserveAspectRatio="none"
             xml:space="preserve">
        <clipPath id="last" clipPathUnits="objectBoundingBox" transform="translate(-200,0)">
            <polygon points="1,0 .78,0 0.58,1 1,1"/>
        </clipPath>
        <clipPath id="first" clipPathUnits="objectBoundingBox" transform="translate(450,0)">
            <polygon points="0,0 0.4,0 0.218,1 0,1"/>
        </clipPath>
        <clipPath id="mid" clipPathUnits="objectBoundingBox">
            <polygon points="0.4,0 0.78,0 0.58,1 0.218,1"/>
        </clipPath>
        <image class="topImage" clip-path="url(#mid)" width="100%" height="100%" xlink:href="http://testyourprojects.net/matrix/project/stackoverflowissue/kit3.jpg"/>
        <!-- top -->
        <image class="leftImage" clip-path="url(#first)" width="100%" height="100%" x="-450" xlink:href="http://testyourprojects.net/matrix/project/stackoverflowissue/kit2.jpg"/>
        <!-- right -->
        <image class="rightImage" clip-path="url(#last)" width="100%" height="100%" x="200" xlink:href="http://testyourprojects.net/matrix/project/stackoverflowissue/kit1.jpg"/>
    </svg>
    

    但是,如果您不固定 svg 或其容器的最小和最大宽度和高度,则可能会在其边界之外显示图像的空白部分。您可以在 svg 或其容器的 css 上设置 min-heightmax-heightmin-widthmax-width

    此外,由于显示感兴趣的区域取决于设备/浏览器的宽度,因此额外的增强功能是根据用户的宽度自动计算精确的平移以使用脚本将感兴趣的区域居中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2019-06-18
      • 2021-10-02
      • 1970-01-01
      相关资源
      最近更新 更多