ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

A HTML 5 CSS 3 Tutorial; Creating 3D Special Effects (Part I): Basic Rotation

Updated on May 9, 2014

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

In this illustration of an object rotated around the x-axis the perspective is set relatively high, so the object appears further away from the viewer.
In this illustration of an object rotated around the x-axis the perspective is set relatively high, so the object appears further away from the viewer.
In this illustration the perspective property value is set lower. The object appears neared.
In this illustration the perspective property value is set lower. The object appears neared.
The object is very, very close to the viewer.
The object is very, very close to the viewer.

Transformations Rotating Around The y-axis

In the following illustrations, the rotateX() method is replaced by the rotateY() method.

Transformations Around the y-axis

In this example, the rotation is around the y-axis.
In this example, the rotation is around the y-axis.
The angle of rotation can be specified as either a positve or negative value.
The angle of rotation can be specified as either a positve or negative value.

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.

Please rate this Tutorial for Completeness

5 out of 5 stars from 1 rating of Please add a rating and your comments. Thanks!

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)