Advertisement
Sayak97

Laser Cat

Nov 8th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4.   <head>
  5.   </head>
  6.  
  7.   <body>
  8.     <h1>Hello, LeapJS (V2)!</h1>
  9.     <div id="output"></div>
  10.    
  11.    
  12.    
  13.     <canvas id="canvas" width=1000 height=500></canvas>
  14.     <script src="https://js.leapmotion.com/leap-0.6.4.js"></script>
  15.     <script type="text/javascript">
  16.    
  17.     function concatData(id, data) {
  18.       return id + ": " + data + "<br>";
  19.     }
  20.    
  21.     function getFingerName(fingerType) {
  22.       switch(fingerType) {
  23.         case 0:
  24.           return 'Thumb';
  25.         break;
  26.    
  27.         case 1:
  28.           return 'Index';
  29.         break;
  30.    
  31.         case 2:
  32.           return 'Middle';
  33.         break;
  34.    
  35.         case 3:
  36.           return 'Ring';
  37.         break;
  38.    
  39.         case 4:
  40.           return 'Pinky';
  41.         break;
  42.       }
  43.     }
  44.    
  45.     function concatJointPosition(id, position) {
  46.       return id + ": " + position[0] + ", " + position[1] + ", " + position[2] + "<br>";
  47.     }
  48.    
  49.    //global variables
  50.    
  51.     var output = document.getElementById('output');
  52.     var frameString = "", handString = "", fingerString = "";
  53.     var hand, finger;
  54.     var callibrated = false;
  55.     var d;
  56.     var ctx = document.getElementById('canvas').getContext("2d");
  57.     var circles = [ [75, 75], [675, 200], [300, 300]];
  58.     var options = { enableGestures: true };
  59.     var controller = new Leap.Controller();
  60.     var frameDisplay = document.getElementById('frameID');
  61.     var state = 0;
  62.     var lines = [];
  63.     var curLine = [];
  64.     var dotA;
  65.     var dotB;
  66.     var dotC;
  67.     var numLines = 0;
  68.  
  69.     Leap.loop(options, function(frame) {
  70.         switch (state){
  71.             case 0:
  72.            
  73.                 x = circles[numLines][0];
  74.                 y = circles[numLines][1];
  75.                 makeCircle(x, y);
  76.                 if(lines.length < 100)
  77.                 {
  78.                     tips = getTips(frame);  
  79.                         if(tips.length > 1)
  80.                         {
  81.                         lines.push(tips);
  82.                         }                  
  83.                 }else{
  84.                 lines = avgTips(lines);
  85.                 console.log(lines);
  86.                 state = numLines + 1;
  87.                 }
  88.                 break;
  89.             case 1:
  90.                 curLine.push(lines);
  91.                 lines = [];
  92.                 state = 0;
  93.                 document.body.style.backgroundColor = "purple";
  94.                 if(curLine.length == 2){
  95.                     dotA = makeDot(curLine[0][0], curLine[0][1], curLine[1][0], curLine[1][1]);
  96.                     numLines = 1;
  97.                     curLine = [];
  98.                 document.body.style.backgroundColor = "blue";
  99.                 }
  100.                 break;
  101.             case 2:
  102.                 curLine.push(lines);
  103.                 lines = [];
  104.                 state = 0;
  105.                 document.body.style.backgroundColor = "purple";
  106.                 if(curLine.length == 2){
  107.                     dotB = makeDot(curLine[0][0], curLine[0][1], curLine[1][0], curLine[1][1]);
  108.                     numLines = 2;
  109.                     curLine = [];
  110.                 document.body.style.backgroundColor = "blue";
  111.                 }
  112.                 break;
  113.             case 3:
  114.                 curLine.push(lines);
  115.                 lines = [];
  116.                 state = 0;
  117.                 if(curLine.length == 2){
  118.                     dotC = makeDot(curLine[0][0], curLine[0][1], curLine[1][0], curLine[1][1]);
  119.                     state = 4;
  120.                 }else{
  121.                 document.body.style.backgroundColor = "blue";
  122.                 }
  123.                 break;
  124.             case 4:
  125.                 d = makePlane(dotA, dotB, dotC);
  126.                 alert('Calibrated!')
  127.                 state = 5;
  128.                 document.body.style.backgroundColor = "purple";
  129.                 break;
  130.             case 5:
  131.                 tips = getTips(frame);
  132.                 if(tips.length == 2){
  133.                 inter = makeLinePlane(tips[0], tips[1]);
  134.                 var coord = imprint(inter);
  135.                 console.log("x " + coord[0] + "y " + coord[1]);
  136.                 makeCircle(coord[0], coord[1]);
  137.                 }
  138.                 break;
  139.             }
  140.      
  141.    /*if(!callibrated){
  142.       d = callibrate(frame);
  143.       callibrated = true;
  144.     }
  145.     points = getTips(frame);
  146.     pos = makeLinePlane(points[0], points[1]);
  147.   */
  148.    
  149.     });
  150.    
  151.    
  152.     function makeLinePlane(p1, p2)               // tells the position of the dot on the screen when you through two fingertips
  153.     {
  154.       dir = sub(p2, p1);
  155.       u = mult(dir,-1 * ((dot(p1, d) - 1)/dot(dir, d)));
  156.       u = add(u,p1);
  157.       return u;
  158.     }
  159.  
  160.    
  161.  
  162.  
  163.  
  164.     function makeDot(p1, p2, p3, p4)         // tells you the the position of the dot on the screen when two lines are given to it
  165.     {
  166.       var points = [];
  167.       for (var i = 0; i < 3; i++){
  168.           point = (p1[i] * (p3[i] - p4[i]) - p3[i] * (p1[i] - p2[i])) / ( (p3[i] - p4[i]) - (p1[i] - p2[i]));
  169.           points.push(point);
  170.       }
  171.       return points;
  172.     }
  173.  
  174.    
  175.  
  176.  
  177.  
  178.     function makePlane(d1, d2, d3)       // tells the plane when the position of three points is given to it.
  179.     {
  180.       o = cross(sub(d1, d2), sub(d2, d3));
  181.       plane = div(o, dot(d1, o));
  182.       return plane;
  183.     }
  184.    
  185.  
  186.     function getTips(frame)           // gives the tips of the fingers of the hand
  187.     {
  188.       frameString = concatData("frame_id", frame.id);
  189.       frameString += concatData("num_hands", frame.hands.length);
  190.       frameString += concatData("num_fingers", frame.fingers.length);
  191.       frameString += "<br>";
  192.       var points = [];
  193.       // Showcase some new V2 features
  194.       for (var i = 0, len = frame.hands.length; i < len; i++)
  195.       {
  196.         points.push(frame.hands[i].fingers[1].dipPosition);
  197.       }    
  198.       output.innerHTML = frameString;
  199.            return points;
  200.      
  201.     }
  202.  
  203.     function avgTips(cat)
  204.     {
  205.       var p1 = [0, 0, 0];
  206.       var p2 = [0, 0, 0];
  207.       len = cat.length;
  208.       for(var i = 0; i < len; i++)
  209.       {
  210.         p1 = add(p1, [cat[i][0][0]/len,cat[i][0][1]/len,cat[i][0][2]/len ] );
  211.         p2 = add(p2, [cat[i][1][0]/len,cat[i][1][1]/len,cat[i][1][2]/len ] );
  212.       }
  213.       return [p1, p2];
  214.     }
  215.    
  216.     function imprint(inter){
  217.         console.log("Y: " + inter[1]);
  218.         console.log("Z: " + inter[2]);
  219.         x = ((circles[1][0])*(inter[0] - dotA[0]) + (circles[0][0])*(inter[0] - dotB[0]) )/(dotB[0] - dotA[0]);
  220.         /*a = (circles[1][1])*(inter[1] - dotA[1]);
  221.         b = (circles[0][1])*(inter[1] - dotB[1])
  222.         c = (dotA[1] - dotB[1]);
  223.         console.log(a+' '+ b + ' '+ c);
  224.         y = 250 - ((b - a)/c);*/
  225.         y = inter[1];
  226.         return [x, y];
  227.     }
  228.  
  229.     function makeCircle(x, y){
  230.     ctx.beginPath();
  231.     ctx.arc(x, y, 25, 0, Math.PI*2, true);
  232.     ctx.closePath();
  233.     ctx.fillStyle="red";
  234.     ctx.fill();
  235.     }
  236.    
  237.    
  238.     function add(v1,v2)
  239.     {
  240.     return [v1[0]+v2[0],v1[1]+v2[1],v1[2]+v2[2]];
  241.     }
  242.  
  243.     function sub(v1,v2)
  244.     {
  245.     return [v1[0]-v2[0],v1[1]-v2[1],v1[2]-v2[2]];
  246.     }
  247.    
  248.     function dot(v1,v2)
  249.     {
  250.     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  251.     }
  252.     function cross(v1,v2)
  253.     {
  254.     return [v1[1]*v2[2]-v1[2]*v2[1],v1[0]*v2[2]-v1[2]*v2[0],v1[0]*v2[1] - v1[1]*v2[0]];
  255.     }
  256.    
  257.     function div(v1, s)
  258.     {
  259.     ret = [];
  260.     for(var i = 0; i < v1.length; i++){
  261.         ret.push(v1[i] / s);
  262.     }
  263.     return ret;
  264.     }
  265.    
  266.     function mult(v1, s){
  267.     ret = [];
  268.     for(var i = 0; i < v1.length; i++){
  269.         ret.push(v1[i] * s);
  270.     }
  271.     return ret;
  272.     }
  273.  
  274.     </script>
  275.   </body>
  276. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement