Image Animation In TypeScript

Introduction

We learn how to show an image animation on the mouse over event and out event with the help of TypeScript.

The following example shows an Image animation using webapplication in TypeScript. In this example we use an image control and function call on its event.

There are two TypeScript Functions in it; they are:

  1. Imageup(): Display the up.gif image on the onmouseover event
  2. Imagedown(): Display the down.gif image on the onmouseout event

Let's use the following.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. In this window, click "HTML Application for TypeScript" under Visual C#.

Give the name of your application as "Image_Animation" and then click "Ok".

Step 2

After Step 1, right-click on "Image_Animation". A pop up window is opened. Click on "Add" -> "New Item" -> "Web From". Give the name of your WebForm as "Image_Animation_Demo.aspx" and then click "Ok".

Step 3

After this session the project has been created; a new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, css file and aspx files, that look like this:

solution-explorer.gif

Coding

app.ts

class Image_animation

    Imageup() {

    var obj = <HTMLImageElement>document.getElementById("img");

    obj.src = "up.gif";

    }

    Imagedown() {

        var obj = <HTMLImageElement>document.getElementById("img");

    obj.src = "down.gif";

    }

}

 

window.onload = () =>

{

    var greeter = new Image_animation();

    var obj = <HTMLImageElement>document.getElementById("img");

    obj.onmouseover = function ()

    {       

        greeter.Imageup();

    }

    obj.onmouseout = function ()

    {

        greeter.Imagedown();

    }

};

 

Demo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Image_Animation_Demo.aspx.cs" Inherits="Image_Animation.Image_Animation_Demo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <script src="app.js"></script>

</head>

<body>

    <form id="form1" runat="server">

    <div> 

        <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="X-Large" ForeColor="#0000CC" Text="Image Animation in TypeScript"></asp:Label>

        <br />

        <br />  

        <img id="img" alt="" src="down.gif" style="height: 161px; width: 165px" /></div>

    </form>

</body>

</html>

 

app.js

var Greeter = (function () {

    function Greeter() { }

    Greeter.prototype.Imageup = function () {

        var obj = document.getElementById("img");

        obj.src = "up.gif";

    };

    Greeter.prototype.Imagedown = function () {

        var obj = document.getElementById("img");

        obj.src = "down.gif";

    };

    return Greeter;

})();

window.onload = function () {

    var greeter = new Greeter();

    var obj = document.getElementById("img");

    obj.onmouseover = function () {

        greeter.Imageup();

    };

    obj.onmouseout = function () {

        greeter.Imagedown();

    };

};

//@ sourceMappingURL=app.js.map

 

Output

 

Animation2.gif

 

Reference By

http://www.typescriptlang.org/

Up Next
    Ebook Download
    View all

    Test

    Read by 16 people
    Download Now!
    Learn
    View all