Search

Change Language

Thursday, March 24, 2011

Distance Finder ( Google Maps API )

Google maps is a free web mapping service application provided by Google. It offers lots of cool features (showing various map types, plotting points, showing routes, geocoding addresses). You can also add all these features to your website using the Google Maps APIs provided by Google. In this tutorial I will show you how to add some of these features to your site.
Download Script



Creating the web form for getting the two addresses

 <body bgcolor="#fff">
    <div id="form" style="width:100%; height:20%">
        <table align="center" valign="center">
            <tr>
                <td colspan="7" align="center"><b>Find the distance between two locations</b></td>
            </tr>
            <tr>
                <td colspan="7">&nbsp;</td>
            </tr>
            <tr>
                <td>First address:</td>
                <td>&nbsp;</td>
                <td><input type="text" name="address1" id="address1" size="50"/></td>
                <td>&nbsp;</td>
                <td>Second address:</td>
                <td>&nbsp;</td>
                <td><input type="text" name="address2" id="address2" size="50"/></td>
            </tr>
            <tr>
                <td colspan="7">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="7" align="center"><input type="button" value="Show" onClick="initialize();"/></td>
            </tr>
        </table>
    </div>
    <center><div style="width:100%; height:10%" id="distance_direct"></div></center>
    <center><div style="width:100%; height:10%" id="distance_road"></div></center>
   
    <center><div id="map_canvas" style="width:70%; height:54%"></div></center>
</body>

Showing the map

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>

