So I wasn’t content with the things I’ve tried doing in Safari, and recently began digging through the documentation of the iPhone SDK and the actual CSS Transforms spec.
I was a bit skeptical when I first read about all the cool functions that work in Webkit iPhone, but nowhere else, because there was little documentation, and literally nothing done by other developers, no demos, no tests. I thought, surely, some students with too much free time must have jumped on that immediately?
Well, I decided to give it a go, and started Dashcode, created a new web application and hacked in some CSS Transforms into my elements. One very important discovery I’ve made is that only 2D CSS Transforms are directly visible on the WYSIWYG canvas in Dashcode. So when you think the actual 3D functions won’t work, run the iPhone simulator.
The iPhone simulator is actually capable of using the exact same API than the iPhone. This is awesome and a bit funny, since I always thought the 3D functions don’t work in the browser version of Safari yet, because they need to be bundled tightly to the hardware, but apparently, there must be a different reason.
The function that sounded the most awesome, I thought, was the -webkit-perspective function (“This matrix maps a viewing cube onto a pyramid whose base is infinitely far away from the viewer and whose peak represents the viewer’s position”, that just HAS to be awesome). The frustration came directly afterwards: The function did nothing.
At least, that is what I thought at first – the function does indeed nothing visually, if you don’t use any other transform functions. I believe this fact is nowhere written in the SDK docs, and it was hard to find out.
Here comes the cool stuff: The perspective function seems to define how the other primitive functions behave.
Let’s have a look at a practical example: Take a look at the following three functions:
- rotateX – Rotates the element on the X axis.
- rotateY – Rotates the element on the Y axis.
- rotateZ – Rotates the element on the Z axis (By default, same as “rotate”)
Try these out – all three will give you flat, 2d animations, although they are, in fact, functions that use a 3D matrix. However, now change the -webkit-perspective property to 200, and try again. Now, you established virtual depth, and all three functions will give you incredible results.
To demonstrate it, here’s a demo that you have to either look at on your iPhone or in the iPhone Simulator:
As an extra flavor, this demo also uses the -webkit-backface-visibility property set to ‘hidden‘, to hide the actual back side of the first element when the second element comes in (otherwise, you’d see the front flipped). Also, it’s lightning fast, even on the iPhone, since it uses Webkit’s native CSS Transitions, and triggers them via setting the webkitTransform css property.
Expect more to come soon!