Javascript: matrix operations for CSS transform perspective

喜夏-厌秋 提交于 2019-12-04 15:51:33

The issue here seems to be how the 3d matrix is represented in the spec/docs (e.g. Mozilla CDN docs) vs how it's represented in the CSSMatrix polyfill's code. The code uses a transposed version of the matrix represented in the docs. The code works correctly but what the code refers to as the matrix position [3,4] is actually position [4,3] if we consider the maxtrix used in the spec/docs.

Regarding the formula for calculating perspective: as per the MDN docs here, the following matrix should be used/multiplied for applying the perspective (d is the perspective value):

a1 a2 a3 a4    1 0  0   0
b1 b2 b3 b4 =  0 1  0   0
c1 c2 c3 c4    0 0  1   0
d1 d2 d3 d4    0 0 −1/d 1 

So you were along the right path, but you were contructing the perspective matrix incorrectly because of the way the polyfill's code references the matrix internally. Instead of m.m43 = -1/d;, use m.m34 = -1/d;

I've updated the fiddle with the fixed code here.

As per the spec for the 'Perspective' property, it accepts only one parameter and that is 'length'. It doesn't accept, X, Y and Z.

Ref: W3ORG - Perspective Property

Ref: W3School's Perspective Example

I know, that I haven't given you the solution for your Perspective, but what's written is not the intended behavior. If my understanding about your question is wrong, please let me know, will try to help you with what I can. Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!