Rotational Text in HTML 5

Introduction

 
In this article, we are going to understand something interesting related to text rotation. The text you want to rotate in your browser you can put in this application.
 
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 Rotational text 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 new Webform
  • Rename it by Rotational text.aspx
rt1.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#FFFF99;  
  3.   color#fff;  
  4.   font300 100.1% "Helvetica Neue" , Helvetica"Arial Unicode MS" , Arialsans-serif;  
  5. }  
  6. #holder {  
  7.   height442px;  
  8.   left: 42%;  
  9.   margin-240px 0 0 -320px;  
  10.   positionabsolute;  
  11.   top: 57%;  
  12.   width842px;  
  13.   font-sizex-large;  
  14.   background-color#008000;  
  15. }  
  16. #duplicate {  
  17.   bottom: 0;  
  18.   font300 .7em "Helvetica Neue"Helvetica,;  
  19.   positionabsolute;  
  20.   right: 1em;  
  21.   text-alignright;  
  22. }  
  23. #duplicate d {  
  24.   color#fff;  
  25. }  
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;  
  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 Rotational text application. In the given JavaScript we use the txt[] as an array where we put the text in a different color and also with properties by using the following code lines.
  1. txt = [], attr = {  
  2.  font: "50px Helvetica",  
  3.  opacity: 0.5  
  4. };  
  5. txt[0] = R.text(320, 240, text).attr(attr).attr({  
  6.  fill: "#0f0"  
  7. });  
  8. txt[1] = R.text(320, 240, text).attr(attr).attr({  
  9.  fill: "#f00"  
  10. });  
  11. txt[2] = R.text(320, 240, text).attr(attr).attr({  
  12.  fill: "#00f"  
  13. });  
The code shown above tells us that the R named variable is holding the Color () and the id holder is passed in it. By these lines, the text will display in a different color in the browser. The mouse event is fired when you put the mouse in the text while it is being displayed on the browser.
 
The whole JavaScript looks as follows.
  1. Color(function() {  
  2.  var hldr = document.getElementById("holder"),  
  3.   text = hldr.innerHTML.replace(/^\s+|\s+$|<[^>]+>/g, "");  
  4.  hldr.innerHTML = "";  
  5.  var R = Color("holder", 640, 480),  
  6.   txt = [],  
  7.   attr = {  
  8.    font: "50px Helvetica",  
  9.    opacity: 0.5  
  10.   };  
  11.  txt[0] = R.text(320, 240, text).attr(attr).attr({  
  12.   fill: "#0f0"  
  13.  });  
  14.  txt[1] = R.text(320, 240, text).attr(attr).attr({  
  15.   fill: "#f00"  
  16.  });  
  17.  txt[2] = R.text(320, 240, text).attr(attr).attr({  
  18.   fill: "#00f"  
  19.  });  
  20.  var mouse = null,  
  21.   rot = 0;  
  22.  document.onmousemove = function(e) {  
  23.   e = e || window.event;  
  24.   if (mouse == null) {  
  25.    mouse = e.clientX;\  
  26.    return;  
  27.   }  
  28.   rot += e.clientX - mouse;  
  29.   txt[0].attr({  
  30.    transform: "r" + rot  
  31.   });  
  32.   txt[1].attr({  
  33.    transform: "r" + rot / 1.5  
  34.   });  
  35.   txt[2].attr({  
  36.    transform: "r" + rot / 2  
  37.   });  
  38.   mouse = e.pageX;  
  39.  };  
  40. });  
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 Rotational text.aspx page. Here in between the <p>--<p> tags, we can provide the desired text to be rotated in the browser and we passed the holder id to fetch the content designing that is defined in the CSS file.
  1. <body>  
  2.     <div id="holder">  
  3.         <p>C-sharp Corner Rocks</p>  
  4.     </div>  
  5. </body>  
Step 5 : The complete code for the Rotational text application:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Roational text.aspx.cs" Inherits="html5article._Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <meta charset="utf-8">  
  7.             <title></title>  
  8.             <link rel="stylesheet" href="d1.css" media="screen">  
  9.                 <link rel="stylesheet" href="d2.css" media="print">  
  10.                     <script src="dee.js"></script>  
  11.                     <script src="rotation.js"></script>  
  12.                 </head>  
  13.                 <body>  
  14.                     <div id="holder">  
  15.                         <p>C-sharp Corner Rocks</p>  
  16.                     </div>  
  17.                 </body>  
  18.             </html>  
Step 6: Output Press F5
 
Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser in your PC. As you put the mouse over the text then you will find the rotational text on your browser.
 
rt2.gif
 
rt3.gif
 
Here are some useful resources
 
Find and Replace Text in Text / HTML Files
Convert Microsoft Office Word to other formats (pdf, html, rtf, xps)
NotePad 2008
Simple Login Form using HTML 5