- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Computer Programming Tutorials
A HTML 5 CSS 3 Tutorial; Creating 3D Special Effects (Part I): Basic Rotation
Goals of This Tutorial
The goal of this tutorial is to demonstrate the first steps in creating an animated 3D object. Our goal is to show the simplest examples involving rotations which give the illusion of being a rotating object. From there over the next several tutorials we will "flesh out" the object and make it. animated. Finally, we will look at the requirements to generalize it for deployment on the full range of devices from mobile to desktop.
Step #1: Creating the HTML Skeleton
There should be nothing mysterious about this framework if you have been doing some work with HTML and CSS. Every attempt will be made to keep the coding accurate, but minimal. Browser differences will be discussed in the accompaning text but may not be fully implemented in the coding examples.
<!doctype html> <html lang="en">
<head> <meta charset="utf-8" /> <title>Intro to CSS 3D transforms</title>
<style> . .........we will expand on styling here rather than in a stylesheet for simplicity sake..... </style> </head>
<body> <h3>Creating a 3D Effect </h3>
<section>
.......our first example will require a section and one div.........
<div></div>
</section> </body>
</html>
Perspective and Transformation Go Hand in Hand
To start you have to begin thinking in three dimensions. To the flat plane of the x-axis and y-axis, you have to consider a z-axis. How close or how far an object is from you.
In order to create this effect in code you use the transform property in conjunction with the perspective property. For our purposes of this tutorial, think of a transformation in terms of a ration of an object around either the x-axis or the y-axis. For the perspective property think of that in relation to the z-axis as to how close the viewer is to the object being viewed.The perspective property is defined in terms of pixels. The lower the value is the more intense (think "object is closer") the 3D effect is. The higher the value of this property is the less intense the effect (think "the object is further away").
The perspective property only affects transformed elements.
In this tutorial we will be looking a perspective value in relation to transformations around the x- and y- axis of varying degrees.
Coding Different Perspective Values
We first need to define a reference container which and an object to be transformed.
The code for the styling of the container is:
<style> .container { width: 200px; height: 200px; border: 5px solid #c0c0c0; margin: 0 auto 20px; }
which you can think of as a "picture frame"
and the object styling is coded as follows:
.box { width: 100%; height: 100%; } #purple .box { background-color:#ffff00; -webkit-transform: perspective( 600px ) rotateX(30deg); transform: perspective( 600px ) rotateX(30deg);}
The HTML elements are simply a container section and a <div> for the box:
<section id="purple" class="container"> <div class="box purple">This is a rotate X of 45 degrees.</div> </section>
The next snapshots demonstrate the impact of modifyling the value of the perspective property
Changing the Values of the Perspective Property
Transformations Rotating Around The y-axis
In the following illustrations, the rotateX() method is replaced by the rotateY() method.
Transformations Around the y-axis
Setting the perspective-origin Property
The final property we will cover in this tutorial is the perspective-origin property. It takes two values. relating to x-axis and y-axis. The possible values are:
Defining where the view is placed at the x-axis
Possible values:
- center
- left
- right
- length
- or a percentage (%)
Default value: 50%
Defining where the view is placed at the y-axis
Possible values:
- bottom
- center
- top
- length
- or a percentage%
Default value: 50%. Values for x and y -axis placement can also be set to either initial or inherit.
Several Important Considerations
Perhaps not as apparent at first but when defining the perspective property for an element, it is the CHILD elements that get the perspective view, NOT the element itself. Notice the <div> within a <div> or a box defined within a container usage in the examples.
The perspective-origin property requires perspective to be set; and the perspective property has no impact without a transformation being set.
Wrap Up and What's Next
In the next part of this tutorial we will finish up the 3D transformations: the transformations along the z-axis. Then will we begin looking at constructing 3D objects .It is important to experiment with these transformations and perspective value as not all combination are useful.