How to move a DIV with your fingers… and the Leap Motion

For those who don’t know Leap Motion is a 3D gesture controller that captures your hands and fingers movements.

Leap Motion

playing Jenga with the Leap Motion

One cool thing the Leap Motion guys did was to create a JavaScript Client Library which allows us to capture the device data in a html page.

Since I don’t have a glass monitor like in Minority Report or 3d holograms like Tony Stark has, I though I’d be cool to use a browser and the Leap Motion to manipulate DOM objects.

So let’s say we have a DIV like this one:

1
<div id="box" style="width: 200px; border: 1px solid #666666; padding: 10px 10px 70px 10px; display: inline-block;"></div>

And now we don’t want to move it with our thoughts because that’s impossible but instead with our hands:

First thing to do -obviously- is include the Leap Motion JS lib.

Secondly, we need to capture the frames data the device is sending us through the Leap.loop event function:

1
2
3
4
Leap.loop(function(frame)
{
    console.log("Frame:" + frame); 
});

Third step is to capture the current frames vectors [x,y,z] and use this information to manipulate DOM objects. The way to do this is to track the position derived from the motion between a first frame and the current frame:

1
2
3
4
5
6
7
8
9
var firstValidFrame = null
 
Leap.loop(function(frame)
{
    // use first frame as comparison
    if (!firstValidFrame) firstValidFrame = frame;
    var translation = frame.translation(firstValidFrame);
    console.log("X:" + translation.x);
});

Now the fun part! As you can see we could grab the x coordinate with translation.x (in millimiters) – on the same way we could do with the coordinates y and z.

By using the x coordinates we can manipulate the DIV marginLeft CSS property and simulate as if the user were moving the DIV with her hand.

If you have the Leap Motion try this full example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf8">
  <title></title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script src="leap.js"></script>
  </head>
  <body>
<script>
var firstValidFrame = null
 
Leap.loop(function(frame)
{
  // use the first frame to serve as comparison
  if (!firstValidFrame) firstValidFrame = frame;
  var translation = frame.translation(firstValidFrame);
  $('#box').css({marginLeft: translation.x});
});
</script>
 
  <div id="box" style="width: 200px; border: 1px solid #666666; padding: 10px 10px 70px 10px; display: inline-block"></div>
  </body>
</html>

Don’t have a Leap Motion? Pre order it!

Preventing parent page to reload after closing a dialog in jQuery Mobile

Long short story, my app flow is something like 1) user clicks on Send button, 2) app make a post to the same page and 3) user clicks on the newly created row and open a dialog window.

What happens is if you close the dialog jQuery Mobile will automatically reload the parent page. In my flow, resulting in inserting a new row.

The solution? data-dom-cache=”true”. You need to place that property in your div data-role=”page”. jQuery Mobile will cache your page and prevent dialogs reload any page after you close them.

More info: http://jquerymobile.com/test/docs/pages/page-cache.html

Install PyCrypto for Windows in less than 5 minutes

I just tried to install PyCrypto by running “easy_install.exe pip PyCrypto” and I got this pretty message:

error: Setup script exited with error: Unable to find vcvarsall.bat

So I googled about it and ran into some time expensive and crazy solutions like “Install Microsoft Visual Studio” or “Compile the package with mingw32″.

Fortunally there is a guy that already compiled it for us. You can download it here, install it and done!

5 ways to improve your Business with Twitter

Twitter is a media platform that allows you to publish text messages up to 140 characters on the web.

It is usually used to communicate opinions, events, news, articles and ideas.

Twitter allows us to be aware of what other people post with a frequency and velocity never seen before.

This creates certain atmosphere of privacy where we can know what other people are doing at the exact moment they are doing it.

[Read more...]

5 steps to take your Business on Facebook

The words Business and Facebook may seem to be incompatible. There is a widespread belief that social networks are for “wasting time with friends”. However reality shows that Facebook has become a mass media platform, to gaining and lose lots of customers at full speed, thanks to instant comunication between people.

What loses my Business for not taking part on Facebook? Having an honest and open relationship with their current clients and market.
In any social network people are used to say whatever they like, on what they want. This radical honesty helps you to have a two different ways of communication to improve your business.

[Read more...]