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.





Thursday, May 2, 2013

Trying different types of Transitions in CSS3



This is another wonderful feature that I found in CSS3. Now, we can make small animations and transitions with the help of just CSS3 but without using flash or any other tool support. CSS3 allows adding effect when changing from one style to another. for an example, you can see some element increases its height within 5 seconds of time when you hover over it. even you can apply multiple transition effects to the same element at the same time. You will definitely enjoy trying out these examples under this post.

First of all, I don't like to bore you with coding but this time, I would like you to examine the output first. Do click the following link to view the output in the browser itself (please make sure that your browser supports all these features of HTM5 and CSS3)


OK, now let's go through the concept behind it. CSS3 transitions are effects that let an element gradually change from one style to another. To do this, you must specify two main things;


  1. Specify the CSS property you want to add an effect to (e.g :- height)
  2. Specify the duration of the effect (e.g:- within 5 seconds)
if you did not specify a time duration to apply the effect, the transition wont take place since the default value for time is considered to be 0. The effect will start when the specified CSS property changes value. A typical CSS property change would be when a user mouse-over an element:

Throughout this post, I'll explain, transitions that take place when you hover over a div element.

change color from magenta to cyan within 5 seconds when hovered over


.div1
{
width:100px;
height:100px;
border-style:dashed;
background:magenta;
border-radius:50px;

-webkit-transition:background 5s;          /*says to make a transition in background within 5 seconds*/
}

.div1:hover                     /*says when you hover over div1 class elements, change the background to cyan*/
{
background:cyan;
}

change the height to 200px within 3 seconds when hovered


.div2
{
width:100px;
height:100px;
background:blue;
border-radius:10px;

-webkit-transition:height 3s;
}

.div2:hover
{
height:200px;
}

change the width to 50px within 5 seconds when hovered and transit the color to orange


.div3
{
width:200px;
height:100px;
background:pink;
border-radius:10px;

-webkit-transition:width 5s,background 5s;

}

.div3:hover
{
width:100px;
background:orange;
}

Performing multiple transitions


/*once hovered, do the following
    change the color from green to blue
    increase the width from 100px to 150px
    increase the height from 100 px to 150 px
    rotate the shape 360 degrees
    do all of the above within 7 seconds
*/

.div4
{
width:100px;
height:100px;
background:cyan;
border-color:Blue;
border-bottom-style:solid;
border-radius:50px;
-webkit-transition:width 7s,height 7s,background 7s, -webkit-transform 7s, border-color 7s;

}

.div4:hover
{
width:150px;
height:150px;
background:blue;
border-color:cyan;
-webkit-transform:rotate(360deg);
}

following code shows how you need to use these div elements in your html page


    <h3>
    Hover over me, I change my color :)
    </h3>

    <div class=div1></div>

    <h3>
    Hover over me, my height increases
    </h3>
    <div class=div2></div>

    <h3>
    Hover over me, my width increases and color changes
    </h3>
    <div class=div3></div>

    <h3>
    Hover over me, I make lot of transitions
    </h3>
    <div class=div4></div>




2D Transforms in CSS3

Cascading Style Sheets or simply CSS is used to control the style and layout of Web pages. Css3 has been introduced with lot of improved features than that were there in earlier versions of CSS3. CSS3 allows users to create 2D Transforms, 3D Transforms  Transitions, Animation and so many more colorful and interesting features to be added to the web pages that they create. Through these features, we b pages improve their performance much more other than using videos or something like flash controls to create attractive web designs.

I am going to describe some interesting 2D Transforms that are available in CSS3. All what I am going to play around here is a simple div control with some initial styles. It will be used through out all the steps to demonstrate different 2D Transforms. A Transform is an effect that lets an element change shape, size and position.

Though I can embed the style tags within my html page, I have created them seperately, but I'll be explaining each together for clarification.

OK, this is my initial div with styles

//in StyleSheet2.css file
div
{
   width:100px;
   height: 75px;
   border:5px;
   border-style:ridge;
   border-radius:7px;
   background-color:Aqua; 
}


//in Css3Transform.html file
<div class="div"> I am the original div</div>

output:-







I am going to shift the shape 50px in X axis and 100px in Y axis


//in StyleSheet2.css file
div#div2
{
    background-color:Coral;       
    -webkit-transform:translate(200px,30px);/*move the div 200px of X and 30px of Y*/
}

