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.
Open File menu ->select new ->Choose Website then.
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 by Rotational text.aspx
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: #FFFF99;
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 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.
txt = [],
attr = { font: "50px Helvetica", opacity: 0.5 };
txt[0] = R.text(320, 240, text).attr(attr).attr({ fill: "#0f0" });
txt[1] = R.text(320, 240, text).attr(attr).attr({ fill: "#f00" });
txt[2] = R.text(320, 240, text).attr(attr).attr({ fill: "#00f" });
The code shown above tells us that the R named variable is holding the Color () and 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 the text while it is being displaying on the browser.
The whole JavaScript is looks like as follow.
Color(function ()
{
var hldr = document.getElementById("holder"),
text = hldr.innerHTML.replace(/^\s+|\s+$|<[^>]+>/g, "");
hldr.innerHTML = "";
var R = Color("holder", 640, 480),
txt = [],
attr = { font: "50px Helvetica", opacity: 0.5 };
txt[0] = R.text(320, 240, text).attr(attr).attr({ fill: "#0f0" });
txt[1] = R.text(320, 240, text).attr(attr).attr({ fill: "#f00" });
txt[2] = R.text(320, 240, text).attr(attr).attr({ fill: "#00f" });
var mouse = null, rot = 0;
document.onmousemove = function (e) {
e = e || window.event;
if (mouse == null) {
mouse = e.clientX;\
return;
}
rot += e.clientX - mouse;
txt[0].attr({ transform: "r" + rot });
txt[1].attr({ transform: "r" + rot / 1.5 });
txt[2].attr({ transform: "r" + rot / 2 });
mouse = e.pageX;
};
});
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 the browser and we passed the holder id to fetch the content designing that is defined in the css file.
<body>
<div id="holder">
<p>C-sharp Corner Rocks</p>
</div>
</body>
Step 5 : The complete code for the Rotational text application:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Roational text.aspx.cs" Inherits="html5article._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="dee.js"></script>
<script src="rotation.js"></script>
</head>
<body>
<div id="holder">
<p>C-sharp Corner Rocks</p>
</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. As you put the mouse over the text then you will find the rotational text on your browser.
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