我想为SVG中的文本蒙版(必须是蒙版)制作动画(以简化起见,假定为Scale +)。 我正在使用GSAP来实现这一目标。
我的动画只能在Edge和Chrome上正常播放,而不能在Firefox上播放。
var tl = new gsap.timeline();
tl.add(
gsap.to("#masktext", 3, {scale: 3, "text-anchor": "middle", transformOrigin: "50% 50%"}),
"0"
);
.svg-container {
font-size: 13rem;
font-weight:bold;
width: 100%;
height: 100%;
max-height:700px;
position: absolute;
top: 0;
}
#mysvg {
width: 100%;
height: 100%;
overflow: visible;
display: block;
}
#masktext {
text-anchor: middle;
transform-origin: center center;
}
#maskrect {
fill: white;
}
#myrect {
fill: white;
-webkit-mask: url(#mask);
mask: url(#mask);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.2.4/gsap.min.js"></script>
<div class="svg-container">
<svg id="mysvg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 700" width="1920px" height="700px" preserveAspectRatio="xMidYMid meet">
<defs>
<mask id="mask" x="0" y="0" width="1920" height="700" maskUnits="userSpaceOnUse" maskContentUnits="userSpaceOnUse" preserveAspectRatio="xMidYMid slice">
<rect id="maskrect" x="0" y="0" width="1920" height="700"/>
<text id="masktext" x="50%" y="50%">Creative</text>
</mask>
</defs>
<rect id="myrect" x="0" y="0" width="1920" height="700" style="fill:green;"/>
</svg>
</div>
https://jsfiddle.net/yumigo/qcrawe2g/
我的猜测是,在Firefox下,文本的缩放位置错误/超出范围并破坏了动画。一直在搜索转换原点和不同的初始文本位置,没有运气。
答案 0 :(得分:1)
有一些问题:
这是使用svgOrigin的固定版本,因此它是绝对的,并且不依赖于诸如“ 50%50%”之类的相对起源的东西:
https://jsfiddle.net/rxva8gnu/1/
// use svgOrigin so that it's absolute and you don't need to rely on relative stuff like "50% 50%"
gsap.to("#masktext", {scale: 3, duration:3, textAnchor: "middle", svgOrigin: "960 260"});
gsap.to("#myrect", {x:"+=0.001", duration: 3}); // to work around another Firefox bug that won't repaint with the newly-transformed mask unless the myrect also has a changed property.
有帮助吗?