如何在没有宽高比的情况下在SVG内保持图像的宽高比

时间:2017-11-16 00:56:55

标签: css svg sass cross-browser

我有一个SVG正常运行作为屏蔽图像的剪切路径。 SVG形状具有preserveAspectRatio =" none"并且表现得我想要它(全宽但保持静态高度)。

问题在于我剪裁的图像是与SVG一起拉伸和压扁,而我喜欢它与背景图像的背景图像相同:覆盖; (总是填充SVG,必要时裁剪,但保持纵横比)。

是否可以通过跨浏览器兼容性(最新的Chrome,Firefox,Safari和Internat Explorer)执行此操作?

HTML:

<div class="outer-wrapper">
  <div class="svg-wrapper">
    <!-- hidden SVG -->
    <svg class="shadow-svg">
      <!-- create drop shadow -->
      <filter id="drop-shadow">
        <feGaussianBlur in="SourceAlpha" stdDeviation="5"/>
        <feOffset dx="0" dy="0" result="offsetblur"/>
        <feFlood flood-color="rgb(0,0,0)" flood-opacity="0.75"/>
        <feComposite in2="offsetblur" operator="in"/>
        <feMerge>
          <feMergeNode/>
          <feMergeNode in="SourceGraphic"/>
        </feMerge>
      </filter>
      <!-- create shape-->
      <symbol id="shadow" viewBox="0 0 100 100" style="enable-background:new 0 0 100 100;" xml:space="preserve">
        <defs>
          <polygon id="clipShape" points="0 0 0 260 20 260 20 210 1350 210 1350 260 1370 260 1370 0 0 0" style="filter:url(#dropshadow)"/>
        </defs>
      </symbol>
      <!-- set shape as clipping path -->
      <clipPath id="clipPath">
        <use xlink:href="#clipShape" />
      </clipPath>
    </svg>
    <!-- visible SVG -->
    <svg class="shape-svg" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1370 260" enable-background="new 0 0 1370 260" xml:space="preserve" preserveAspectRatio="none">
      <!-- add drop shadow -->
      <use xlink:href="#shadow" />
      <!-- add clipping path shape -->
      <g transform="matrix(1 0 0 1 0 0)" clip-path="url(#clipPath)" >
        <!-- add image inside clipping path shape -->
        <image width="1370" height="260" xlink:href="https://www.unsplash.it/1370/260" transform="matrix(1 0 0 1 0 0)"></image>
      </g>
    </svg>
  </div><!-- .svg-wrapper -->
  <!-- text block -->
  <div class="header-text">
    <h2>Test Header Placeholder</h2>
    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Velit exercitationem quibusdam sed natus blanditiis quia assumenda, magni tenetur veritatis itaque iure explicabo veniam maiores magnam architecto et corporis possimus.</p>    
  </div>
</div><!-- .outer-wrapper -->

SCSS:

* {
  margin: 0;
  padding: 0;
}

body {
  height: 100vh;
  background: repeating-linear-gradient(-45deg, palegreen, palegreen 3px, lightgreen 3px, lightgreen 40px);
}

.outer-wrapper {
  display: block;
  width: 100%;
  margin: 0 auto;
}

.svg-wrapper {
  width: 96%;
  height: 150px;
  margin: 0 auto;
}

.shadow-svg {
  width: 0;
  height: 0;
  position: absolute;
}

.shape-svg {
  width: 100%;
  height: 150px;
  filter: url(#drop-shadow);
}

.header-text {
  width: 90%;
  margin: -20px auto 0;
  font-family: monaco, courier;
}

CodePen Here

注意:它目前无法在Firefox或IE11中运行。我也非常感谢您在这些浏览器中实现所有这些功能。

谢谢。

2 个答案:

答案 0 :(得分:0)

关于宽高比

您可以将svg设置为div的背景图像。用css打造它的风格; background-size: cover;background-position: center bottom;可能会做到这一点。

以下是问题的答案&#34;为什么它在MS-Edge中不起作用?&#34;。

  • <defs>元素中定义clipPath。
  • defs元素应该是引用元素的祖先之一的直接子元素。在您的示例中不是这种情况。将defs元素放置为<svg>元素的子元素始终有效。资料来源:https://www.w3.org/TR/SVG/struct.html#DefsElement
  • 生活规则:尽可能保持简单。这意味着不使用不必要的引用。只需显示<clipPath>元素内的剪切路径。 <defs>元素最终看起来像这样,并且是<svg>元素的子元素。

&#13;
&#13;
<defs>
  <!-- set shape as clipping path -->
  <clipPath id="clipPath">
    <polygon points="0 0 0 260 20 260 20 210 1350 210 1350 260 1370 260 1370 0 0 0"/>
  </clipPath>
</defs>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

可以使用CSS中的mask属性代替使用标签。它的行为类似于背景属性。

我尝试了这个,并解决了问题。 https://clipping-masking.webflow.io/

.masked {
  mask-image: url(domain.com/image.sv ), none;
  mask-size: contain;
  mask-repeat: no-repeat;
  mask-position: center;
}

相关问题