javascript - SVG 通过 Javascript 启动和编辑 animateTransform

标签 javascript animation svg

我想通过变换矩阵(即时决定)变换 SVG Canvas 上的元素。我可以使用 JQuery-SVG animate() 来完成,但结果一点也不流畅。所以我想使用原生的 SVG animateTransform,问题是:

  • 我怎样才能让它在我想要的时候开始(也许是 beginElement()),以及
  • 如何即时设置矩阵的参数?

提前致谢:D

最佳答案

感谢您的回答! 因为我想使用原生 SVG 动画,所以我找到了这个解决方案(仍然不能完美运行)。这是一种不存在的 animateTransform(attributeName="transform"type="matrix")

注意:我在 group.transform 中保留每个元素的 svg 转换,group.transform.matrix() 只返回该元素的转换矩阵。

我首先将这些元素添加到我想要设置动画的元素中:

<animateTransform id="canvTranslate" begin="indefinite" attributeName="transform" type="translate" to="" dur="1s" additive="sum" fill="freeze"/>
<animateTransform id="canvRotate" begin="indefinite" attributeName="transform" type="rotate" to="" dur="1s" additive="sum" fill="freeze"/>
<animateTransform id="canvScale" begin="indefinite" attributeName="transform" type="scale" to="" dur="1s" additive="sum" fill="freeze"/>

然后我做:

 var tMatrix = transformation.matrix(); //this is the transformation i want to obtain
    var cMatrix = group.transform.matrix(); //this is the actual CTM of the element

    //getting the animations
    var animTrans = document.getElementById('canvTranslate');
    var animRotaz = document.getElementById('canvRotate');
    var animScale = document.getElementById('canvScale');

    //setting duration (it's got somewhere before)
    animTrans.setAttribute('dur', duration/1000+'s');
    animRotaz.setAttribute('dur', duration/1000+'s');
    animScale.setAttribute('dur', duration/1000+'s');

    //calculating the 'from' attribute
    var transX = cMatrix.e;
    var transY = cMatrix.f;
    var scaleX = Math.sqrt(Math.pow(cMatrix.a, 2)+Math.pow(cMatrix.b, 2));
        var rotate = Math.atan(cMatrix.c/cMatrix.d);

    animTrans.setAttribute('from', transX+','+transY);
    animRotaz.setAttribute('from', -rotate*180/Math.PI);
    animScale.setAttribute('from', scaleX);
    //end 'from'

    //calculating the 'to' attribute to set
    var transX = tMatrix.e;
    var transY = tMatrix.f;
    var scaleX = Math.sqrt(Math.pow(tMatrix.a, 2)+Math.pow(tMatrix.b, 2));
    var rotate = Math.atan(tMatrix.c/tMatrix.d);

    animTrans.setAttribute('to', transX+','+transY);
    animRotaz.setAttribute('to', -rotate*180/Math.PI);
    animScale.setAttribute('to', scaleX);
    //end 'to'

    animTrans.beginElement();
    animRotaz.beginElement();
    animScale.beginElement();

group.transform = transformation; 最后,更新元素的 transform 属性:

    setTimeout(function(){ //i will change this somehow better :)

        //this is a problematic step. with it animations work on Chrome, without it they work good on firefox and opera too
        $(group).attr('transform', 'matrix('+tMatrix.a+','+tMatrix.b+','+tMatrix.c+','+tMatrix.d+','+tMatrix.e+','+tMatrix.f+')');
    }, duration+100);

最后一步是有问题的。我不明白为什么它在 Chrome 中工作正常,而动画最终在 Firefox 和 Opera 中缩放得更多(不调用 setTimeout 工作得很好)。

关于javascript - SVG 通过 Javascript 启动和编辑 animateTransform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8833290/

相关文章:

javascript - 向多个服务器发送 Ajax 请求?

javascript - 使用 axios 响应在 vuejs 中设置数据对象

html - 屏蔽视频 SVG 形状

css - 悬停时动画 SVG 图像蒙版

javascript - 使用 jQuery 内联 SVG 中的目标 id

javascript - 对 ng-options 的误解

javascript - 使图像向上跳跃(并向后下降)

ios - 使用 UILabel 的持续时间动画表现得很奇怪

JavaScript 无法在下面的 html 代码中被读取

javascript - addEventListener 与 onclick