Is it possible to make a SVG clipPath with a <path> responsive?

我的未来我决定 提交于 2019-12-01 07:17:23

问题


I am trying to make a responsive SVG with a bitmap pattern and a sort of complex shape with clipPath.

If I use a <polygon> instead of a <path> to create simpler shapes they are responsive, but I haven't achieved the same using a path, any ideas on how to achieve this or even if is possible?

Here's the code I have and a working demo.

<!-- Path Test-->
<div class="mainContainer">
  <div class="assetContainer">
    <div class="asset">
      <svg width="0" height="0">
        <defs>
          <clipPath id="pathTest">
            <path d="M 0.1562378,0.5 H 323.76366 c 0,0 -20.4244,6.3661 -21.48541,47.4801 -1.06101,41.1141 19.8939,42.7056 21.22016,43.2361 1.32626,0.5305 -323.3421722,0 -323.3421722,0 0,0 19.6286472,-1.8568 19.8938992,-42.9708 C 20.315389,7.1313 0.1562378,0.5 0.1562378,0.5 Z"
            />
          </clipPath>

          <pattern x="0" y="0" width="84" height="70" id="img7" patternUnits="userSpaceOnUse">
            <image xlink:href="http://i.imgur.com/T66UMJs.png" width="84" height="70" x="0" y="0" />
          </pattern>
        </defs>
        <rect x="0" y="0" width="100%" height="100%" fill="url(#img7)" clip-path="url(#pathTest)" />
      </svg>
    </div>
  </div>
</div>


/* CSS */

svg {
  position: relative;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.assetContainer {
  overflow: visible;
  height: 100px;
  position: relative;
  width: 100%;
}

.asset {
  position: absolute;
  width: 100%;
  height: 70px;
  bottom: 0px;
  left: 0px;
  cursor: pointer;
  z-index: auto;
  left: 0px;
}

.container {
  width: 400px;
  height: 400px;
  background: gray;
  position: relative;
  margin-bottom: 20px;
}

.mainContainer {
  background: rebeccapurple;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100px;
  position: absolute;
  //overflow: hidden;
}

回答1:


Convert your <clipPath> to use clipPathUnits="objectBoundingBox". When using objectBoundingBox units, your clip path coords map to the bounding box of the element they are being applied to. (0,0) maps to the top left of the element. (1,1) maps to the bottom right.

You'll need to redefine your path (or apply a transform to convert the coords to this range). But once you do, it will automatically scale to fit whatever you apply it to.



来源:https://stackoverflow.com/questions/41752027/is-it-possible-to-make-a-svg-clippath-with-a-path-responsive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!