Animated Key Strokes in HTML 5


Introduction

In this article we are going to understand 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 the 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 Web form

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


body
{
    background: #008080;
    color: #800000;
    font: 300 x-large 'Comic Sans MS';
    font-style: normal;
    background-image: url('aa.jpg');
}
#section
{
    height: 442px;
    left: 42%;
    margin: -240px 0 0 -320px;
    position: absolute;
    top: 57%;
    width: 842px;
    font-size: x-large;
    background-color: #008000;
}
#duplicate {
    bottom: 0;
    font: 300 .7em "Helvetica Neue", Helvetica, ;
    position: absolute;
    right: 1em;
    text-align: right;
}
#duplicate d {
    color: #fff;
}

d2.css scripting.

body {
    background: #fff;
    color: #000;
    font: 100.1% "Comic Sans MS", Lucida, Verdana, sans-serif;
}
#section {
    height: 480px;
    left: 50%;
    margin: 0 0 0 -320px;
    position: absolute;
    top: 0;
    width: 640px;
}
#duplicate {
    bottom: 0;
    font-size: .7em;
    position: absolute;
    right: 1em;
    text-align: right;
}

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.

<script type="text/javascript" charset="utf-8">
                        window.onload = function () {
                var r = Character("section", 800, 600), pos = [0, 0];
                var letter = r.path("M0,0L0,0z").attr({ fill: "#fff", stroke: "#fff", "fill-opacity": .3, "stroke-width": 1, "stroke-linecap": "round",   translation: "100 100" });
                var kc = document.getElementById("keystrock");
                kc.onkeyup = function (e) {
                    var key = this.value.substring(this.value.length - 1);
                    this.value = "";
                    if (key && key in helvetica) {
                        letter.animate({ path: helvetica[key] }, 200);
                    }
                    return false;
                };
                kc.onblur = function () {
                    kc.focus();
                };
                kc.focus();
            };
</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.

<body bgcolor="White">
        <p><b><strong>&nbsp; TYPE YOUR CHARACTER </strong></b> </p>
        <input type="text" value="" id="keystrock">
   <div id="section" style="background-color: #008080; font-family: 'Comic Sans MS'"></div>
    </body>

Step 5 : Output Press F5

Note : For the accurate output of HTML5 applications, you must have the Google Chrome browser in 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

Open Animated Popup Windows Using JavaScript

Using the ThreadPool to Run Animated Gifs in C# and .NET

Special characters not allowed as a value of Partition Key and Row Key

Up Next
    Ebook Download
    View all
    Learn
    View all