简体   繁体   中英

SVG.js: Animate matrix via morph() method

I would like to animate a transform matrix using SVG.js. There is a method listed, matrix.morph() , that is intended to do this. I can't make this happen.

Below is an example that attempts to apply this method, but it does not do anything. Could someone please show me how to animate a transform matrix using SVG.js. Thanks.

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>SVG.js - Rect Animate Matrix Morph</title> <script type="text/javascript" src="//svgDiscovery.com/SVG.js/svg.js"></script> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width"> </head> <body style='padding:10px;font-family:arial;'> <center> <h4>SVG.js - Rect Animate Animate Matrix Morph</h4> <table> <tr> <td> <div id="svgDiv" style='background-color:lightgreen;width:400px;height:400px;'></div> </td> </tr></table> <script> var mySVG = SVG('svgDiv').size(400, 400); var myRect = mySVG.rect(100,100).attr({x:150,y:150,id:'myRect',fill: '#f00','stroke-width':3,stroke:'black'}); var myRectMatrix = new SVG.Matrix var myRectMatrixMorph=myRectMatrix.morph(1,2,3,4,5,6) console.log(myRectMatrixMorph.toString()) </script> </body> </html> 

To animate the transformation of an element you do it the same way as setting a transform on the element - with the transform() method. You dont need to touch the morph methods yourself when animating.

Try:

el.animate().transform({rotation:25})
// or with matrix
el.animate().transform({a:1, b:0, c:0 .... })
// or with SVG.Matrix
el.animate().transform(new SVG.Matrix(1,0,0 ....))

The morph method is only for the case you want a matrix at a specific position. Like:

var m1 = new SVG.Matrix(1,0,0,1,0,0)
var m2 = new SVG.Matrix(2,0,0,2,0,0)
m1.morph(m2)
m1.at(0.5) // returns matrix (1.5,0,0,1.5,0,0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM