HTML
<rect id="red" style="fill: red;" height="100" width="20"></rect>
JS
var layer =
{
sizeReal : { "width": 20, "height": 100 }
, sizeScaled : { "width": 10, "height": 50 }
, position : { "x": 200, "y": 200 }
, scale : 0.5
, rotation : 0
, matrix : [ 1, 0, 0, 1, 0, 0 ]
};
// Not sure if its the cleanest way but it works it rotates itself arounds its center.
//
$("#red")[0].setAttribute( 'transform', 'translate(' + layer.position.x + ',' + layer.position.y +') rotate(' + layer.rotation +',' + ( layer.sizeScaled.width / 2 ) + ',' + ( layer.sizeScaled.height / 2 ) + ') scale(' + layer.scale + ',' + layer.scale +')' )
上面的示例通过应用变换来更改 SVG 矩形。我想做同样的事情,使用矩阵。我正在使用 Sylvester 来乘以矩阵。我做了一个 fiddle 来弄清楚这个问题:)
我希望红色矩形的行为与绿色矩形相同。我做错了什么?
最佳答案
已修复!矩阵元素的顺序是错误的:)
$("#red")[0].setAttribute( 'transform', 'matrix(' + layer.matrix[ 0 ][ 0 ] + ',' + layer.matrix[ 1 ][ 0 ] + ',' + layer.matrix[ 0 ][ 1 ] + ',' + layer.matrix[ 1 ][ 1 ] + ',' + layer.matrix[ 0 ][ 2 ] + ',' + layer.matrix[ 1 ][ 2 ] + ')' );
},50);
关于javascript - 使 SVG 变换矩阵围绕其中心旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13663343/