admin管理员组文章数量:1026989
I have an element 3D transformed something like this:
.foo {
transform: rotateX(-30deg) rotateY(35deg);
}
Now I want to get these values via javascript. It's easy to get the 3D matrix:
var matrix = $('.foo').css('transform');
// return:
// matrix3d(0.819152, -0.286788, -0.496732, 0, 0, 0.866025, -0.5, 0, 0.573576, 0.409576, 0.709406, 0, 0, 0, 0, 1)
But is it possible to calculate CSS like values -30 and 35 with that matrix? I just found ways to do this for 2D transforms.
I have an element 3D transformed something like this:
.foo {
transform: rotateX(-30deg) rotateY(35deg);
}
Now I want to get these values via javascript. It's easy to get the 3D matrix:
var matrix = $('.foo').css('transform');
// return:
// matrix3d(0.819152, -0.286788, -0.496732, 0, 0, 0.866025, -0.5, 0, 0.573576, 0.409576, 0.709406, 0, 0, 0, 0, 1)
But is it possible to calculate CSS like values -30 and 35 with that matrix? I just found ways to do this for 2D transforms.
Share Improve this question asked Dec 24, 2015 at 9:56 Philipp KühnPhilipp Kühn 1,6592 gold badges19 silver badges26 bronze badges 1- Even though the question is straight-forward, the answer seems to be very broad and plex for the scope of a single answer. You may find these links helpful - 1 - W3C Spec, 2 - Gist by jsfiii, 3 - Mathematics of 3d rotation. – Harry Commented Dec 25, 2015 at 6:26
1 Answer
Reset to default 6Ok, got it!
var _getTransform = function($element) {
var matrix = $element.css('transform'),
rotateX = 0,
rotateY = 0,
rotateZ = 0;
if (matrix !== 'none') {
// do some magic
var values = matrix.split('(')[1].split(')')[0].split(','),
pi = Math.PI,
sinB = parseFloat(values[8]),
b = Math.round(Math.asin(sinB) * 180 / pi),
cosB = Math.cos(b * pi / 180),
matrixVal10 = parseFloat(values[9]),
a = Math.round(Math.asin(-matrixVal10 / cosB) * 180 / pi),
matrixVal1 = parseFloat(values[0]),
c = Math.round(Math.acos(matrixVal1 / cosB) * 180 / pi);
rotateX = a;
rotateY = b;
rotateZ = c;
}
return {
rotateX: rotateX,
rotateY: rotateY,
rotateZ: rotateZ
};
}
I have an element 3D transformed something like this:
.foo {
transform: rotateX(-30deg) rotateY(35deg);
}
Now I want to get these values via javascript. It's easy to get the 3D matrix:
var matrix = $('.foo').css('transform');
// return:
// matrix3d(0.819152, -0.286788, -0.496732, 0, 0, 0.866025, -0.5, 0, 0.573576, 0.409576, 0.709406, 0, 0, 0, 0, 1)
But is it possible to calculate CSS like values -30 and 35 with that matrix? I just found ways to do this for 2D transforms.
I have an element 3D transformed something like this:
.foo {
transform: rotateX(-30deg) rotateY(35deg);
}
Now I want to get these values via javascript. It's easy to get the 3D matrix:
var matrix = $('.foo').css('transform');
// return:
// matrix3d(0.819152, -0.286788, -0.496732, 0, 0, 0.866025, -0.5, 0, 0.573576, 0.409576, 0.709406, 0, 0, 0, 0, 1)
But is it possible to calculate CSS like values -30 and 35 with that matrix? I just found ways to do this for 2D transforms.
Share Improve this question asked Dec 24, 2015 at 9:56 Philipp KühnPhilipp Kühn 1,6592 gold badges19 silver badges26 bronze badges 1- Even though the question is straight-forward, the answer seems to be very broad and plex for the scope of a single answer. You may find these links helpful - 1 - W3C Spec, 2 - Gist by jsfiii, 3 - Mathematics of 3d rotation. – Harry Commented Dec 25, 2015 at 6:26
1 Answer
Reset to default 6Ok, got it!
var _getTransform = function($element) {
var matrix = $element.css('transform'),
rotateX = 0,
rotateY = 0,
rotateZ = 0;
if (matrix !== 'none') {
// do some magic
var values = matrix.split('(')[1].split(')')[0].split(','),
pi = Math.PI,
sinB = parseFloat(values[8]),
b = Math.round(Math.asin(sinB) * 180 / pi),
cosB = Math.cos(b * pi / 180),
matrixVal10 = parseFloat(values[9]),
a = Math.round(Math.asin(-matrixVal10 / cosB) * 180 / pi),
matrixVal1 = parseFloat(values[0]),
c = Math.round(Math.acos(matrixVal1 / cosB) * 180 / pi);
rotateX = a;
rotateY = b;
rotateZ = c;
}
return {
rotateX: rotateX,
rotateY: rotateY,
rotateZ: rotateZ
};
}
本文标签: Get 3D CSS rotation value from matrix3d() with JavaScriptStack Overflow
版权声明:本文标题:Get 3D CSS rotation value from matrix3d() with JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745653233a2161442.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论