Rotating the Image Using HTML 5


Introduction

In this article we are going to understand how to rotate an image using Html 5. In this part we will show how to rotate the image while the image is displaying in the browser.

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.

Let's see how the rotatingimage application can be created. To do so go through the following steps.

Step 1 : Open a HTML editor or Visual Studio.

sd.gif

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
  • Rename it to rotateimage.aspx

ro1.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> tags.

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: #00688B;
    color: #fff;
    font: 300 100.1% "Helvetica Neue" , Helvetica, "Arial Unicode MS" , Arial, sans-serif;
}
#holder
{
    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;
}
#holder {
    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. For fully understanding how the JavaScript works download the attached .rar file and run the rotatingimage application.

The whole JavaScript looks as in the following.

window.onload = function ()
                {
                  var src = document.getElementById("rotateimage").src,
                  angle = 0;
                  document.getElementById("holder").innerHTML = "";
                  var Rid = Color("holder", 640, 480);
                  Rid.circle(320, 240, 200).attr({ fill: "#000", "fill-opacity": .5, "stroke-width": 5 });
                  var img = Rid.image(src, 160, 120, 320, 240);
                  var c = Rid.set(),
                  c++ = Rid.set();
                  c.push(Rid.circle(24.833, 26.917, 26.667).attr({ stroke: "#ccc", fill: "#fff", "fill-opacity": .4, "stroke-width": 2 }),
                         Rid.path("M12.582,9.551C3.251,16.237,0.921,29.021,7.08, 9.551z").attr({ stroke: "none", fill: "#000" }),
                         Rid.circle(24.833, 26.917, 26.667).attr({ fill: "#fff", opacity: 0 }));
                  c++.push(Rid.circle(24.833, 26.917, 26.667).attr({ stroke: "#ccc", fill: "#fff", "fill-opacity": .4, "stroke-width": 2 }),
                           Rid.path("M37.566,9.551c9.331,6.686,11.661,19.471,5.502,29.014l2.36,1.689l-4.893,2.262l-4.893, ,9.551z").attr({ stroke: "none", fill: "#000" }),
                           Rid.circle(24.833, 26.917, 26.667).attr({ fill: "#fff", opacity: 0 }));
                  c.translate(10, 181);
                  c++.translate(10, 245);
                  c[2].click(function ()
                       {
                        angle -= 90;
                        img.stop().animate({ transform: "r" + angle }, 1000, "<>");
                        }).mouseover(function ()
                        {
                        c[1].animate({ fill: "#fc0" }, 300);
                        }).mouseout(function ()
                        {
                        c[1].stop().attr({ fill: "#000" });
                        });
                        c++[2].click(function ()
                        {
                        angle += 90;
                        img.animate({ transform: "r" + angle }, 1000, "<>");
                        }).mouseover(function ()
                        {
                        c++[1].animate({ fill: "#fc0" }, 300);
                        }).mouseout(function ()
                        {
                         c++[1].stop().attr({ fill: "#000" });
                        });
                  };

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 rotateimage.aspx page. Here we pass a #holder in the div id that is defined in the d1.css file.

<body>
        <div id="holder">
            <img id="rotateimage" src="T.png" width="320" height="240" alt="Bee">
        </div>
    </body>


Step 5 : The Complete code for the
rotatingimage application:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="rotateimage.aspx.cs" Inherits="rotatingimage._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta charset="utf-8">
        <title></title>
        <link rel="stylesheet" href="d1.css" media="screen">
        <link rel="stylesheet" href="d2.css"media="print">
        <script src="javascript.js"></script>
        <script>
         
From Step 3 JavaScript copy from there and paste it here.
       </script>
    </head>
    <body>
        <div id="holder">
            <img id="rotateimage" src="T.png" width="320" height="240" alt="Bee">
        </div>
    </body>
</html>

Step 6 : Output Press F5

Note :
For the accurate output of HTML5 applications, you must have the Google Chrome browser in your PC. You
will see we rotated the image while the image is displaying in the browser.

ro2.gif

After clicking on the anticlockwise button it will look as shown in the figure:

ro3.gif

Here are the some useful resources

Rotate a Photo on Canvas Using HTML 5
Multiple Image Upload in ASP.NET
Create Rotating Buttons Using CSS3 and HTML 5 Tools
Create an Image Gallery Using CSS3 and HTML 5
How to rotate images in Asp.net
 

Up Next
    Ebook Download
    View all
    Learn
    View all