JavaScript IE 旋转变换数学

发布于 2024-10-18 07:47:16 字数 874 浏览 5 评论 0原文

我正在使用 JavaScript 设置元素的旋转,我知道如果你想设置为 90 度角,这很容易实现,但这不是我想要的。因为我想设置为奇怪的角度,例如 20 度,所以我需要使用变换滤镜。我知道这个的布局,但我想知道如何计算这四个值,起初我尝试了这个。

calSin = Math.sin(parseInt(css[c]));
calCos = Math.cos(parseInt(css[c]));
element.style.filter = 'progid:DXImageTransform.Microsoft.Matrix(M11=' + calCos + ', M12=-' + calSin + ',M21=' + calSin + ', M22=' + calCos + ', sizingMethod="auto expand")';

但正如你所看到的,这永远不会起作用,我只是在黑暗中尝试。由于数学不是我的强项,我想知道是否有人可以帮助我计算这些值?

谢谢。

编辑

好的!使用下面的代码就可以了。

radians = parseInt(css[c]) * Math.PI * 2 / 360;
calSin = Math.sin(radians);
calCos = Math.cos(radians);
element.style.filter = 'progid:DXImageTransform.Microsoft.Matrix(M11=' + calCos + ', M12=-' + calSin + ',M21=' + calSin + ', M22=' + calCos + ', sizingMethod="auto expand")';

但现在的问题是如何让它从中心而不是左上角旋转?

I am working on setting the rotation of an element with JavaScript, I know this is easy to achieve if you want to set to a 90 degree angle but this is not what I want. Because I want to set to strange angles such as 20 degrees I need to use the transform filter. I know the layout of this but I was wondering how I calculate the four values, at first I tried this.

calSin = Math.sin(parseInt(css[c]));
calCos = Math.cos(parseInt(css[c]));
element.style.filter = 'progid:DXImageTransform.Microsoft.Matrix(M11=' + calCos + ', M12=-' + calSin + ',M21=' + calSin + ', M22=' + calCos + ', sizingMethod="auto expand")';

But as you can see, this was never going to work, I was just taking a stab in the dark. Due to maths not being my strong point I was wondering if anyone could help me calculate the values?

Thanks.

EDIT

Okay! Got it working with the following code.

radians = parseInt(css[c]) * Math.PI * 2 / 360;
calSin = Math.sin(radians);
calCos = Math.cos(radians);
element.style.filter = 'progid:DXImageTransform.Microsoft.Matrix(M11=' + calCos + ', M12=-' + calSin + ',M21=' + calSin + ', M22=' + calCos + ', sizingMethod="auto expand")';

But now the question is how do I make it rotate from the center rather than top left?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘紸啶 2024-10-25 07:47:16

实际上有一种方法可以仅使用 ie 矩阵变换过滤器来完成您想要做的事情。

除了M11、M12、M21、M22之外,矩阵还有两个附加属性:Dx和Dy用于平移。不幸的是,你不能只翻译一个恒定的量。为了围绕对象的中心旋转,需要组合三个变换。

首先,将对象的中心平移到原点。其次,旋转物体。第三,从原点平移回中心所在的位置。

IE本身不具备组合多个矩阵的能力,所以你必须自己做线性代数。假设 theta 是要旋转的角度,并假设对象的宽度为 2w,高度为 2h。 (所以 w 和 h 分别是宽度和高度的一半。)让 c 和 s 分别代表 cos(theta) 和 sin(theta)。

您想要计算的矩阵乘积为:

    [[1 0 w] [[c -s 0] [[1 0 -w]
     [0 1 h]  [s  c 0]  [0 1 -h]
     [0 0 1]] [0  0 1]] [0 0  1]]

等于:

    [[ c           -s           (-wc + hs + w)]
     [ s            c           (-ws - hc + h)]
     [ 0            0                   1     ]]

因此,如果您计算两个量 (-wc + hs + w) 和 (-ws -hc + h) 并将它们分别作为 Dx 和 Dy 添加到过滤器中,最终结果将是围绕对象的中心旋转。

我已经对此进行了编码并针对一个项目进行了测试,在该项目中我需要为一个对象绕其中心旋转设置动画,这对我有用。

There actually is a way to do what you're trying to do using just the ie matrix transformation filter.

in addition to M11, M12, M21, M22, the matrix also has two additional properties: Dx and Dy for translation. Unfortunately, you can't just translate by a constant amount. In order to rotate about the center of the object, you need to compose three transformation.

First, translate the center of the object to the origin. Second, rotate the object. Third, translate from the origin back to where the center belongs.

IE does not have the ability to compose multiple matrices itself, so you have to do the linear algebra yourself. Suppose theta is the angle by which you want to rotate, and suppose that the object's width is 2w while its height is 2h. (So w and h are the HALF width and height respectively.) Let c and s stand for cos(theta) and sin(theta) respectively.

The matrix product you want to compute is then:

    [[1 0 w] [[c -s 0] [[1 0 -w]
     [0 1 h]  [s  c 0]  [0 1 -h]
     [0 0 1]] [0  0 1]] [0 0  1]]

which equals:

    [[ c           -s           (-wc + hs + w)]
     [ s            c           (-ws - hc + h)]
     [ 0            0                   1     ]]

So, if you compute the two quantities (-wc + hs + w) and (-ws -hc + h) and add them to your filter as Dx and Dy respectively, the end result will be that the rotation is about the center of the object.

I have coded this up and tested it for a project in which I needed to animate having an object rotate about its center, and this worked for me.

鹊巢 2024-10-25 07:47:16

要从中心旋转,您需要将 DxDy 添加到 IE 过滤器,然后通过 css 添加位移并添加 css hacks增加 IE 中元素的宽度和高度。

你可以在我的网站上看看它是如何组合的: http://kirilloid.ru/

实际上这是更多与 CSS 相关的问题,而不是 JavaScript。

For rotate from the center, you'll need to add Dx and Dy to IE filter, then add displacement via css and add css hacks to increase elements' width and height in IE.

You may look how it's combined on my site: http://kirilloid.ru/

Actually this is more CSS-related question, than javascript.

一个人的夜不怕黑 2024-10-25 07:47:16

您应该检查 https://github.com/ heygrady/transform/wiki/ Correcting-transform-origin-and-translate-in-ie 用于全面的 50%,50% 变换源 IE 破解。请注意,最后一个函数中有一些拼写错误,这是正确的块:

var matrices = {
    tl: matrix.x($M([0, 0, 1])),
    bl: matrix.x($M([0, y, 1])),
    tr: matrix.x($M([x, 0, 1])),
    br: matrix.x($M([x, y, 1]))
};

您还需要 Sylvester 库:http ://sylvester.jcoglan.com/

HTH

You should check https://github.com/heygrady/transform/wiki/correcting-transform-origin-and-translate-in-ie for a comprehensive 50%,50% transform origin IE hack. Be careful, there are a few typos in the last function, here is the correct block:

var matrices = {
    tl: matrix.x($M([0, 0, 1])),
    bl: matrix.x($M([0, y, 1])),
    tr: matrix.x($M([x, 0, 1])),
    br: matrix.x($M([x, y, 1]))
};

You will also need the Sylvester library: http://sylvester.jcoglan.com/

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文