【问题标题】:Svg matrix decompositionSVG矩阵分解
【发布时间】:2012-01-31 15:33:54
【问题描述】:

在 svg 中,我们有方法 element.getCTM(),它返回 SVGMatrix

[a c e][b d f][0 0 1] 

我想根据这个矩阵计算 sx 、 sy 和旋转角度。

【问题讨论】:

  • 帮我解决这个问题。

标签: matrix svg rotation angle decomposition


【解决方案1】:

关于这个主题有很多要阅读和学习的地方。我将给出一个基本的答案,但请注意,如果您尝试制作游戏或动画,这不是这样做的方式。

a == sxd == sy,因此您可以像这样访问它们:

var r, ctm, sx, sy, rotation;

r   = document.querySelector('rect'); // access the first rect element
ctm = r.getCTM();
sx  = ctm.a;
sy  = ctm.d;

现在轮换a == cos(angle)b == sin(angle)。 Asin 和 acos 不能单独为您提供完整的角度,但它们一起可以。从tan = sin/cos 开始,您就想使用atan,而对于这种问题,您实际上想使用atan2

RAD2DEG = 180 / Math.PI;
rotation = Math.atan2( ctm.b, ctm.a ) * RAD2DEG;

如果您研究过inverse trigonometric functionsunit circle,您就会明白为什么会这样。

这是 W3C 关于 SVG 转换的不可或缺的资源:@​​987654323@。向下滚动一点,您可以阅读更多关于我上面提到的内容。

更新,示例用法如何以编程方式制作动画。将转换单独存储,并在更新这些转换时覆盖/更新 SVG 元素转换。

var SVG, domElement, ...

// setup
SVG        = document.querySelector( 'svg' );
domElement = SVG.querySelector( 'rect' );
transform  = SVG.createSVGTransform();
matrix     = SVG.createSVGMatrix();
position   = SVG.createSVGPoint();
rotation   = 0;
scale      = 1;

// do every update, continuous use
matrix.a = scale;
matrix.d = scale;
matrix.e = position.x;
matrix.f = position.y;

transform.setMatrix( matrix.rotate( rotation ) );
domElement.transform.baseVal.initialize( transform ); // clear then put

【讨论】:

  • 如何找到 svg 动画的分解值?
  • 你想做哪些动画?你需要什么价值观?请描述更多。
  • 我想说,如果我有一个 svg 动画,其中许多动画节点(如 animateTransform(translate, scale, rotate) 和 animateMotion)都附加到容器节点。因此,如果我想将此动画简化为单个 translate 、 rotate 、 scale 和 skew ,这可以在 ctm 的帮助下实现,因为 ctm 是组合变换矩阵。问题是我应该在 translate 、 rotate 、 scale 和 skew 中输入什么值节点以获取当前转换。
  • 这需要大量的工作。当您使用animateTransformanimateMotion 和其他动画元素时,您不应该以编程方式控制它们。如果你想以编程方式控制动画,你想直接使用元素变换。我用一个例子更新了我上面的答案。
  • 我找到了使用 SVD 的解决方案,它给了我像 A = USV* 这样的矩阵,其中 U 和 V* 是旋转矩阵,S 是比例矩阵。假设 U 的第一行是 [a11 a12 ] 和第二行是 [a21 a22] 。如何获得围绕哪个坐标系轴旋转的角度?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多