473,412 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,412 developers and data experts.

Build a Multi-Touch Interface with the Fidget jQuery Plugin

Simon27
This article explains how my Fidget jQuery plugin can be used to easily use the Touch events within Mobile WebKit to detect various gestures. You will build a very simple interface that allows a lecturer to organise students into 2 different groups, simply by dragging.

Because of the way devices like the iPad handle traditional mouse events, it is better to use the available Touch events instead, and Fidget simplifies a process that can become quite complex.

Start by downloading the attached files an explore their contents. This demo works best on an iPad (for the size) but should work on other Mobile WebKit browsers. Each code block can be tested, providing results before the end of this tutorial is reached.




The project folder contains the HTML page, a CSS file to style it, jQuery, the Fidget plugin and students.js, for your own code.

View the HTML from an iPad and you will see 3 boxes, one green filled with student names, and 2 others for student groups A and B. In this tutorial you will use Fidget to build an interface that allows the lecturer to group their students into the 2 groups by dragging.

Open the blank students.js file and add the following code:


Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function() {
  2.     $('.student').fidget({
  3.         dragThis: true
  4.     });
  5. });

Once the page has finished loading, this code finds every HTML element with a class of student
and automatically moves it around the page as the user drags it.

You will be able to move the students around but they will just stay where you leave them. We need to know when they have been dropped over a group.

Now update students.js with the code in bold:


Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function() {
  2.     $('.student').fidget({
  3.         dragThis: true,
  4.         swipe: dragStudent
  5.     });
  6. });
  7.  
  8.  
  9. function dragStudent(event, fidget) {
  10.  
  11. $(this).css('pointerEvents', 'none');
  12.  
  13. var hoveredElement =
  14.     document.elementFromPoint(fidget.currentFingers[0].pageX,
  15.     fidget.currentFingers[0].pageY);
  16.  
  17. if (fidget.swipe.status == 'move') {
  18.     if ((hoveredElement.id == 'groupA') || (hoveredElement.id == 'groupB')) {
  19.         $(hoveredElement).addClass('hover');
  20.     }     
  21. }
  22.  
  23. $(this).css('pointerEvents', 'auto');
  24.  
  25. }

Save this file and refresh the page on the iPad. Now when a student is dragged over one of the groups a drop shadow highlights the group by adding the class hover to the group.

Amend that function with the code in bold to remove the drop shadow when the finger is no longer over a group:


Expand|Select|Wrap|Line Numbers
  1. function dragStudent(event, fidget) {
  2.  
  3. $(this).css('pointerEvents', 'none');
  4.  
  5. var hoveredElement =
  6.     document.elementFromPoint(fidget.currentFingers[0].pageX,
  7.     fidget.currentFingers[0].pageY);
  8.  
  9. if (fidget.swipe.status == 'move') {
  10.     if ((hoveredElement.id == 'groupA') || (hoveredElement.id == 'groupB')) {
  11.         $(hoveredElement).addClass('hover');
  12.     } else {
  13.             $('#groupA, #groupB').removeClass('hover');
  14.     }
  15. }
  16.  
  17. $(this).css('pointerEvents', 'auto');
  18.  
  19. }

Now then the finger is not over either of the 2 groups the hover class is removed.

This is the “dragStudent” function you gave to Fidget earlier and it takes 2 arguments called event and fidget. fidget is an object with properties that give us information on the current gesture of the user.

The hoveredElement variable is used to find which of the boxes the finger is currently dragging over, based on the x and y coordinates of the first finger on the screen. This finger is fidget.currentFingers[0] and is an array of all the fingers on the screen at any one time. The “zeroth” finger is taken from the array as that will be the first that touched the screen. The pageX and pageY properties give the coordinates of this finger.

The if statement in the code uses the fidget.swipe.status property to check if the fingers on the screen have moved since they started.

The second if statement checks to see if the finger is over either <div> with the ids #groupA and #groupB. If the finger is, then it adds the class of hover to that group. You’ll notice that when you drag one of the students over a group a drop-shadow appears, this is from the hover class.

The else statement is used to remove the shadow – by removing the hover class – when the finger is not over either of the groups.

Now we need to add the following code to detect when the user has let go of a student and where it should be placed. Amend the dragStudent function with the code in bold:


