【问题标题】:animated svg mask doesn't work in firefox动画 svg 蒙版在 Firefox 中不起作用
【发布时间】:2017-10-28 17:25:41
【问题描述】:

h1 大家,我正在尝试为 svg 元素制作基于 svg 掩码的跨浏览器动画。起初,我发现了一个 css 的错误,在 Firefox 中,掩码不适用于 css 的“宽度”和“高度”,只能用于内联属性。所以,下一个问题 - 如何为 firefox 制作蒙版动画,没有比“调整宽度”更难的了。 https://codepen.io/flyby/pen/KmYOgb

<div id='cont'>
  <svg>
    <defs>
      <mask id='rectMask' maskUnits='userSpaceOnUse' maskContentUnits='userSpaceOnUse' transform='scale(1)'>
        <rect x='0' y='0' id='maskRect' width='700' height='850'/>
      </mask>
    </defs>
    <path id='maskedPath' d='m 0,0 l 650,0 -100,850 -550,0 z' mask='url(#rectMask)'/>
    <path id='riverPath' d='m 653,0 l -100,850' mask='url(#rectMask)'/>
    <path id='notMaskedPath' d='m 655,0 l 650,0 0,850 -750,0 z'/>
  </svg>
</div>

CSS:

body {
  margin:0;
  padding:0;
  overflow:hidden;
}
#cont {
  width:100vw;
  height:100vh;
  background-color:rgb(50,50,50);
}
svg {
  width:100%;
  height:100%;
}
#maskedPath {
  stroke:none;
  fill:rgb(230,230,230);
}
#notMaskedPath {
  stroke:none;
  fill:rgb(230,230,230);
}
#riverPath {
  stroke:rgb(50,160,240);
  stroke-width:8;
  fill:none;
}
#maskRect {
  width:0px;
  height:850px;
  fill:white;
  stroke:none;
  animation: resize 3s linear infinite;
}
@-moz-keyframes resize {
  50% {width: 700px !important;}
  0%,100% {width: 0px !important;}
}

【问题讨论】:

  • 伙计们,wtf,wehre 都是网络大师,如何从我的代码中制作一个 sn-p?
  • 编辑框上方有一个小图标,用于创建sn-p。编辑器与 jsfiddle 非常相似。

标签: firefox animation svg mask


【解决方案1】:

正如您所发现的,您不能(还)设置几何属性,例如 Firefox 中 SVG 元素的 widthheight。这不是一个错误。这只是一个 SVG 2 的东西,Firefox 还没有实现,但 Chrome 已经实现了。

解决方案是使用内置的 SVG(“SMIL”)动画元素,而不是 CSS 动画。

body {
  margin:0;
  padding:0;
  overflow:hidden;
}
#cont {
  width:100vw;
  height:100vh;
  background-color:rgb(50,50,50);
}
svg {
  width:100%;
  height:100%;
}
#maskedPath {
  stroke:none;
  fill:rgb(230,230,230);
}
#notMaskedPath {
  stroke:none;
  fill:rgb(230,230,230);
}
#riverPath {
  stroke:rgb(50,160,240);
  stroke-width:8;
  fill:none;
}
#maskRect {
  fill:white;
  stroke:none;
}
<!--this animation doesn't work in FIREFOX, and not tested in IE11 ad Edge YET, WILL BE FIXED SOON (I HOPE)-->
<div id='cont'>
  <svg>
    <defs>
      <mask id='rectMask' maskUnits='userSpaceOnUse' maskContentUnits='userSpaceOnUse' transform='scale(1)'>
        <rect x='0' y='0' width="0" height="850" id='maskRect'>
          <animate attributeName="width"
                   keyTimes="0; 0.4; 0.5; 1"
                   values="0; 670; 670; 0"
                   dur="4s" repeatCount="indefinite"/>
        </rect>
      </mask>
    </defs>
    <path id='maskedPath' d='m 0,0 l 650,0 -100,850 -550,0 z' mask='url(#rectMask)'/>
    <path id='riverPath' d='m 653,0 l -100,850' mask='url(#rectMask)'/>
    <path id='notMaskedPath' d='m 655,0 l 650,0 0,850 -750,0 z'/>
  </svg>
</div>

【讨论】:

  • 对不起,m8!这是我的错,我忘了说 - 它必须是跨浏览器的多用途解决方案。如果我将使用 SMILL - 这是我在项目中使用蒙版复制我的所有事件,现在我明白了,这里没有关键帧“宽度”动画的单一解决方案。只有木腿...看起来,我需要尝试 jquery 动画,或更改“转换:翻译”上的“宽度”。
  • CSS 目前只能在 Chrome 中工作(也许是 Safari?)。 SMIL 可以在 Chrome 和 Firefox 中运行(如果您使用 FakeSmile polyfill,则可以在 IE 中使用)。它是最好的跨浏览器解决方案——也许不是 JS 动画库之一。
  • 已弃用...我讨厌生活在时代的边缘,SMILL 已弃用,CSS 不起作用。顺便说一句,CSS 在 Opera to® 中工作。我听说过假微笑,这是很老的 polyfill,谢谢。
  • AFAIK SMIL 动画在可预见的未来仍将保留在 Chrome 中。由于大量负面反馈,Chrome 无限期推迟了删除它的计划。
  • 我的解决方案——js setTimeout函数,仅此而已,
猜你喜欢
  • 2015-04-25
  • 2014-11-13
  • 2013-04-25
  • 2023-03-14
  • 2015-09-05
  • 2016-06-11
  • 1970-01-01
  • 2017-09-15
  • 2017-11-20
相关资源
最近更新 更多