Posts Tagged ‘JavaScript’

Analog Clock using Javascript & Raphael

May 11th, 2010

Click to see online demo

Last weekend, I was playing with Raphaël – A JavaScript Library for drawing/graphics. Its like drawing anything on a canvas, pretty easy and impressive. In that, there was a function call rotate() which can rotate any element(line, circle, rectangle, image etc.) at any angle from a given axis. This gives me an idea to have 3 lines of different color, stroke and length for second, minute and hour hand in a clock. After every second, we need to check the time with the help of javascript Date(). Then rotate the second hand line, about the center axis with [6 * number of seconds] degrees (as 1 sec is equal to 6 degrees). Rotate the minute hand line with [6 * number of minutes] degrees. Rotate the hour hand with [30 * number of hours] degrees (as 1 hour is equal to 30 degrees). Combining this logic, I built a clock (with just 30 lines of code), whose demo is given below,

For this demo, I have used images of different dials (which I have downloaded from google) as backgound. Upon which the clock is drawn through Raphaël. Luckly, for me, the center of these dials images excatly conicides with the center for clock. Another option I provided in the demo is to change the color of second, minute and hour hand of the clock according to the dial selected by the user. It can be used anywhere with any size, styling and customization. I can also help you with that.

Open the demo in new window. As this clock is based upon Raphaël (which is dependent upon CSS 3), it may not work in certain versions of different browsers. Kindly get back to in that case with browser name and version. Lastly, your feedback and suggestions are welcomed as always. Raphaël is a powerful library and you can built some really fancy stuff with it. Here is my bit, hope you guys like it.

JQuery Pagination Plugin

January 7th, 2010

Downloaded more than 2000 times.

After using various jQuery plugins for pagination, I decided to write my own jQuery paginator plugin. While creating this plugin, I tried to overcome other plugin’s limitations and make it easy & highly customizable to meet demands of most of developers. Here are few features of this plugin,

1)      Very simple. Just 1 line of code to apply pagination on any div/container.

2)      No need for creating extra divs for pagination.

3)      4 styles of pagination.

4)      Various other options like jump to page, page info and change items per page on fly.

5)      Easy customization and styling.

6)      Nice fading effect during changing of page.

Why use it? Often you have large number of items/data in a div, which not only breaks your design but also looks ugly and lengthy. Apply this plugin on that div and you will get a nice seamless pagination on that div, which don’t break your design and visitors can easily navigate to all the items. You can style or it customize according to your requirements.   See Demo.

Implementation:

1)      Download latest jquery script and paginator.js

2)      Include both scripts in html document in head section.

<script src="js/jquery.js"></script>
<script src="js/paginator.js"></script>

3)      Initiate pagination by  writing following code in head section inside <script> tags,

<script>
$(function(){ $("#<id_of_div>").pagination(); });
</script>

// instead of <id_of_div>, type in id of the div on which you want to create pagination.

Its all you need to do. Load the page on browser and now you can see a simple, basic pagination on the page. See Demo.

Customization:

1)      Style Paginator: There are 3 classes – paginator (container for paginator), active (class for active pages) and inactive (class for current page). We can apply little css on these classes to make paginator looks pretty, either in document itself or in stylesheet. See demo

<style type="text/css">
.active
{
color:#0033CC;
border: solid 1px #0000FF;
padding:3px 5px;
margin:5px;
text-decoration:none;
}
.inactive
{
color:#000000;
cursor:default;
text-decoration:none;
border: solid 1px #0000FF;
background-color:#0099FF;
padding:3px 5px;
margin:5px;
}
</style>

2)      Paginator Style: Open paginator.js, there is a variable called paginatorStyle. This variable accepts values from 1 to 4, for displaying different styles of paginator.  More info is given in comments below that.

3)      Pagination Position: By default the position of paginator is below the div where pagination is applied. In paginator.js you can change paginatorPosition to ‘top’ if you want paginator to be on the top instead of bottom, or ‘both’ if you want it to be on both (top and bottom).

4)      Changing Symbols: Open paginator.js, there are variables like firstPageSymbol, previousPageSymbol, nextPageSymbol, lastPageSymbol. You can change these symbols, so instead of << as symbol for first page, you can write FIRST PAGE, or give img tag if you want fancy image for first page. Also, there is a variable called separator, which will appear in-between page options. Make it blank if you don’t want it.

5)      Changing Items per page and Start Page: By default there are 5 items per page and start page for pagination is first page. You can change them in paginator.js, the variables are named itemsPerPage and startPage.

6)      Enabling other options: There are 3 options available, like goto to page, change items per page, display page info. By default these are disabled, you can enable them according to your needs. You can enable them by replacing false with true as variables value, in paginator.js. More info is given in comments besides each option.

7)      Calling pagination again after changing content: If you change content, upon which pagination is applied in-between, just call initPaginator(); after changing content. It will apply pagination again on the new content with same previous settings.

8)      De-paginating Content : At any time, if you want to remove pagination from the content, just call $("#<id_of_div>").depagination(); You can call $("#<id_of_div>").pagination(); to apply pagination again on same or any other content. Kindly remember, at one time you can apply pagination on only one div or content. If you want to apply pagination on other div during program, first call depagination function on previous div and then pagination function on new div.

9)      Don’t want to open paginator.js: No worries, if you don’t want to change these variables in paginator.js, you can still change these variables in html documents just before calling the pagination function. Like See Demo.

$(function(){
itemsPerPage = 3;
paginatorStyle = 2;
paginatorPosition = 'both';
enableGoToPage = true;
$("#result").pagination();
});

That’s it. Apply it wherever you want. Play around it with, try different styles and options. Email me in case of any bug, error, confusion or you need any further assistance/customization. All kinds of feedbacks are appreciated and welcomed.

I also recently uploaded uncompressed version of the plugin for development purposes, download it. feel free to download and extend it. Also, I have uploaded this plugin on jquery plugin directory. Here is the link – http://plugins.jquery.com/project/jquery-pagination

Tested in all major Browsers

Tested in all major Browsers

Highlight Your Searches

August 7th, 2009

Recently I was strucked with one BIG problem in programming my one of the application. How to highlight the search phases in the result through javascript? The problem was simple enough for any programmer and there are lots of srcipts available online.

However, this problem is lot more complicated than it seems to be. The searched result should be checked for any link, hashtag or at the rate symbol. If found the script needs to convert them into a specific link. That’s fine, I searched on google, found some script and implemented them without any much difficulty.

The real problem arises when after converting all links I need to highlight searched phase in result. The very initial reaction was to search for the phase and replace it with <span class=”highlight”>word</span>. However, this script searches for the word even inside the anchor tag or any other tag … and boom everything is converted into link or some codeline.

Therefore, I needed some script which does not search for the word inside anchor or any tag. I know Regex is the solution but I am not so good at it. Even after lots of searching, I I just found related questions and solutions that never worked for me. The problem presist.

Yesterday, I encountered one of the articles on devthought.com by Guillermo Rauch. He has written code for highlighting searched word as extended function in jquery and mootool. As I was already using Jquery, I decided to give this code a try. I modified the code to fit into my application and it worked flawlessly.

The article can be reached here. It helped me a lot in highlighting the searches in result. It can be used anywhere with any script for highlighting anything. Its really amazing that how any small function can solve BIG problems.