Expand|Select|Wrap|Line Numbers
  1. function dragStudent(event, fidget) {
  2.  
  3. $(this).css('pointerEvents', 'none');
  4.  
  5. var hoveredElement =
  6.     document.elementFromPoint(fidget.currentFingers[0].pageX,
  7.     fidget.currentFingers[0].pageY);
  8.  
  9. if (fidget.swipe.status == 'move') {
  10.     if ((hoveredElement.id == 'groupA') || (hoveredElement.id == 'groupB')) {
  11.         $(hoveredElement).addClass('hover');
  12.     } else {
  13.         $('#groupA, #groupB').removeClass('hover');
  14.     }    
  15. }
  16.  
  17. if (fidget.swipe.status == 'end') {
  18.     if ((hoveredElement.id == 'groupA') || (hoveredElement.id == 'groupB')) {
  19.         $(hoveredElement).append(this);
  20.         $(hoveredElement).removeClass('hover');
  21.     } else {
  22.         $('#students').append(this);
  23.         $(this).fidget({
  24.             dragThis: true,
  25.             swipe: dragStudent
  26.         });
  27.     }
  28.     $(this).css('top', 0);
  29.     $(this).css('left', 0);
  30. }
  31.  
  32. $(this).css('pointerEvents', 'auto');
  33.  
  34. }
  35.  
Refresh the page on the iPad and you should now be able to drag and drop students into either of the groups, between groups, and back to the main students list.

This code uses another if statement using the same fidget.swipe.status property, but this time checks for when it is “end”, i.e. when the finger has left the screen to drop the student name.

Again, another if statement checks to see if the student was dropped over group A or B, and moves the student HTML element into the group by using the jQuery append method. The class of hover is again removed from here.

The else statement will run when the student has not been dropped on either group A or B. In that case, the student is returned to the main student list and not left in any position on the page.

You will notice that only 2 students can be added to a group without the names disappearing below. To solve this, we can use Fidget to detect a 2 finger pinch gesture and use that to change the size of the groups. Modify the first part of students.js with the code in bold:


Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function() {
  2.     $('.student').fidget({
  3.         dragThis: true,
  4.         swipe: dragStudent
  5.     });
  6.  
  7.     $('#groupA, #groupB').fidget({
  8.         pinch: function resize(event, fidget) {
  9.             $(this).css('height', fidget.pinch.height + 'px');
  10.         }
  11.     });
  12. });

This takes the HTML elements of #groupA and #groupB and tells Fidget to call the resize function when a pinch event occurs on those matched elements.

On refreshing the page, you should now be able to place 2 fingers on one of the groups and pinch outwards to increase the size, or inwards to decrease the size.

We are only interested in the Y direction of movement, as we want to change the height, not the width of the group.

The this keyword is the HTML element that the pinch event is targeting.

I am looking for feedback
I'm currently developing Fidget for my university honours project and any feedback that can be provided (positive and negative) would be hugely appreciated.

Visit my site for more information...
Attached Files
File Type: zip fidget.zip (38.3 KB, 226 views)
Feb 19 '12 #1
0 4321

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gernot Hillier | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi! I'm the developer of a Linux ISDN application which uses embedded Python for controlling the communication. It starts several threads (i.e....
1
by: Stephen Thorne | last post by:
Decorators have been getting lots of air-time at the moment, but only really the syntax. After a short discussion on irc the other night I decided to download python2.4 from experimental and write...
3
by: Amit Dedhia | last post by:
Hi I am developing a Dot net application (involving image processing) on a uni processor. It works well on my machine. I then take all my code on a multi processor, build and run the application...
2
by: Michal Przytulski | last post by:
Hi, I'm looking information abouth runing PHP CLI application in multi-threaded - on http://www.php.net/manual/pl/ref.pcntl.php i found information abouth process control - but is PHP support...
0
by: Yogi_Bear_79 | last post by:
I have a form with 30 text boxes on it. They are set up in three columns, and 10 rows. Each row equals one record in the array. What I want to do is start by reading the first box txtColumnA_1,...
2
by: Rudy Ray Moore | last post by:
How can I modify the project build order of a multi-project workspace under "Visual Studio .net 2003 7.1 c++"? I tried to modify the .sln by hand to influence the build order, but it didn't seem...
27
by: Bernard Bourée | last post by:
Is there a way to overpass the impossibility of VN.NET to accept the multi heritage, that is to allow a class to inherit from TWO mother classes ? -- Bernard Bourée bernard@bouree.net
4
by: Andy_Khosravi | last post by:
I'm trying to build a search utility for users to find 'inquiries' in my database that involves several tables. This is normally easy to do with the query builder, but I have a unique situation...
2
by: Aussie Rules | last post by:
Hi, I have a site that Iwant to either display my text in english or french, based on the users prefernces ? I am new to webforms, but I know in winforms, this is pretty easy with a resource...
1
by: KrazyKasper | last post by:
Access 2003 – Multi-Column List Box – Select Multiple Items I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.