使SVG变换矩阵绕其中心旋转

编程入门 行业动态 更新时间:2024-10-27 06:26:46
本文介绍了使SVG变换矩阵绕其中心旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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 +')' )

现在我只想对矩阵做同样的事情.我正在使用西尔维斯特(Sylvester)将矩阵相乘.

Now i want to do the same only with a matrix. I'm using sylvester to multiply the matrixes.

我摆弄一个小提琴来弄清楚这个问题:)

I made a fiddle to make the question clear :)

jsfiddle/xYsHZ/3/

我希望红色矩形的行为与绿色矩形相同.我在做什么错了?

I want the red rect to behave the same as the green rect. What am i doing wrong?

推荐答案

已修复!矩阵元素的顺序是错误的:)

Fixed!! the sequence of the matrix elements was wrong :)

$("#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);

jsfiddle/6DR3D/2/

更多推荐

使SVG变换矩阵绕其中心旋转

本文发布于:2023-10-29 20:33:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   中心   SVG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!