Tuesday, February 26, 2013

How to Create Bounce Effect with CSS3 Animation

How to Create Bounce Effect with CSS3 Animation:
Today, we are going to doing an experiment with CSS3 Animation. In our previous post, we discussed how to re-create “marquee” effect using CSS3 Animation. This time, we will try to create a “notification bar” with bounce effect.
So, let’s get started.

Getting Started

Let’s create a new HTML document and add the following markup to structure the notification bar.
<div class="css3-notification">
 <p>Hi, this is a notification and it bounces.</p>
</div>
Then, add some decorative styles to dress up the notification bar.
.css3-notification {
 font-size: .8em;
 text-align: center;
 padding: 10px;
 background-color: #111;
 color: #fff;
 box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, .3);
 text-transform: uppercase;
 position: relative;
 font-weight: bold;
}
At this stage, this will give us the following result in browsers.

Writing CSS3 Animation Keyframes

The basic idea on how the bounce will run is that when the object drops on the surface the first time, it bounces of the surface and will reach its highest level. Then it gradually reaches a lower point with every subsequent bounce, until the object stops bouncing. This is illustrated in the following figure.

Step 1: Creating Animation Keyframe

Now, let’s start writing on the CSS3 Animation Keyframe in the stylesheet. In this example, we simply name this keyframe bounce.
@keyframes bounce {
 
}
Keep in mind that in this tutorial we will use the standard @keyframe syntax from W3C to make the codes look simpler. But, in order for the animation to work across the browsers, the vendor syntax (-webkit-, -moz-, -o-) should be included and you will find them, later on, in the source codes.

Step 2: Specifying Initial Position

First, we set the object to the top of its initial position. In CSS3 Transforms, we add negative value for the Y-axis. In the following code snippet, we set the position for 0% to 5% of the time frame. So, the notification bar will stay a little while in that position.
0% {
 transform:translateY(-100%);
 opacity: 0;
}
5% {
   transform:translateY(-100%);
    opacity: 0;
}

Step 3: Specifying the First Bounce

Then, from 5% to 15% of the time frame, the object start moving to its original position. We set the translateY property back to 0%. Typically, an object that bounces has elasticity.
When this object strikes a solid object, the side of that object that hits the surface, should be a little compressed or deformed. Thus, in our case, we will decrease the padding-bottom — from 10px to 5px.
15% {
 transform:translateY(0);
 padding-bottom: 5px;
}
The object will bounce back upwards after hitting the surface. At this point, the object bounces at its highest point and we set it for 50% at 30% of the time frame.
30% {
 transform:translateY(-50%);
}

Step 4: Specifying the Second Bounce

After reaching the peak, the object should be back to 0 position, or in other words, hitting the ground. The object will be less deformed than the previous hit. So, at this point, we decrease the padding-bottom of the object to only 6px.
40% {
   transform:translateY(0%);
    padding-bottom: 6px;
}
Then it bounces.
This time, it will only reach the point lower than the first one; it moves upwards 30% from the current position.
50% {
 transform:translateY(-30%);
}

Step 5: Countinuously Bouncing Until It Stops

These events repeat until the end of the time frame and the following are the rest of the Animation — from 70% to 100% of the time frame.
70% {
   transform:translateY(0%);
    padding-bottom: 7px;
}
80% {
 transform:translateY(-15%);
}
90% {
 transform:translateY(0%);
 padding-bottom: 8px;
}
95% {
 transform:translateY(-7%);
}
97% {
 transform:translateY(0%);
 padding-bottom: 9px;
}
99% {
 transform:translateY(-3%);
}
100% {
 transform:translateY(0);
 padding-bottom: 9px;
 opacity: 1;
}
That’s all the codes we need right now, now you can view the bounce effect in action from the link below.

Further Resources

Below are good resources for digging into CSS3 Animation, Transformations and Bounce Effect further.

Monday, February 25, 2013

Sorting and Searching Program with C/C++ Language

Sorting and Searching Program with C/C++ Language:
Problem: How to make Sorting and Searching Program.

Solution

Sorting and Searching is an important task in programming. Here we will see such a C Program. This program is used to sorting and searching a string array. Running this program we will provide names and given names are sorted and searched by the program. Programming code is given bellow→

#include<stdio.h>  
#include<conio.h>
#include<string.h>

  struct name            // Custom Data Type
  {
      char a[10];   
       
   }b [10];                 // Custom data type’s variable


  int N,i,j;
  int c=0;
  char name[10];
  printf("How many names :");
  scanf("%d",&N);  fflush(stdin);
  {
   printf("Enter %d Name :",i+1);
   gets(b[i].a);               
  }

  printf("\n\nEntered name list :");
  {
            printf("\n%s",b[i].a);
  }

  {
            for(j=i+1;j<N;j++)
            {
             if((strcmp(b[i].a,b[j].a))>0)
               char temp[10];
               strcpy( temp,b[i].a);
               strcpy(b[i].a,b[j].a);
               strcpy( b[j].a,temp);
             }
   }
  }
  printf("\n\nSorted name list :");
   {
             printf("\n%s",b[i].a);
   }



 printf("\n\nEnter name to search :");
 gets(name);
 {
   if((strcmp(b[i].a,name))==0)
   c++;
 }
 if(c==0)
 {
   printf("%s not found .",name);
 }
 else
 {
   printf("%s remains %d times .",name,c);
 }
 }

Output





Remarks

After giving  name (string) we can see the list of name and then sorted list. When we will give a name and search, we will see that name if it is in array otherwise name not found on the screen. Here strcpy ()  function is used to copy one variable’s value another variable and strcmp() function is used to compare two string. fflush(stdin) is used to flush buffer.