Animated Key Strokes in HTML 5

Introduction

 
In this article, we are going to understand the animation of keyboard keys using JavaScript, CSS and HTML 5.
 
Here we will use some JavaScript and some styles along with the HTML code. Just go through the steps to see how to create this application.
 
Step 1 : Open a HTML editor or Visual Studio.
 
Open File menu ->select new ->Choose Website then.
 
0000.jpg
 
This is where we will create an HTML5 application.
  • Go to Solution Explorer
  • Right Click on the Application name
  • Select Add-->add new item
  • Now in the window that opens, select an HTML page or new Webform
k1.gif
 
Step 2: In this section, we will create the style for the media and create the .css on the media screen. Put the given script in the Head section of the HTML or in between the <head>--</head>.
 
Here two CSS files are used for design purposes; one is for the section div, named d1.css and the other d2.css for the whole body of the web page.
 
d1.css scripting
  1. body {  
  2.   background#008080;  
  3.   color#800000;  
  4.   font300 x-large 'Comic Sans MS';  
  5.   font-stylenormal;  
  6.   background-imageurl('aa.jpg');  
  7. }  
  8. #section {  
  9.   height442px;  
  10.   left: 42%;  
  11.   margin-240px 0 0 -320px;  
  12.   positionabsolute;  
  13.   top: 57%;  
  14.   width842px;  
  15.   font-sizex-large;  
  16.   background-color#008000;  
  17. }  
  18. #duplicate {  
  19.   bottom: 0;  
  20.   font300 .7em "Helvetica Neue"Helvetica,;  
  21.   positionabsolute;  
  22.   right: 1em;  
  23.   text-alignright;  
  24. }  
  25. #duplicate d {  
  26.   color#fff;  
  27. }  
d2.css scripting.
  1. body {  
  2.   background#fff;  
  3.   color#000;  
  4.   font100.1% "Comic Sans MS", Lucida, Verdanasans-serif;  
  5. }  
  6. #section {  
  7.   height480px;  
  8.   left: 50%;  
  9.   margin0 0 0 -320px;  
  10.   positionabsolute;  
  11.   top: 0;  
  12.   width640px;  
  13. }  
  14. #duplicate {  
  15.   bottom: 0;  
  16.   font-size: .7em;  
  17.   positionabsolute;  
  18.   right: 1em;  
  19.   text-alignright;  
  20. }  
Step 3: In this part, we need to work on some JavaScript over here. For understanding the full working of JavaScript download the attached .rar file and run the Html5typing application.
  1. < script type = "text/javascript"  
  2. charset = "utf-8" >  
  3.  window.onload = function() {  
  4.   var r = Character("section", 800, 600),  
  5.    pos = [0, 0];  
  6.   var letter = r.path("M0,0L0,0z").attr({  
  7.    fill: "#fff",  
  8.    stroke: "#fff",  
  9.    "fill-opacity": .3,  
  10.    "stroke-width": 1,  
  11.    "stroke-linecap""round",  
  12.    translation: "100 100"  
  13.   });  
  14.   var kc = document.getElementById("keystrock");  
  15.   kc.onkeyup = function(e) {  
  16.    var key = this.value.substring(this.value.length - 1);  
  17.    this.value = "";  
  18.    if (key && key in helvetica) {  
  19.     letter.animate({  
  20.      path: helvetica[key]  
  21.     }, 200);  
  22.    }  
  23.    return false;  
  24.   };  
  25.   kc.onblur = function() {  
  26.    kc.focus();  
  27.   };  
  28.   kc.focus();  
  29.  }; <  
  30. /script>  
Step 4: In this section, we are going to become familiar with the body part of HTML scripting. Replace this script from the body section of the Typeletter.aspx page.
  1. <body bgcolor="White">  
  2.     <p>  
  3.         <b>  
  4.             <strong>  TYPE YOUR CHARACTER </strong>  
  5.         </b>  
  6.     </p>  
  7.     <input type="text" value="" id="keystrock">  
  8.         <div id="section" style="background-color: #008080; font-family: 'Comic Sans MS'"></div>  
  9.     </body>  
Step 5: Output Press F5
 
Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC.
 
When you browse the Typeletter page in the Google chrome you will get:
 
k2.gif
 
Say for example when you press the key F from the keyboard, the word F will display in the browser as an animated pop-up word.
 
img1.gif
 
img2.gif
 
Here are some useful resources