<script type="text/javascript">

    var location1;
    var location2;
   
    var address1;
    var address2;

    var latlng;
    var geocoder;
    var map;
   
    var distance;
   
    // finds the coordinates for the two locations and calls the showMap() function
    function initialize()
    {
        geocoder = new google.maps.Geocoder(); // creating a new geocode object
       
        // getting the two address values
        address1 = document.getElementById("address1").value;
        address2 = document.getElementById("address2").value;
       
        // finding out the coordinates
        if (geocoder)
        {
            geocoder.geocode( { 'address': address1}, function(results, status)
            {
                if (status == google.maps.GeocoderStatus.OK)
                {
                    //location of first address (latitude + longitude)
                    location1 = results[0].geometry.location;
                } else
                {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
            geocoder.geocode( { 'address': address2}, function(results, status)
            {
                if (status == google.maps.GeocoderStatus.OK)
                {
                    //location of second address (latitude + longitude)
                    location2 = results[0].geometry.location;
                    // calling the showMap() function to create and show the map
                    showMap();
                } else
                {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
    }
       
    // creates and shows the map
    function showMap()
    {
        // center of the map (compute the mean value between the two locations)
        latlng = new google.maps.LatLng((location1.lat()+location2.lat())/2,(location1.lng()+location2.lng())/2);
       
        // set map options
            // set zoom level
            // set center
            // map type
        var mapOptions =
        {
            zoom: 1,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.HYBRID
        };
       
        // create a new map object
            // set the div id where it will be shown
            // set the map options
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
       
        // show route between the points
        directionsService = new google.maps.DirectionsService();
        directionsDisplay = new google.maps.DirectionsRenderer(
        {
            suppressMarkers: true,
            suppressInfoWindows: true
        });
        directionsDisplay.setMap(map);
        var request = {
            origin:location1,
            destination:location2,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function(response, status)
        {
            if (status == google.maps.DirectionsStatus.OK)
            {
                directionsDisplay.setDirections(response);
                distance = "The distance between the two points on the chosen route is: "+response.routes[0].legs[0].distance.text;
                distance += "<br/>The aproximative driving time is: "+response.routes[0].legs[0].duration.text;
                document.getElementById("distance_road").innerHTML = distance;
            }
        });
       
        // show a line between the two points
        var line = new google.maps.Polyline({
            map: map,
            path: [location1, location2],
            strokeWeight: 7,
            strokeOpacity: 0.8,
            strokeColor: "#FFAA00"
        });
       
        // create the markers for the two locations       
        var marker1 = new google.maps.Marker({
            map: map,
            position: location1,
            title: "First location"
        });
        var marker2 = new google.maps.Marker({
            map: map,
            position: location2,
            title: "Second location"
        });
       
        // create the text to be shown in the infowindows
        var text1 = '<div id="content">'+
                '<h1 id="firstHeading">First location</h1>'+
                '<div id="bodyContent">'+
                '<p>Coordinates: '+location1+'</p>'+
                '<p>Address: '+address1+'</p>'+
                '</div>'+
                '</div>';
               
        var text2 = '<div id="content">'+
            '<h1 id="firstHeading">Second location</h1>'+
            '<div id="bodyContent">'+
            '<p>Coordinates: '+location2+'</p>'+
            '<p>Address: '+address2+'</p>'+
            '</div>'+
            '</div>';
       
        // create info boxes for the two markers
        var infowindow1 = new google.maps.InfoWindow({
            content: text1
        });
        var infowindow2 = new google.maps.InfoWindow({
            content: text2
        });

        // add action events so the info windows will be shown when the marker is clicked
        google.maps.event.addListener(marker1, 'click', function() {
            infowindow1.open(map,marker1);
        });
        google.maps.event.addListener(marker2, 'click', function() {
            infowindow2.open(map,marker1);
        });
       
        // compute distance between the two points
        var R = 6371;
        var dLat = toRad(location2.lat()-location1.lat());
        var dLon = toRad(location2.lng()-location1.lng());
       
        var dLat1 = toRad(location1.lat());
        var dLat2 = toRad(location2.lat());
       
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(dLat1) * Math.cos(dLat1) *
                Math.sin(dLon/2) * Math.sin(dLon/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        var d = R * c;
       
        document.getElementById("distance_direct").innerHTML = "<br/>The distance between the two points (in a straight line) is: "+d;
    }
   
    function toRad(deg)
    {
        return deg * Math.PI/180;
    }
</script>

FireBug in Internet Explorer

Firebug In Internet Explorer
Working : Copy this Script  to the URL ..


javascript:var%20firebug=

Friday, March 18, 2011

Php Pagination Script



<!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1. Code to connect to your DB - place or include your code to connect to database.                                                +
2. $tbl_name - your table name.                                                                                                    +
3. $adjacents - how many adjacent pages should be shown on each side.                                                            +
4. $targetpage - is the name of file ex. I saved this file as pagination.php, my $targetpage should be "pagination.php".        +
5. $limit - how many items to show per page.                                                                                    +
6. "SELECT column_name - change to your own column.                                                                                +
7. Replace your own while..loop here - place your code to echo results here.                                                    +
                                                                                                                                +   
                                                                                                                                +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
-->   
<?php
    /*
        Place code to connect to your DB here.
    */
    include('config.php');    // include your code to connect to DB.

    $tbl_name="";        //your table name
    // How many adjacent pages should be shown on each side?
    $adjacents = 3;
   
    /* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    $query = "SELECT COUNT(*) as num FROM $tbl_name";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];
    
    /* Setup vars for query. */
    $targetpage = "filename.php";     //your file name  (the name of this file)
    $limit = 2;                                 //how many items to show per page
    $page = $_GET['page'];
    if($page) 
        $start = ($page - 1) * $limit;             //first item to display on this page
    else
        $start = 0;                                //if no page var is given, set start to 0
    
    /* Get data. */
    $sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
    $result = mysql_query($sql);
    
    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                    //if no page var is given, default to 1.
    $prev = $page - 1;                            //previous page is page - 1
    $next = $page + 1;                            //next page is page + 1
    $lastpage = ceil($total_pages/$limit);        //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                        //last page minus 1
    
    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {    
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
        else
            $pagination.= "<span class=\"disabled\">« previous</span>";    
        
        //pages    
        if ($lastpage < 7 + ($adjacents * 2))    //not enough pages to bother breaking it up
        {    
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
            }
        }
        
        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
        else
            $pagination.= "<span class=\"disabled\">next »</span>";
        $pagination.= "</div>\n";        
    }
?>

    <?php
        while($row = mysql_fetch_array($result))
        {
    
        // Your while loop here
    
        }
    ?>

<?=$pagination?>
     

Sunday, March 13, 2011

CSS transforms

Rotation 
<style type="text/css">
.rotation {
    -moz-transform: rotate(-20deg);
    background-color:#000;
    width:500px;
    height:500px;
   -webkit-transform: rotate(-20deg);
   -o-transform: rotate(-20deg);
   margin:100px;
}
.skew {
    -moz-transform: skewx(10deg) translatex(150px);
    -moz-transform-origin: bottom left;
    -webkit-transform: skewx(10deg) translatex(150px);
    -webkit-transform-origin: bottom left;
    -o-transform: skewx(10deg) translatex(150px);
    -o-transform-origin: bottom left;
      margin:100px;
}
</style>

<div class="rotation">
  <iframe src="http://www.genioplus.blogspot.com/" width="500" height="500"></iframe>
</div>

<div class="skew">
  <iframe src="http://genioplus.blogspot.com/" width="400" height="200"></iframe>
</div>

Example

Thursday, March 10, 2011

CSS Text Shadow

Regular text shadow:

p { text-shadow: 1px 1px 1px #000; } 

Multiple shadows:

p { text-shadow: 1px 1px 1px #000, 3px 3px 5px blue; }


The first two values specify the length of the shadow offset. The first value specifies the horizontal distance and the second specifies the vertical distance of the shadow. The third value specifies the blur radius and the last value describes the color of the shadow:
1. value = The X-coordinate
2. value = The Y-coordinate
3. value = The blur radius
4. value = The color of the shadow
Using positive numbers as the first two values ends up with placing the shadow to the right of the text horizontally (first value) and placing the shadow below the text vertically (second value).
The third value, the blur radius, is an optional value which can be specified but don’t have to. It’s the amount of pixels the text is stretched which causes a blur effect. If you don’t use the third value it is treated as if you specified a blur radius of zero.
Also, remember you can use RGBA values for the color, for example, a 40% transparency of white:

p { text-shadow: 0px 2px 2px rgba(255, 255, 255, 0.4); }

Examples


 p    
    { 
    text-shadow: 1px 1px 10px #000; 
    font-size:100px;
    color:#fff;
    }


 h1   
    {
    text-shadow: 1px 1px 1px yellow, 3px 3px 1px #000;
    font-size:100px;
    color:#fff;
    }


 h3 
    {
    text-shadow: 0.2em 0.5em 0.1em #600,
    -0.3em 0.1em 0.1em #060,
    0.4em -0.3em 0.1em #006;
    font-size:100px;
    }


h4 {
    text-shadow: -1px 0 black, 0 1px black,
     1px 0 black, 0 -1px black;
     font-size:100px;
     color:#fff;
    } 


/*This one is Internet Exlorer */
h2 
    {
    height: 5em;
    font-size:100px;
    color:#009900;
    filter: Shadow(Color=#666666,Direction=125, Strength=10);
}

Wednesday, March 9, 2011

Hiding Content for Accessibility

For years now, we've used a number of techniques for hiding content offscreen for accessibility purposes. We do this because the content is still accessible to screenreaders while being removed from the interface for sighted users.
An article over at Adaptive Themes reviews a number of techniques for hiding content that were considered for inclusion on a Drupal 7 project (but certainly applicable to any project).
Here is a summary of those techniques and the pitfalls of each technique:
Text Indent

.element-invisible {
  text-indent: -9999em;
  outline: 0;
}
Unfortunately, this technique doesn't work with RTL (Right-to-Left) languages.

Position Absolute and Collapsed

.element-invisible {
  height: 0;
  overflow: hidden;
  position: absolute;
}

In this case, Apple's Voice Over will not read content within an element that has 
zero height.

Position Absolute and Offscreen

.element-invisible {
  position: absolute;
  top: -999999em;
  left: auto;
  width: 1px;
  height: 1px;
  overflow:hidden;
}

In this case, if you have focusable content within the positioned
element, the page will scroll to that element, thus creating an
unsettling jump for sighted users.

Clip Method

.element-invisible {
  position: absolute !important;
  clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
  clip: rect(1px, 1px, 1px, 1px);
}

 The article ends with this final technique and is the solution they ended up using on their project.

With the work I've been doing at Yahoo!, we had been using the Position Absolute and Offscreen method. But sometimes we wanted to set focus to offscreen content. We had switched our technique to the Clip Method but uncovered differing behaviour between browsers.
Everything works great in Internet Explorer and Firefox. However, in Webkit (Chrome and Safari) and Opera, there's an interesting behavior when the element is at the edge of the screen. If the element, when unclipped, is large enough to force a horizontal scrollbar, will force a scrollbar even when clipped.
This seems to go against the CSS 2.1 guidelines that say:
Content that has been clipped does not cause overflow.
However, by forcing a scrollbar in Webkit and Opera, it does, in fact, seem to cause overflow. So how did we get around this?

Positioned, Clipped, and (almost) Collapsed

We combine a few techniques into one:
.element-invisible {
  position: absolute !important;
  height: 1px; width: 1px; 
  overflow: hidden;
  clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
  clip: rect(1px, 1px, 1px, 1px);
}
Using absolute positioning, we take the element out of flow so as not to affect the layout of anything around it. With a height of 1px, the element should still be visible for Voice Over to read the content. The clipping removes any visible trace of the element from the page.
Any focusable elements inside are still focusable, so depending on placement within the overall layout, some considered placement may still be of concern. Although, I might question why you are focusing on an element that was so far removed from the overall flow of the document.
We've only begun using and testing this technique, so even this may not be perfect. Any feedback and suggestions are quite welcome.

Tuesday, March 8, 2011

Pure CSS Gradients

CSS Gradients
One of the many cool CSS additions to come out of Webkit is the ability to specify gradients. Whereever you would normally specify an image using a url() syntax, you can specify -webkit-gradient instead. Probably the most likely scenario will be for background images but you could use it for border-image or list-style-image, too.
background-image: -webkit-gradient(linear, 0 top, 0 bottom, from(#496178), to(#2E4960));
The syntax takes a gradient type as the first parameter: linear or radial. The next two values indicate the start and stop points of the gradient. Each parameter after that is a color-stop(x, y) function where x is a percentage or a value between 0 and 1 and y is the colour value. from and to are shortcuts for color-stop(0, y) and color-stop(1, y) respectively. This implementation mirrors the functionality within the canvas specification.
CSS gradients have made their way into the W3C as a draft spec, although the syntax is different from how Webkit has implemented it. Firefox 3.6 has just been released and now includes CSS gradients using this newer syntax which separates the two types of gradients into their own syntax: -moz-linear-gradient and -moz-radial-gradient.
background-image: -moz-linear-gradient(90deg, #496178, #2E4960);
The first parameter is the position or angle. There are a number of ways
that the shorthand can be calculated and degrees are likely the easiest
to use. If you're doing a gradient from top to bottom, the angle can be
ignored altogether and the colour stops are all that need to be
specified.
 background-image: -moz-linear-gradient(#496178, #2E4960);
There's no need to specify the color-stop, from or to functions like with the webkit gradients. You can specify multiple colour stops and it'll create a gradient between each one. If you wish to adjust the position of where the gradient transitions, you can specify it as a second value with the color stop.
background-image: -moz-linear-gradient(#496178, #323232 20%, #2E4960);
You can also use values, too, if you wanted to create semi-opaque gradients.

Monday, March 7, 2011

Using jQuery for Background Image Animations


After reading Dave Shea's article on CSS Sprites using jQuery to produce animation effects, I felt like playing around with things to see what could be done but accomplish it with a simpler HTML structure (no need for adding superfluous tags) and simpler code, too.

Changing the position of the background image felt to be the best approach to creating the type of effect we're looking for (and I'm not the first to think so: see the examples at the end of this article).

jQuery is a great library for this type of task but out of the box, it can't animate background position properly because of the need to animate two values instead of just one (too bad not all browsers implemented the non-standard background-position-x and -y like Internet Explorer). Grab the latest version (1.0.2 as of this writing) of the Background-Position plugin. Previous versions didn't support negative or decimal values properly.

The HTML

Nobody likes adding extra HTML to pull off all the fancy stuff and therefore, we're looking at a very simple unordered list:

<ul>
 <li><a href="#">Home</a></li>
 <li><a href="#">About</a></li>
 <li><a href="#">Contact</a></li>
</ul>



The Basic CSS
 For this, I've just floated the navigation and set each of the links to display block with a little padding. Nothing fancy (which is the whole point).

ul 
{
 list-style:none;
 margin:0;
 padding:0;
}
li {
 float:left;
 width:100px;
 margin:0;
 padding:0;
 text-align:center;
}
li a {
 display:block;
 padding:5px 10px;
 height:100%;
 color:#FFF;
 text-decoration:none;
 border-right:1px solid #FFF;
}
li a {
 background:url(bg.jpg) repeat 0 0;
}
li a:hover {
 background-position:50px 0;
}
Notice that a hover state has been declared. Users who visit the page without JavaScript will, at least, still be able to view the final state. I've declared the background on the li a separately to make it stand out, but an initial background position needs to be set along with the background image you want to use for the effect.
The Image
The Script
Finally, the script to put this altogether is really straightforward. The animation needs to run when the user moves their mouse over and out of the navigation.

$('#nav a')

 .css( {backgroundPosition: "0 0"} )
 .mouseover(function(){
  $(this).stop().animate(
   {backgroundPosition:"(0 -250px)"}, 
   {duration:500})
  })
 .mouseout(function(){
  $(this).stop().animate(
   {backgroundPosition:"(0 0)"}, 
   {duration:500})
  })

The elements are captured via the $ function and given a default CSS. This is necessary for both Internet Explorer and Firefox 2, which don't report the default background position. In IE, you can get around the issue using background-position-x and -y but it's easier just to set the initial values in the script.

Then the elements are animated using the mouseover and mouseout events.

The key thing to note is that any animation is stopped before attempting to animate again. This avoids animations queuing up from repeatedly moving the mouse in and out of the element.

jQuery also has a hover method which can be used instead of separately using the mouseover and mouseout methods. The reason I didn't was because I like the clarity that is provided by explicitly declaring them. The hover method takes two parameters: the function to run when over and the function to run when out.

Saturday, March 5, 2011

Text Rotation with CSS



CSS
<style type="text/css">
body
    {
    margin:0px;
    padding:0px;
    }
#main
    {
    float:left;
    width:105px;
    margin:10px;
    height:88px;
    mos-radius:10px;
    -moz-border-radius-topright:10px;
    -moz-border-radius-bottomleft:10px;
    border-radius: 15px;
    background-color:#66CC00;
    font-family:Verdana, Arial, Helvetica, sans-serif;
  
    }
#main1
    {
    float:left;
    width:105px;
    margin:10px;
    height:88px;
    mos-radius:10px;
    -moz-border-radius-topleft:10px;
    -moz-border-radius-bottomright:10px;
    border-radius: 15px;
    background-color:#00FFFF;
    font-family:Verdana, Arial, Helvetica, sans-serif;
  
    }
.date
    {
    font-size:50px;
    padding:0px;
    margin:0px;
    width:76px;
    float:left;
    text-align:center;
    }
.month
    {
    font-size:18px;
    text-align:center;
  
    }
.year
    {
    -webkit-transform: rotate(-90deg);
    -moz-transform: rotate(-90deg);
    float:left;
    width:23px;
    top:32px;
    left:0px;
    font-size:17px;
    text-align:center;
    position:relative;
    }
</style>

/*Code For Internet Explorer*/

<!--[if IE]>
    <style>
        .year {
            filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
            top:9px;       
        }
      
    </style>
    <![endif]-->


HTML

<div id="main">
   <span class="date">22</span>
   <span class="year">1990</span>
   <div class="month">December</div>
</div>

Friday, March 4, 2011

Control.ScrollBar : Pure JavaScript/CSS scroll bars for Prototype

Thursday, March 3, 2011

cross-browser gradient backgrounds without images

cross-browser gradient backgrounds without images 
Summary
This experiment allows for gradient backgrounds on elements by defining a six character hex value for the start and end color as part of the class attribute. No background-images are required. This has been tested and verified to work in MSIE6, Firefox 1.0.7, Safari 2.0.1, Opera 7.5 &amp; 8.5 on Windows 2K, XP and OS X.
How it Works
The behavior attaches itself to the onload event and walks through the DOM looking for objects with a class name containing the word "gradient" in the 0th position of the string.
Upon finding an object that matches the criteria, it is added to an array which is returned to the caller upon completion. The class attribute of the object is then split into an array which assumes that the starting color will be in the first index, the end color in the second and the vertical or horizontal designation in the third.
The following would result in a white to black vertical gradient:

<p class="gradient ffffff 000000 vertical">hello world!</p>

Download source file

Since the javascript only cares about the first four class names in the class attribute string, additional class names specific to your document can be appended to the end, i.e. class="gradient ff0000 0000ff vertical footer".

Wednesday, March 2, 2011

Html 5 [CANVAS tag]

What is HTML5?
HTML5 will be the new standard for HTML, XHTML, and the HTML DOM.
The previous version of HTML came in 1999. The web has changed a lot since then.
HTML5 is still a work in progress. However, most modern browsers have some HTML5 support.
  
What is Canvas?

The HTML5 canvas element uses JavaScript to draw graphics on a web page.
A canvas is a rectangular area, and you control every pixel of it.
The canvas element has several methods for drawing paths, boxes, circles, characters, and adding images.

Create a Canvas Element
 <canvas id="myCanvas" width="200" height="100"></canvas>

Example Copy this script and past into a Editor (Dreamweaver) and run this script 
                                                                                                                              Live Demo
                                                                          
<!DOCTYPE HTML>
<html>
<head>
<script>

// animation globals
var t=0;
var frameInterval = 10; // in ms
var canvas = null; // canvas DOM object
var context = null; // canvas context

// ball globals
var ballRadius = 10;

// physics globals
var collisionDamper = 0.4;
var floorFriction = 0.005;
var mouseForceMultiplier = 4;
var restoreForce =0.02;

var mouseX = 99999;
var mouseY = 99999;

var balls = null;

function Ball(x,y,vx,vy,color) {
    this.x=x;
    this.y=y;
    this.vx=vx;
    this.vy=vy;
    this.color=color;

    this.origX = x;
    this.origY = y;
}

function init() {
    canvas=document.getElementById("myCanvas");
    context=canvas.getContext("2d");
    initStageObjects();
    setInterval(updateStage, frameInterval);
}

function updateStage() {
    t+=frameInterval;
    clearCanvas();
    updateStageObjects();
    drawStageObjects();  
}

function initStageObjects() {
    balls = new Array();

    var blue = "#3A5BCD";
    var red="#EF2B36";
    var yellow = "#FFC636";
    var green="#02A817";
     var oOffset = 67;
    // S
    balls.push(new Ball(173,63,0,0,blue));
    balls.push(new Ball(158,53,0,0,blue));
    balls.push(new Ball(143,52,0,0,blue));
    balls.push(new Ball(130,53,0,0,blue));
    balls.push(new Ball(117,58,0,0,blue));
    balls.push(new Ball(110,70,0,0,blue));
    balls.push(new Ball(113,82,0,0,blue));
    balls.push(new Ball(125,87,0,0,blue));
    balls.push(new Ball(143,90,0,0,blue));
    balls.push(new Ball(159,90,0,0,blue));
    balls.push(new Ball(170,99,0,0,blue));
    balls.push(new Ball(170,115,0,0,blue));
    balls.push(new Ball(160,128,0,0,blue));
    balls.push(new Ball(145,128,0,0,blue));
    balls.push(new Ball(160,128,0,0,blue));
    balls.push(new Ball(130,128,0,0,blue));
    balls.push(new Ball(110,128,0,0,blue));

     //H
     balls.push(new Ball(200,49,0,0,green));
    balls.push(new Ball(200,50,0,0,green));
    balls.push(new Ball(200,61,0,0,green));
    balls.push(new Ball(200,73,0,0,green));
    balls.push(new Ball(200,89,0,0,green));
    balls.push(new Ball(200,105,0,0,green));
    balls.push(new Ball(200,118,0,0,green));
    balls.push(new Ball(200,128,0,0,green));
    balls.push(new Ball(200,128,0,0,green));

    balls.push(new Ball(220,90,0,0,green));
  
    balls.push(new Ball(240,49,0,0,green));
    balls.push(new Ball(240,50,0,0,green));
    balls.push(new Ball(240,61,0,0,green));
    balls.push(new Ball(240,73,0,0,green));
    balls.push(new Ball(240,89,0,0,green));
    balls.push(new Ball(240,105,0,0,green));
    balls.push(new Ball(240,118,0,0,green));
    balls.push(new Ball(240,128,0,0,green));
    balls.push(new Ball(240,128,0,0,green));
  
    // A
  
    balls.push(new Ball(270,128,0,0,yellow));
    balls.push(new Ball(270,110,0,0,yellow));
    balls.push(new Ball(270,93,0,0,yellow));
    balls.push(new Ball(270,75,0,0,yellow));
    balls.push(new Ball(270,60,0,0,yellow));
    balls.push(new Ball(280,50,0,0,yellow));
    balls.push(new Ball(295,50,0,0,yellow));
    balls.push(new Ball(312,63,0,0,yellow));
    balls.push(new Ball(305,50,0,0,yellow));
    balls.push(new Ball(315,80,0,0,yellow));
    balls.push(new Ball(315,100,0,0,yellow));
    balls.push(new Ball(315,115,0,0,yellow));
    balls.push(new Ball(315,128,0,0,yellow));
    balls.push(new Ball(285,100,0,0,yellow));
    balls.push(new Ball(305,100,0,0,yellow));

    // N
    balls.push(new Ball(350,49,0,0,green));
    balls.push(new Ball(350,50,0,0,green));
    balls.push(new Ball(350,61,0,0,green));
    balls.push(new Ball(350,73,0,0,green));
    balls.push(new Ball(350,89,0,0,green));
    balls.push(new Ball(350,105,0,0,green));
    balls.push(new Ball(350,118,0,0,green));
    balls.push(new Ball(350,128,0,0,green));
    balls.push(new Ball(350,128,0,0,green));

    balls.push(new Ball(360,48,0,0,green));
    balls.push(new Ball(365,65,0,0,green));
    balls.push(new Ball(370,83,0,0,green));
    balls.push(new Ball(375,100,0,0,green));
    balls.push(new Ball(380,115,0,0,green));
    balls.push(new Ball(385,130,0,0,green));
  
    balls.push(new Ball(400,49,0,0,green));
    balls.push(new Ball(400,50,0,0,green));
    balls.push(new Ball(400,61,0,0,green));
    balls.push(new Ball(400,73,0,0,green));
    balls.push(new Ball(400,89,0,0,green));
    balls.push(new Ball(400,105,0,0,green));
    balls.push(new Ball(400,118,0,0,green));
    balls.push(new Ball(400,128,0,0,green));
    balls.push(new Ball(393,135,0,0,green));
  
     //u
  
    balls.push(new Ball(430,128,0,0,yellow));
    balls.push(new Ball(430,110,0,0,yellow));
    balls.push(new Ball(430,93,0,0,yellow));
    balls.push(new Ball(430,75,0,0,yellow));
    balls.push(new Ball(430,60,0,0,yellow));
    balls.push(new Ball(430,50,0,0,yellow));
    balls.push(new Ball(430,50,0,0,yellow));
    balls.push(new Ball(430,63,0,0,yellow));
    balls.push(new Ball(470,65,0,0,yellow));
    balls.push(new Ball(440,140,0,0,yellow));
    balls.push(new Ball(460,140,0,0,yellow));
    balls.push(new Ball(470,50,0,0,yellow));
    balls.push(new Ball(470,80,0,0,yellow));
    balls.push(new Ball(470,100,0,0,yellow));
    balls.push(new Ball(470,115,0,0,yellow));
    balls.push(new Ball(470,128,0,0,yellow));
    balls.push(new Ball(470,100,0,0,yellow));
    balls.push(new Ball(470,100,0,0,yellow));
  
    // L
    /*balls.push(new Ball(394,49,0,0,green));
    balls.push(new Ball(381,50,0,0,green));
    balls.push(new Ball(391,61,0,0,green));
    balls.push(new Ball(390,73,0,0,green));
    balls.push(new Ball(392,89,0,0,green));
    balls.push(new Ball(390,105,0,0,green));
    balls.push(new Ball(390,118,0,0,green));
    balls.push(new Ball(388,128,0,0,green));
    balls.push(new Ball(400,128,0,0,green));

    // E
    balls.push(new Ball(426,101,0,0,red));
    balls.push(new Ball(436,98,0,0,red));
    balls.push(new Ball(451,95,0,0,red));
    balls.push(new Ball(449,83,0,0,red));
    balls.push(new Ball(443,78,0,0,red));
    balls.push(new Ball(430,77,0,0,red));
    balls.push(new Ball(418,82,0,0,red));
    balls.push(new Ball(414,93,0,0,red));
    balls.push(new Ball(412,108,0,0,red));
    balls.push(new Ball(420,120,0,0,red));
    balls.push(new Ball(430,127,0,0,red));
    balls.push(new Ball(442,130,0,0,red));
    balls.push(new Ball(450,125,0,0,red));
 */
}

function drawStageObjects() {
    for (var n=0; n<balls.length; n++) {  
        context.beginPath();
        context.arc(balls[n].x,balls[n].y,ballRadius,
            0,2*Math.PI,false);
        context.fillStyle=balls[n].color;
        context.fill();
    }


}

function updateStageObjects() {

    for (var n=0; n<balls.length; n++) {

        // set ball position based on velocity
        balls[n].y+=balls[n].vy;
        balls[n].x+=balls[n].vx;

        // restore forces



        if (balls[n].x > balls[n].origX) {
            balls[n].vx -= restoreForce;
        }
        else {
            balls[n].vx += restoreForce;
        }
        if (balls[n].y > balls[n].origY) {
            balls[n].vy -= restoreForce;
        }
        else {
            balls[n].vy += restoreForce;
        }



        // mouse forces
        var distX = balls[n].x - mouseX;
        var distY = balls[n].y - mouseY;

        var radius = Math.sqrt(Math.pow(distX,2) +
            Math.pow(distY,2));

        var totalDist = Math.abs(distX) + Math.abs(distY);

        var forceX = (Math.abs(distX) / totalDist) *
            (1/radius) * mouseForceMultiplier;
        var forceY = (Math.abs(distY) / totalDist) *
            (1/radius) * mouseForceMultiplier;

        if (distX>0) { // mouse is left of ball
            balls[n].vx += forceX;
        }
        else {
            balls[n].vx -= forceX;
        }
        if (distY>0) { // mouse is on top of ball
            balls[n].vy += forceY;
        }
        else {
            balls[n].vy -= forceY;
        }


        // floor friction
        if (balls[n].vx>0) {
            balls[n].vx-=floorFriction;
        }
        else if (balls[n].vx<0) {
            balls[n].vx+=floorFriction;
        }
        if (balls[n].vy>0) {
            balls[n].vy-=floorFriction;
        }
        else if (balls[n].vy<0) {
            balls[n].vy+=floorFriction;
        }

        // floor condition
        if (balls[n].y > (canvas.height-ballRadius)) {
            balls[n].y=canvas.height-ballRadius-2;
            balls[n].vy*=-1;
            balls[n].vy+=collisionDamper;
        }

        // ceiling condition
        if (balls[n].y < (ballRadius)) {
            balls[n].y=ballRadius+2;
            balls[n].vy*=-1;
            balls[n].vy-=collisionDamper;
        }

        // right wall condition
        if (balls[n].x > (canvas.width-ballRadius)) {
            balls[n].x=canvas.width-ballRadius-2;
            balls[n].vx*=-1;
            balls[n].vx+=collisionDamper;
        }

        // left wall condition
        if (balls[n].x < (ballRadius)) {
            balls[n].x=ballRadius+2;
            balls[n].vx*=-1;
            balls[n].vx-=collisionDamper;
        }  
    }
}

function clearCanvas() {
    context.clearRect(0,0,canvas.width, canvas.height);
}

function handleMouseMove(evt) {
    mouseX = evt.clientX - canvas.offsetLeft;
    mouseY = evt.clientY - canvas.offsetTop;  
}

function handleMouseOut() {
    mouseX = 99999;
    mouseY = 99999;
}

</script>
</head>
<body onLoad="init()"  >
    <canvas id="myCanvas" width="578" height="200"
        onmousemove="handleMouseMove(event)"
        onmouseout="handleMouseOut()"

    ></canvas>
</body>
</html>


Out Put 



Tuesday, March 1, 2011

Font Embedding in HTML (compatible for all Browsers including IE6)

Font Formats
Generally speaking, these days, a font on our system is going to be one of two formats: TrueType (with a .ttf file extension) or OpenType (with a .otf file extension). While it would be nice to be able to just throw a font like this on the web and link it up, we're hit with two major limitations.
  1. Licensing, and
  2. Browser Support 

Browser Support

Which leads me into the other major issue, browser support. Font embedding with a TrueType or OpenType font only works as of Firefox 3.5, Safari 3.1, and Opera 10. (You can enable it in your copy of Chrome 2 by using a command line switch.)
Okay, that's decent already but we can do better. We can get Internet Explorer 4+, Chrome 0.3+, Opera 9+ and even a little mobile Safari action.

internet explorer only supports .eot  type

You can convert  .TTF  font to EOT Font  click here
You can convert  .OTF  font to TTF Font  click here

   script                                                                                                                                                         
<style type="text/css">
@font-face {
     font-family: "YourFont";
     src: url(font.eot);                    /* IE */
     src: local("YourFont"), url(font.ttf) format("truetype");  /* non-IE */
}
.test
    {
    font-family:YourFont;
    font-size:50px;
    }
</style>


Rounded Corners in Webkit (Pure CSS)

Much like Mozilla Firefox, Webkit allows developers to create pure CSS rounded corners in all browsers that use the Webkit engine, including Apple's Safari and Google's Chrome. Check out how easy implementing rounded corners can be!

 .sample {
  -webkit-border-radius:10px; /* all corners */
  -webkit-border-top-left-radius:15px; /* top left corner */
  -webkit-border-top-right-radius:50px; /* top right corner */
  -webkit-border-bottom-left-radius:15px; /* bottom left corner */
  -webkit-border-bottom-right-radius:50px; /* bottom right corner */
  border:1px solid #fc0; /* border color, per usual */
}