Showing posts with label Animation. Show all posts
Showing posts with label Animation. Show all posts

Friday, May 3, 2013

Creating a colorful Animation in CSS3

Creating animation using few steps in an easier manner is no more magic with CSS3. It's really simple to create CSS3 animations which can even be replaced for animated images, flash animations and JavaScript in web pages.  

Before going through this post about animations, I prefer if you can go through the couple of previous psost on CSS3 2D and 3D transforms that I have published. Most of the concepts used their will be helpful in understanding this post. 

First of all, to create animation in CSS3, we have to know about a stranger and that is key-frame  If you know some animation creating tool like flash, Moho or even any video editing tools, you will surely know what these key frames are. Yes, collection of key frames makes a complete animation. here, in CSS3, the rule is @keyframes and it is where the animation is created. We have to specify a CSS style inside the @keyframes rule and the animation will gradually change from the current style to the new style.

Note that, I use Google Chrome to test all my HTML5 and CSS3 pages throughout all these posts where Chrome requires the prefix -webkit- to work with them. 

When the animation is created in the @keyframe, bind it to a selector, otherwise the animation will have no effect.

Bind the animation to a selector by specifying at least these two CSS3 animation properties:
-->Specify the name of the animation
-->Specify the duration of the animation

It's just like creating a keyframe and telling who should use it specifying a time as well. So, all we have to do is creating @keyframes specifying when each of them should be displayed  and what will be changed in the element during the appearance of each of those key frames and finally telling the element which required the animation in it about the key frames we have created.

I thought to go for a little animation example at once, without describing little concepts that you may already know.

first of all, I would like you to see the output by following the link below to know what we are going to animate.




Don't get scared :) you have to do very little work in CSS3 to get that output. have a look at the code below.

<!DOCTYPE html>
<html>
<head>
<style> 

/*I am just creating a div with some styles as below
then i also specfy that it is going  to be animated using the myfirst @keyframes
 */
div
{
width:150px;
height:150px;
border-style:ridge;
border-radius:20px;
background:red;
position:relative;

-webkit-animation:myfirst 15s; /*the animation using myfirst keyframes will last for 15 seconds*/
}

/*now, we'll create the @keyframes
Note that, I want to move my div element to ten diffrrent position in ten diffrrent colors
so, I specify the starting (0%) and ending (100%) points with  a collection of gradually increasing percentage values to get hold of ten positions. 
In each position , I specify a new color to the background and state the x and y positions where the div should find its path through. The path I specify is sort of a box-zig-zag way.
Do a little calculation to decide the x and y positions that you must follow, then it's a matter of applying the same in each line :) that's all with CSS3. then you can use the div tag wherever you want in the page.
the animation is done ...
[the highlighted part does a small transform to rotate the element around X axis to 360 degrees as a 3D transformation]
*/

@-webkit-keyframes myfirst
{
0%   {background:aqua; left:0px; top:0px;-webkit-transform:rotateX(360deg)}
10%  {background:blue; left:0px; top:100px;}
20%  {background:cyan; left:100px; top:100px;}
30%  {background:green; left:100px; top:0px;}
40%  {background:pink; left:200px; top:0px;}
50%  {background:purple; left:200px; top:100px;}
60%  {background:magenta; left:300px; top:100px;}
70%  {background:coral; left:300px; top:0px;}
80%  {background:red; left:300px; top:0px;}
90%  {background:orange; left:300px; top:100px;}
100% {background:yellow; left:400px; top:100px;}
}
</style>
</head>
<body>

<p>This shape changes ten positions in ten different colors</p>

<div>@@@@@@@@@@</div>

</body>
</html>






How to do a simple 3D Transform in CSS3?

In my previous post, I have described how to perform simple 2D transforms with some examples. Here I am going to describe a similar thing but here, our shape is going to transform it self in a 3D surface. have a look at the below output. shape mentioned as 'I am Happy' rotates around X axis to 360 degrees and shape 'I am sad' rotates around Y axis to 360 degrees within 7 minutes changing their colors. this happens once the user hovers over the shapes.




It's not that complex. You should know to apply styles to a simple div control to design the above shape. Little knowledge on how 2D transform happens in CSS3. All what have done above is to let the two shapes rotate in X axis and Y axis to 360 degrees once the user hovers on them. Meanwhile it changes the colors and all it happens within 7 seconds. observe the code below. since the code is simple enough to handle in the same file, i have not used an external style sheet to include the style tag.

<!DOCTYPE html>
<html>
<head>
<title>3D Transitions</title>
<style>

/*creates the initial div tag and two of its properties (background and transform defines time for transformations)*/
div
{
width:200px;
height:200px;
background:cyan;
border-radius:10px;
border-style:dotted;
font:40px Verdana;
-webkit-transition: background 7S,-webkit-transform 7S;
}

/*once the use hovers over div element, transform rotate it over 360 degrees in X axis during 7 minuets*/
div:hover
{
-webkit-transform:rotateX(360deg);
background:Orange;
} /*get all the features of div to div2 and once the use hovers over div2 element, transform rotate it over 360 degrees in Y axis during 7 minuets*/
div#div2:hover
{
-webkit-transform:rotateY(360deg);
background:purple;
}
</style>
<!-------------------------------------------------------------->
</head>
<body>
<div >I am Happy :) </div>
<br/>
<div id="div2">I am Sad :( </div>
</body>
</html>

that's all you have to do.