我希望能够通过一个变换矩阵(即时决定)来转换SVG画布上的元素。我可以使用JQuery-SVG animate()来实现,但效果并不流畅。因此,我想使用原生的SVG animateTransform,并且问题是:
- 我如何在需要时启动它(可能使用beginElement()),以及
- 我如何即时设置矩阵参数?
<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,则正常工作)。
动画可以用许多不同的方式完成。
将动画元素添加到图形/形状元素中适用于预定义的动画。"animate elements" 提供了非常简短的解决方案,演示:http://jsfiddle.net/UjuR8
交互式动画需要更多手动解决方案,需要相当多的 Javascript 样板代码。您必须创建一个函数 render
,它将被requestAnimationFrame
每秒调用60次(请参见http://paulirish.com/2011/requestanimationframe-for-smart-animating/)。在render
中,您可以获取“当前变换矩阵”(CTM)并根据此应用更改。这是一个非常小的概念证明:http://jsfiddle.net/PaSD8/。
在大型项目中,我建议包装SVG元素,并可能不使用字符串连接进行动画,而是直接使用矩阵和变换进行工作。这是一个我拥有的示例类:
var SVG, Object2D;
SVG = document.querySelector( 'svg' );
// ...
Object2D = ( function () {
var proto;
function Object2D ( domElement ) {
this.domElement = domElement;
this.transform = SVG.createSVGTransform();
this.matrix = SVG.createSVGMatrix();
this.position = SVG.createSVGPoint();
this.rotation = 0;
this.scale = 1;
}
proto = Object2D.prototype;
proto.draw = function ( timestamp ) {
// update scale and position, apply rotation
var transform = this.transform,
matrix = this.matrix,
position = this.position,
rotation = this.rotation,
scale = this.scale;
matrix.a = scale;
matrix.d = scale;
matrix.e = position.x;
matrix.f = position.y;
transform.setMatrix( matrix.multiply( rotation ) );
this.domElement.transform.baseVal.initialize( transform ); // clear then put
};
return Object2D;
} )();