SVG通过Javascript启动和编辑animateTransform
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了SVG通过Javascript启动和编辑animateTransform,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2809字,纯文字阅读大概需要5分钟。
内容图文
![SVG通过Javascript启动和编辑animateTransform](/upload/InfoBanner/zyjiaocheng/655/23eebea81e7b430080a41ccc5229fc58.jpg)
我想通过转换矩阵(由飞行决定)来转换SVG画布上的元素.我可以使用JQuery-SVG animate()做到这一点,但根本无法实现平滑效果.所以我想使用原生的SVG animateTransform,问题是:
>如何在需要时启动它(可能是beginElement()),并且
>如何动态地从矩阵设置参数?
提前致谢
解决方法:
感谢您的回答!
由于我想使用本机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 =转换;最后,更新元素的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的情况下效果很好).
内容总结
以上是互联网集市为您收集整理的SVG通过Javascript启动和编辑animateTransform全部内容,希望文章能够帮你解决SVG通过Javascript启动和编辑animateTransform所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。