//in Css3Transform.html file

<div id="div2">I am translated by 50px of x and 100px of y</div>

I am going to rotate the shape by 30 degrees


//in StyleSheet2.css file

div#div3

  background-color:CornflowerBlue;       
 -webkit-transform:rotate(30deg); /* rotate the above div in 30 degrees(works in Chrome) */  
}

//in Css3Transform.html file

<div id="div3"> I am rotated by 30 degrees</div>

I am going to scale up the shape by 1.5 times of x and 2 times of Y


//in StyleSheet2.css file
div#div4
{
    background-color:Aquamarine;
    -webkit-transform:scale(1.5,2);/*scale up x with 1.5 times and y with 2 times*/
}

//in Css3Transform.html file

<div id="div4"> i am scaled up 1.2 times x and 1.5 times y</div>

I am going to turn the shape 20 degrees to X axis and 30 degrees to Y axis


//in StyleSheet2.css file
div#div5
{
    background-color:BurlyWood;
    -webkit-transform:skew(20deg,30deg);
    /*With the skew() method, the element turns in a given angle, 
    depending on the parameters given for the horizontal (X-axis) 
    and the vertical (Y-axis) lines:*/
}

//in Css3Transform.html file

<div id="div5"> i am skewed 20deg to X and 30degs to Y</div>


I am going to turn the shape only toward X axis by 45 degrees


//in StyleSheet2.css file
div#div6
{
    background-color:Purple;
    -webkit-transform:skewX(45deg);/*turn to X axis only in 45 degrees */
}

//in Css3Transform.html file

<div id="div6"> i am skewed 45deg to X only</div>


I am going to turn the shape only toward Y axis by 45 degrees



//in StyleSheet2.css file
div#div7
{
    background-color:Pink;
    -webkit-transform:skewY(45deg);/*turn to y axis only in 45 degrees */
}


//in Css3Transform.html file
<div id="div7"> i am skewed 45deg to Y only</div>


The final output compared to the initial div element:-

you can get the source files from the below links

DOWNLOAD FROM HERE


VIEW IN BROWSER



Working with HTML5 Canvas Element

The new Canvas element of HTML5 is used to draw graphics on the fly via JavaScripting. It is called so because you can draw graphics and images on it and it is just a container which holds them. Just by specifying a canvas element, you wont see anything on the page. To make something visible on the canvas, you have to use some JavaScripts. I find it really interesting since it is easy to understand how things work if we go from the very basics. I'll show some snippets in this post which I used to show different usages of canvas element in HTML5.

First of all, we can markup a canvas as below. here I draw a canvas with specified height and width but this want show an output on your web page


<canvas id="canvas1" width="200" height="100">
</canvas>

I am going to draw another canvas with a blue border of 2 px

<canvas id="canvas2" width="200" height="100" style="border:2px solid #0000FF;">
</canvas>








I am going to draw another canvas with a blue border of 2 px
<canvas id="canvas3" width="200" height="100" style="border:2px solid #0000FF;"> </canvas> <script> var c=document.getElementById("canvas3"); var cntx=c.getContext("2d"); cntx.fillStyle="#00FF00"; cntx.fillRect(10,10,150,75); </script>


I am going to draw a line in the above canvas using some javascript
<canvas id="canvas4" width="200" height="100" style="border:2px solid #0000FF;"> </canvas> <script> var c=document.getElementById("canvas4"); var cntx=c.getContext("2d"); cntx.moveTo(0,0); //starting point (x and y) cntx.lineTo(200,50); //ending point (x and y) cntx.stroke(); //draw the line </script>


I am going to draw a circle inside the canvas
<canvas id="canvas5" width="200" height="100" style="border:2px solid #0000FF;"> </canvas> <script> var c=document.getElementById("canvas5"); var cntx=c.getContext("2d"); cntx.beginPath(); cntx.arc(95,50,50,0,2*Math.PI); //arc(x,y,r,start.stop) cntx.stroke(); //cntx.fill(); will fill the circle with color </script>


I am going to draw some text on my canvas which is in Verdana font and 40px in size
<canvas id="canvas6" width="500" height="150" style="border:2px solid #0000FF;"> </canvas> <script> var c=document.getElementById("canvas6"); var cntx=c.getContext("2d"); cntx.fillStyle="#FF00FF"; cntx.font="40px Verdana"; cntx.fillText("Text with Fill",20,40); cntx.strokeText("Text with NO Fill",20,80); </script>







