I'll assume you already understand parallax and how it creates the illusion of depth. The idea being we will shift certain objects on the screen to mimic how they would be perceived if the were physically further back. For the illusion to work, close enough is not good enough. Our minds are extremely well developed to understand and process the 3D world we live in. If we are to fool it then we have to be damn close to real. I've seen other articles out there implement a simplified model with the argument that it's close enough. I argue that it is not and if you want something that is actually going to create a believable illusion of depth then keep on reading. Below is a diagram of the basic model we will be working off.
In the diagram above:
- The black line is the physical screen
- The blue line is the virtual layer we are simulating depth for
- P1 is where a point on the virtual layer would be normally (ie without any depth)
- P2 is the point on the virtual layer
- P2s is where P2 would appear when projected onto our physical screen. As you can see, that point will vary depending on the distance of the eye from the screen.
Let's start with what we know:
- The depth of the virtual layer ( D ) is something we define.
- The distance of the viewer from the screen ( F ) is also something we need to set a value for. (It would be nice if there were a way to know this for certain by way of the camera or something similar but this is unfortunately not possible.) As you can see in the diagram, the distance of the viewer from the screen will affect the offset. You'll have to pick a value that feels roughly right and may need to experiment to find that sweet spot.
We will refer to the offset (the distance between P1 and P2s) as O.
Let's dive in:
- We need to know the angle b . b = 180 - a (The full circle above is 360 and we have two 90 degree angles already)
- Next is the distance between the eye ( E ) and the virtual point ( P2 ). L2 = D2 + F2 - 2 D F cos(b)
- Now we may proceed to work out the angle c. We know that L/sin(b) = F/sin(c). From this c = arcsin((sin(b) / L) * F)
- And finally O is derived as O = D tan(c)
Remember you need to do this for both the X and Y axis.
Remember, you will need to call this function twice, once to get the X offset and once to get the Y offset.
In order to find the current tilt of your device you will need to listen on the deviceorientation event. In your code you will have to record the initial tilt of the device and then work out how far you have deviated from that point.
That final Touch
This will give you the correct offsets but to really create the illusion you'll need to go a bit further.
- Experiment with different values of F until you find a distance that feels right. This can take a bit of time and can be difficult as it won't feel more right as you get closer to the sweet spot. It will just feel plain wrong until you hit that sweet spot and all of a sudden it is right. You may need to experiment with different values of the depth ( D ) as well. eg on TodoListMe mobile I found that a deeper value for the Categories panel background worked nicely whereas a shallower value looked better for the items on the main page.
- Add some drop shadow in on items in the foreground. Keep it subtle though. Again this has to mimic what would actually happen in real life. Many people like to put a bold shadow in to make it really stand out. Remember, if it stands out then it is not what would normally happen and if it's not what would normally happen then it's not helping the illusion. Again, you'll need to experiment. When you get it right you won't notice it but the illusion of depth will be enhanced.
- Use colour to your advantage too. Items in the distance are usually lighter whilst items in the foreground are usually brighter.
Now go check out the TodoListMe mobile web app to see the end result.