index.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <!DOCTYPE html>
  2. <html style="height: 100%;">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>ASR tangibles</title>
  7. <style>
  8. body {
  9. width: 100%;
  10. height: 100%;
  11. margin: 0;
  12. background-color: #EEEEEE;
  13. }
  14. #box {
  15. width: 50%;
  16. height: 50%;
  17. background-color: #0b5397;
  18. margin:0px;
  19. }
  20. #box2 {
  21. width: 50%;
  22. height: 50%;
  23. background-color: #0b9756;
  24. margin:0px;
  25. }
  26. .tuioCursor {
  27. background: #111;
  28. height: 8px;
  29. left: 0;
  30. position: absolute;
  31. top: 0;
  32. width: 8px;
  33. }
  34. .tuioObj {
  35. background: #111;
  36. height: 15px;
  37. left: 0;
  38. position: absolute;
  39. top: 0;
  40. width: 8px;
  41. rotate: 0deg;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <!--<div id="box"></div>-->
  47. <div id="box"></div>
  48. <div id="box2"></div>
  49. <!--<script src="/jquery-1.7.2.js"></script>-->
  50. <script src="/lodash.js"></script>
  51. <script src="/socket.io/socket.io.js"></script>
  52. <script src="/tuio.js"></script>
  53. <script>
  54. let config = {};
  55. const getConfig = () => {
  56. return fetch('/config.json');
  57. };
  58. getConfig().then(response => {
  59. return response.json();
  60. }).then(data => {
  61. config = data;
  62. if (config.app.debug) {
  63. console.log('### Frontend debug activated ###');
  64. }
  65. const screenW = window.innerWidth;
  66. const screenH = window.innerHeight;
  67. if (config.app.debug && config.app.debugLog.frontend.emitter.screen) {
  68. console.log(`-- Screen width: ${screenW} --`);
  69. console.log(`-- Screen height: ${screenH} --`);
  70. }
  71. /* -------
  72. * EMITTER
  73. * -------
  74. */
  75. const box = document.getElementById('box');
  76. const box2 = document.getElementById('box2');
  77. const boxes = [box, box2]
  78. const eventHandler = (evt, type) => {
  79. const touches = [];
  80. for (var i = 0; i < evt.touches.length; i++) {
  81. const boxId = boxes.findIndex(box => box.id == evt.touches[i].target.id);
  82. if(boxId >= 0){ // we only keep touches in the initial div
  83. touches.push({
  84. clientX: evt.touches[i].clientX,
  85. clientY: evt.touches[i].clientY,
  86. force: evt.touches[i].force,
  87. identifier: evt.touches[i].identifier,
  88. pageX: evt.touches[i].pageX,
  89. pageY: evt.touches[i].pageY,
  90. radiusX: evt.touches[i].radiusX,
  91. radiusY: evt.touches[i].radiusY,
  92. rotationAngle: evt.touches[i].rotationAngle,
  93. screenX: evt.touches[i].screenX,
  94. screenY: evt.touches[i].screenY,
  95. target: boxId});
  96. }
  97. };
  98. if (config.app.debug && config.app.debugLog.frontend.emitter.events) {
  99. console.log(`-- Event: ${type} --`, touches);
  100. }
  101. if (config.app.debug && config.app.debugDisplay) {
  102. //box.innerHTML = evt.touches.length;
  103. }
  104. fetch(`http://localhost:${config.app.httpPort}/emitter/json`, {
  105. method: 'POST',
  106. mode: 'cors',
  107. cache: 'no-cache',
  108. credentials: 'same-origin',
  109. headers: {
  110. 'Content-Type': 'application/json'
  111. },
  112. redirect: 'follow',
  113. referrerPolicy: 'no-referrer',
  114. body: JSON.stringify({
  115. type,
  116. screenW,
  117. screenH,
  118. touches
  119. })
  120. });
  121. evt.preventDefault();
  122. };
  123. box.addEventListener('touchstart', (evt) => {
  124. return eventHandler(evt, 'touchstart');
  125. });
  126. box.addEventListener('touchmove', (evt) => {
  127. return eventHandler(evt, 'touchmove');
  128. });
  129. box.addEventListener('touchend', (evt) => {
  130. return eventHandler(evt, 'touchend');
  131. });
  132. box2.addEventListener('touchstart', (evt) => {
  133. return eventHandler(evt, 'touchstart');
  134. });
  135. box2.addEventListener('touchmove', (evt) => {
  136. return eventHandler(evt, 'touchmove');
  137. });
  138. box2.addEventListener('touchend', (evt) => {
  139. return eventHandler(evt, 'touchend');
  140. });
  141. /* --------
  142. * RECEIVER
  143. * --------
  144. */
  145. const client = new Tuio.Client({
  146. host: `http://localhost:${config.app.httpPort}`
  147. });
  148. onConnect = function() {
  149. if (config.app.debug) {
  150. console.log('connected to web-socket');
  151. }
  152. },
  153. onAddTuioCursor = function(addCursor) {
  154. onUpdateTuioCursor(addCursor);
  155. if (config.app.debug && config.app.debugLog.frontend.receiver.onAddTuioCursor) {
  156. console.log('addTuioCursor', addCursor);
  157. }
  158. },
  159. onUpdateTuioCursor = function(updateCursor) {
  160. if (config.app.debug && config.app.debugLog.frontend.receiver.onUpdateTuioCursor) {
  161. console.log('updateTuioCursor', updateCursor);
  162. }
  163. },
  164. onRemoveTuioCursor = function(removeCursor) {
  165. if (config.app.debug && config.app.debugLog.frontend.receiver.onRemoveTuioCursor) {
  166. console.log('removeTuioCursor', removeCursor);
  167. }
  168. },
  169. onAddTuioObject = function(addObject) {
  170. boxes[addObject.xPos].innerText = addObject.angle + " added " + addObject.symbolId;
  171. boxes[addObject.xPos].style.transform = "rotate(" + addObject.angle + "deg)";
  172. if (config.app.debug && config.app.debugLog.frontend.receiver.onAddTuioObject) {
  173. console.log('addTuioObject', addObject);
  174. }
  175. },
  176. onUpdateTuioObject = function(updateObject) {
  177. boxes[updateObject.xPos].innerText = updateObject.angle + " " + updateObject.symbolId;
  178. boxes[updateObject.xPos].style.transform = "rotate(" + updateObject.angle + "deg)";
  179. if (config.app.debug && config.app.debugLog.frontend.receiver.onUpdateTuioObject) {
  180. console.log('updateTuioObject', updateObject);
  181. }
  182. },
  183. onRemoveTuioObject = function(removeObject) {
  184. boxes[removeObject.xPos].innerText = "removed";
  185. boxes[removeObject.xPos].style.transform = "rotate(0deg)"
  186. if (config.app.debug && config.app.debugLog.frontend.receiver.onRemoveTuioObject) {
  187. console.log('removeTuioObject', removeObject);
  188. }
  189. },
  190. onRefresh = function(time) {
  191. if (config.app.debug && config.app.debugLog.frontend.receiver.onRefresh) {
  192. console.log('refresh', this.objectList);
  193. }
  194. };
  195. client.on('connect', onConnect);
  196. client.on('addTuioCursor', onAddTuioCursor);
  197. client.on('updateTuioCursor', onUpdateTuioCursor);
  198. client.on('removeTuioCursor', onRemoveTuioCursor);
  199. client.on('addTuioObject', onAddTuioObject);
  200. client.on('updateTuioObject', onUpdateTuioObject);
  201. client.on('removeTuioObject', onRemoveTuioObject);
  202. client.on('refresh', onRefresh);
  203. client.connect();
  204. });
  205. </script>
  206. </body>
  207. </html>