I am going to draw a rectangle on my canvas filled with linear gradient
<canvas id="canvas7" width="200" height="100" style="border:2px solid #0000FF;"> </canvas> <script> var c=document.getElementById("canvas7"); var ctx=c.getContext("2d"); // Create gradient var grd=ctx.createLinearGradient(0,0,200,0);//createLinearGradient(x,y,x1,y1) grd.addColorStop(0,"green"); grd.addColorStop(1,"cyan"); // Fill with gradient ctx.fillStyle=grd; ctx.fillRect(10,10,150,80); //fillRect(x,y,width,height) </script>









I am going to draw a rectangle on my canvas filled with circular gradient
<canvas id="canvas8" width="200" height="100" style="border:2px solid #0000FF;"> </canvas> <script> var c=document.getElementById("canvas8"); var ctx=c.getContext("2d"); // Create gradient var grd=ctx.createRadialGradient(75,50,5,90,60,100);//createRadialGradient(x,y,r,x1,y1,r1) grd.addColorStop(0,"cyan"); grd.addColorStop(1,"blue"); // Fill with gradient ctx.fillStyle=grd; ctx.fillRect(10,10,150,80); //fillRect(x,y,width,height) </script>









I am going to display an image on my canvas
<canvas id="canvas9" width="230" height="300" style="border:2px solid #0000FF;"> </canvas> <script> var c=document.getElementById("canvas9"); var ctx=c.getContext("2d"); var img=new Image(); img.src="tilly.jpg"; ctx.drawImage(img,5,5); </script>

you can get the source code from below link

DOWNLOAD IT FROM HERE

or view the outputs directly from the below link

VIEW IN BROWSER


Note that, all the above outputs are working fine in Firefox and Google Chrome web browsers but chrome did not display the image



Wednesday, May 1, 2013

An all in one Registration Form in HTML5

I am practicing a very interesting technology these days for one of the subjects in my College. Yes, it is HTML5 and I am in an experimenting stage in it these days. One of my own way of self learning is to identify little little syntax and GUI controls in programming languages (here, it is a scripting language) using a simple text editor like notepad. Once I am comfortable enough with the language, I'll be using some IDE to do some  complex tasks like building a website etc.

Why I decided to write this post is to show you all how interesting HTML5 is though it is not fully supported with all the major web browsers yet. If you have Google Chrome to the latest updated version (I have Version 26.0.1410.64 m Currently), I think you can manage all the below outputs as I have already experienced.

As a good start, I thought to create an all in one registration form. What I'm trying here to demonstrate is how each input control of HTML5 can be used with ease. Note that you may find some irrelevant entry fields but as I said, It's just tried out by myself for a demonstration purpose.  

If you want to check the code, download the html file and the css file below. 


It contains an html file and the CSS file required to it. Just right click the html file and run it in Chrome Web browser to see the below output.The output looks as below in Chrome




If you observe the code, you will find that none of the Input fields does not hold any validation control attached to it. That is because HTML5 automatically identifies the the type of the input from the type attribute of the input tag that we use to determine the data entered to those fields and validates to avoid user errors. We don't need to use complex regular expressions to validate these input fields. 

I have clearly formatted the code so that anyone can go through it easily and understand what I have used. there's only one place where I have used a piece of java script to identify the user selected rate in the range control using a text box. Note that other than that, I did not do any back end client side or server side coding logic in this simple example. All I wanted to understand is, how to use the new controls in HTML5.

You can also, work with HTML5 and CSS3 in Microsoft Visual Studio 2010 IDE. If you don't have CSS3 in your IDE, you can download and install it from here. Then you can notice CSS3 in your IDE when creating CSS files. One such place where I required CSS3 is, using rounded- rectangles for the boarders of the input controls that I have used. You can style your forms very nicely using CSS files. I just have used only a very few styles in my StyleSheet file and attached it to the html file. 

I think I could cover almost all the major Input controls in HTML5 in this simple exercise. Hoping to post some more interesting stuff under this area soon :) 

have a better view of the Completed Registration Form by clicking the below shown link. You can straight away view it in your browser