index.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>Proof of concept - Objets tangibles</title>
  7. </head>
  8. <body>
  9. <script src="/jquery-1.7.2.js"></script>
  10. <script src="/lodash.js"></script>
  11. <script src="/socket.io/socket.io.js"></script>
  12. <script src="/tuio.js"></script>
  13. <style>
  14. body {
  15. background: #000;
  16. margin: 0;
  17. }
  18. .tuioCursor {
  19. background: #fff;
  20. height: 8px;
  21. left: 0;
  22. position: absolute;
  23. top: 0;
  24. width: 8px;
  25. }
  26. </style>
  27. <script>
  28. function getHypotenuse(touch1, touch2) {
  29. var w = Math.abs(touch1.w - touch2.w);
  30. var h = Math.abs(touch1.h - touch2.h);
  31. return Math.sqrt(Math.pow(w, 2) + Math.pow(h, 2));
  32. }
  33. $(function() {
  34. var dotGroups = [];
  35. var client = new Tuio.Client({
  36. host: 'http://localhost:5000'
  37. }),
  38. screenW = $(document).width(),
  39. screenH = $(document).height()
  40. cursors = {},
  41. onConnect = function() {
  42. console.log('connected');
  43. },
  44. onAddTuioCursor = function(addCursor) {
  45. var $addCursor = $('<div class="tuioCursor"></div>');
  46. $('body').append($addCursor);
  47. cursors[addCursor.getCursorId()] = $addCursor;
  48. onUpdateTuioCursor(addCursor);
  49. },
  50. onUpdateTuioCursor = function(updateCursor) {
  51. var $updateCursor = cursors[updateCursor.getCursorId()];
  52. $updateCursor.css({
  53. left: updateCursor.getScreenX(screenW),
  54. top: updateCursor.getScreenY(screenH)
  55. });
  56. var cursorIds = Object.keys(cursors);
  57. var dots = [];
  58. cursorIds.forEach(function(cursor) {
  59. dots.push({
  60. id: cursor,
  61. w: cursors[cursor][0].offsetLeft,
  62. h: cursors[cursor][0].offsetTop
  63. });
  64. });
  65. var groups = [];
  66. if (dots.length > 2) {
  67. for (var i = 0; i < dots.length; i++) {
  68. for (var j = 0; j < dots.length; j++) {
  69. if (j !== i) {
  70. var hyp = getHypotenuse(dots[i], dots[j]);
  71. if (hyp <= 260) {
  72. if (groups.length === 0) {
  73. groups.push([i, j]);
  74. } else {
  75. groups.forEach((group, groupIndex) => {
  76. if (!group.includes(i) && !group.includes(j)) {
  77. groups.push([i, j]);
  78. } else {
  79. if (group.includes(i)) {
  80. if (!group.includes(j)) {
  81. groups[groupIndex].push(j);
  82. }
  83. } else {
  84. groups.push([j]);
  85. }
  86. if (group.includes(j)) {
  87. if (!group.includes(i)) {
  88. groups[groupIndex].push(i);
  89. }
  90. } else {
  91. groups.push([i]);
  92. }
  93. }
  94. });
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. dotGroups = groups;
  102. console.log(dotGroups);
  103. },
  104. onRemoveTuioCursor = function(removeCursor) {
  105. var $removeCursor = cursors[removeCursor.getCursorId()];
  106. $removeCursor.remove();
  107. delete[removeCursor.getCursorId()];
  108. },
  109. onAddTuioObject = function(addObject) {
  110. console.log(addObject);
  111. },
  112. onUpdateTuioObject = function(updateObject) {
  113. console.log(updateObject);
  114. },
  115. onRemoveTuioObject = function(removeObject) {
  116. console.log(removeObject);
  117. },
  118. onRefresh = function(time) {
  119. };
  120. client.on("connect", onConnect);
  121. client.on("addTuioCursor", onAddTuioCursor);
  122. client.on("updateTuioCursor", onUpdateTuioCursor);
  123. client.on("removeTuioCursor", onRemoveTuioCursor);
  124. client.on("addTuioObject", onAddTuioObject);
  125. client.on("updateTuioObject", onUpdateTuioObject);
  126. client.on("removeTuioObject", onRemoveTuioObject);
  127. client.on("refresh", onRefresh);
  128. client.connect();
  129. });
  130. </script>
  131. </body>
  132. </html>