CSS 3D transform to make trapezoid of given edge lengths

前端 未结 2 791
旧时难觅i
旧时难觅i 2021-01-13 08:43

I have an element of given dimensions (say, 100x300 px) living in a container of the same height and variable width that I want to transform using rotateX aroun

2条回答
  •  长情又很酷
    2021-01-13 09:05

    Okay, after a glass of wine the maths came back to me:

    First, let's look at the perspective / rotation ratio. Viewed from the side, it looks like this:

    enter image description here

    The red element is rotated around its upper edge, if we project its lower edge to the lower edge of the container, the intersection between the projection line and the line perpendicular to the container at its upper edge is the required viewpoint. We get this by simple trigonometry (notice phi here is in radians, not in degree).

    If we apply this, the lower edge of the element will always appear on the lower edge of the container. Now the free parameter is rotation. This seems to have the relation

    rad = pi/2 - element.width / container.width
    

    for sufficiently large widths, however I can't quite wrap my head around the actual relationship. Here is a fiddle: http://jsfiddle.net/24qrQ/6/

提交回复
热议问题