Search code examples
javascriptleap-motion

How to get the event when fingers disappear


I am making leap motion application.

I can detect when fingers come up in the sensor, but I can't detect when all fingers disappear.

Is there any way to tell the moment all fingers disappear like mouseout event for mouse?


Solution

  • There is no event when pointables objets (fingers or tools) disappear, but to handle this you can keep the previous frame, or information about the previous frame, and check if fingers were in the device area.

    var lastNbFingers = 0;
    
    // Setup Leap loop with frame callback function
    var controllerOptions = {enableGestures: true};
    Leap.loop(controllerOptions, function(frame)
    {
      var nbFingers = 0; // Cpt for all fingers from all hands in the current frame
      var fingerRemoved = false; // are all the fingers just removed from device area ?
    
      // how many fingers in our scene
      for (var h = 0; h < frame.hands.length; ++h)
      {
        var hand = frame.hands[h];
        nbFingers += hand.fingers.length;
      }
    
      if (nbFingers > 0)   // there are fingers
        lastNbFingers = nbFingers;
      else if (lastNbFingers > 0)  // there is no finger on current frame, but some on previous frame
        {
          lastNbFingers = 0;
          fingerRemoved = true;   
        }
       if (fingerRemoved)
          // do some stuff
    
    });