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 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 s new Webform
  • 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
  1. body  
  2. {  
  3.    background#00688B;  
  4.     color#fff;  
  5.     font300 100.1% "Helvetica Neue" , Helvetica"Arial Unicode MS" , Arialsans-serif;  
  6. }  
  7. #holder  
  8. {  
  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;  
d2.css scripting
  1. body {  
  2.     background#fff;  
  3.     color#000;  
  4.     font100.1% "Comic Sans MS", Lucida, Verdanasans-serif;  
  5. }  
  6. #holder {  
  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;  
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.
  1. window.onload = function ()  
  2. {  
  3.   var src = document.getElementById("rotateimage").src,  
  4.   angle = 0;  
  5.   document.getElementById("holder").innerHTML = "";  
  6.   var Rid = Color("holder", 640, 480);  
  7.   Rid.circle(320, 240, 200).attr({ fill: "#000""fill-opacity": .5, "stroke-width": 5 });  
  8.   var img = Rid.image(src, 160, 120, 320, 240);  
  9.   var c = Rid.set(),  
  10.   c++ = Rid.set();  
  11.   c.push(Rid.circle(24.833, 26.917, 26.667).attr({ stroke: "#ccc", fill: "#fff""fill-opacity": .4, "stroke-width": 2 }),  
  12.          Rid.path("M12.582,9.551C3.251,16.237,0.921,29.021,7.08, 9.551z").attr({ stroke: "none", fill: "#000" }),  
  13.          Rid.circle(24.833, 26.917, 26.667).attr({ fill: "#fff", opacity: 0 }));  
  14.   c++.push(Rid.circle(24.833, 26.917, 26.667).attr({ stroke: "#ccc", fill: "#fff""fill-opacity": .4, "stroke-width": 2 }),  
  15.            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" }),  
  16.            Rid.circle(24.833, 26.917, 26.667).attr({ fill: "#fff", opacity: 0 }));  
  17.   c.translate(10, 181);  
  18.   c++.translate(10, 245);  
  19.   c[2].click(function ()  
  20.        {  
  21.         angle -= 90;  
  22.         img.stop().animate({ transform: "r" + angle }, 1000, "<>");  
  23.         }).mouseover(function ()  
  24.         {  
  25.         c[1].animate({ fill: "#fc0" }, 300);  
  26.         }).mouseout(function ()  
  27.         {  
  28.         c[1].stop().attr({ fill: "#000" });  
  29.         });  
  30.         c++[2].click(function ()  
  31.         {  
  32.         angle += 90;  
  33.         img.animate({ transform: "r" + angle }, 1000, "<>");  
  34.         }).mouseover(function ()  
  35.         {  
  36.         c++[1].animate({ fill: "#fc0" }, 300);  
  37.         }).mouseout(function ()  
  38.         {  
  39.          c++[1].stop().attr({ fill: "#000" });  
  40.         });  
  41.   }; 
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.
  1. <body>  
  2.         <div id="holder">  
  3.             <img id="rotateimage" src="T.png" width="320" height="240" alt="Bee">  
  4.         </div>  
  5. </body> 
Step 5 : The Complete code for the rotatingimage application:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="rotateimage.aspx.cs" Inherits="rotatingimage._Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <meta charset="utf-8">  
  6.         <title></title>  
  7.         <link rel="stylesheet" href="d1.css" media="screen">  
  8.         <link rel="stylesheet" href="d2.css"media="print">  
  9.         <script src="javascript.js"></script>  
  10.         <script>  
  11.           From Step 3 JavaScript copy from there and paste it here.  
  12.        </script>  
  13.     </head>  
  14.     <body>  
  15.         <div id="holder">  
  16.             <img id="rotateimage" src="T.png" width="320" height="240" alt="Bee">  
  17.         </div>  
  18.     </body>  
  19. </html> 
Step 6: Output Press F5 Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on 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:
 
Here are some useful resources