javascript - 使用 GSAP 制作 SVG 图案变换动画

标签 javascript animation svg

我的 svg 中有这个:

<rect fill="url(#texture)" width="100%" height="100%" />
<defs>
    <pattern id="texture" patternUnits="userSpaceOnUse" patternTransform="scale(0.5)" width="300" height="300">
        <image id="texture-image" xlink:href="texture.png" x="0" y="0" width="300" height="300" />
    </pattern>
</defs>

我想使用这个库 GSAP对“texture”的 patternTransform 属性进行动画处理,例如 scale(0.5) scale (0.8)。 但由于它不是普通的数字或颜色属性,而库可以轻松处理这些属性,因此我很难做到这一点。 我以不同的方式尝试了 rAF但实际上它也不起作用:

var svgTexture = document.getElementById('texture'),
    scaleValue, scaleLimit, increaseBy;

function animate(value, limit, step) {
    scaleValue = value;
    scaleLimit = limit;
    increaseBy = step;
    animateSvgTexture();
}

function animateSvgTexture() {
    var val = scaleValue - increaseBy;
    if ( val < scaleLimit ) {
      return false;
    }
    svgTexture.setAttribute('patternTransform', 'scale('+ val +')');
    requestAnimationFrame(animateSvgTexture);
}

/**
 * Provides requestAnimationFrame in a cross browser way.
 * @author paulirish / http://paulirish.com/
 */
if (!window.requestAnimationFrame) {
    window.requestAnimationFrame = (function() {
        return window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
                window.setTimeout( callback, 1000 / 60 );
        };
    })();
}

然后当我想开始动画时我称之为:

animate(0.3, 0.2, 0.01);

无论如何,我真的希望有人能够阐明如何使用 GSAP 来达到这个结果,使用类似这样的语法:

TweenLite.to("#texture", 1, { 
    patternTransform: 'scale(0.8)', 
    ease: Power2.easeInOut 
});

非常感谢

最佳答案

终于被我发现了。 如果有人需要它,请按以下步骤操作:

var svgTexture = document.getElementById('texture'),
    anim = { 'scale' : 0.5 }; // 0.5 is the initial default value

TweenLite.to(anim, 2, { 
    scale: 0.3, 
    onUpdate: function() {
        svgTexture.setAttribute('patternTransform', 'scale('+ anim.scale +')') 
    } 
});

关于javascript - 使用 GSAP 制作 SVG 图案变换动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21661126/

相关文章:

python - 如何使用 Mayavi 在 python 中制作 3D 数据动画

javascript - 轴上的自定义刻度大小(d3js)

javascript - 使用多个 promise 的区域符号 currentTask Ionic2 错误

javascript - getDate() slice() 混淆

javascript - 除非我使用 $timeout,否则指令中的 AngularJs 动画不起作用

JavaScript 对绝对定位元素从底部到每个原始位置进行动画处理

css - SVG CSS 过渡不适用于 `use` 元素 (Chrome)

python - 使 matplotlib 导出图形 svg 没有描边

javascript - knockout 变量未定义

javascript - 度量图形图表中的样式轴标签