Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS 3D transform to make trapezoid of given edge lengths

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 around -webkit-transform-origin: top center; while picking the -webkit-perspective of the container so that it appears that the bottom line of the image stays where it is but only expands to fill the entire container.

Wow, that sounds confusing. Here's a picture:

Trapezoid tranformation

So basically, I want to create a trapezoid with a fixed upper width and a variable lower width. I can't however quite figure out the math behind the relations... Javascript welcome. Following example works IF the body is 600px wide: http://jsfiddle.net/24qrQ/

Now the task is to change the perspective and rotation continuously with the body width. Any Ideas?

like image 587
Manuel Ebert Avatar asked Nov 10 '12 20:11

Manuel Ebert


1 Answers

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/

like image 95
Manuel Ebert Avatar answered Sep 22 '22 17:09

Manuel Ebert