index.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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: 100%;
  16. height: 100%;
  17. }
  18. .tuioCursor {
  19. background: #111;
  20. height: 8px;
  21. left: 0;
  22. position: absolute;
  23. top: 0;
  24. width: 8px;
  25. }
  26. .tuioObj {
  27. background: #111;
  28. height: 15px;
  29. left: 0;
  30. position: absolute;
  31. top: 0;
  32. width: 8px;
  33. rotate: 0deg;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <!--<div id="box"></div>-->
  39. <canvas id="box"></canvas>
  40. <!--<script src="/jquery-1.7.2.js"></script>-->
  41. <script src="/lodash.js"></script>
  42. <script src="/socket.io/socket.io.js"></script>
  43. <script src="/tuio.js"></script>
  44. <script>
  45. let config = {};
  46. const getConfig = () => {
  47. return fetch('/config.json');
  48. };
  49. getConfig().then(response => {
  50. return response.json();
  51. }).then(data => {
  52. config = data;
  53. const urlParams = new URLSearchParams(window.location.search);
  54. const sectionParam = urlParams.get('section');
  55. const section = sectionParam ? parseInt(sectionParam, 10) : 100;
  56. if (config.app.debug) {
  57. console.log('### Frontend debug activated ###');
  58. }
  59. const screenW = window.innerWidth;
  60. const screenH = window.innerHeight;
  61. if (config.app.debug && config.app.debugLog.frontend.emitter.screen) {
  62. console.log(`-- Screen width: ${screenW} --`);
  63. console.log(`-- Screen height: ${screenH} --`);
  64. }
  65. /* -------
  66. * EMITTER
  67. * -------
  68. */
  69. const box = document.getElementById('box');
  70. const eventHandler = (evt, type) => {
  71. const touches = [];
  72. for (var i = 0; i < evt.changedTouches.length; i++) {
  73. touches[i] = {
  74. clientX: evt.changedTouches[i].clientX,
  75. clientY: evt.changedTouches[i].clientY,
  76. force: evt.changedTouches[i].force,
  77. identifier: ((evt.changedTouches[i].identifier + 1) % 100) + section,
  78. pageX: evt.changedTouches[i].pageX,
  79. pageY: evt.changedTouches[i].pageY,
  80. radiusX: evt.changedTouches[i].radiusX,
  81. radiusY: evt.changedTouches[i].radiusY,
  82. rotationAngle: evt.changedTouches[i].rotationAngle,
  83. screenX: evt.changedTouches[i].screenX,
  84. screenY: evt.changedTouches[i].screenY
  85. };
  86. };
  87. if (config.app.debug && config.app.debugLog.frontend.emitter.events) {
  88. console.log(`-- Event: ${type} --`, touches);
  89. }
  90. if (config.app.debug && config.app.debugDisplay) {
  91. box.innerHTML = evt.touches.length;
  92. }
  93. fetch(`http://localhost:${config.app.httpPort}/emitter/json`, {
  94. method: 'POST',
  95. mode: 'cors',
  96. cache: 'no-cache',
  97. credentials: 'same-origin',
  98. headers: {
  99. 'Content-Type': 'application/json'
  100. },
  101. redirect: 'follow',
  102. referrerPolicy: 'no-referrer',
  103. body: JSON.stringify({
  104. type,
  105. section,
  106. screenW,
  107. screenH,
  108. touches
  109. })
  110. });
  111. evt.preventDefault();
  112. };
  113. box.addEventListener('touchstart', (evt) => {
  114. return eventHandler(evt, 'touchstart');
  115. });
  116. box.addEventListener('touchmove', (evt) => {
  117. return eventHandler(evt, 'touchmove');
  118. });
  119. box.addEventListener('touchend', (evt) => {
  120. return eventHandler(evt, 'touchend');
  121. });
  122. /* --------
  123. * RECEIVER
  124. * --------
  125. */
  126. const canvas = document.getElementById('box');
  127. canvas.width = screenW;
  128. canvas.height = screenH;
  129. context = canvas.getContext('2d');
  130. const drawTriangle = (a, b, c) => {
  131. context.clearRect(0, 0, canvas.width, canvas.height);
  132. const path = new Path2D();
  133. path.moveTo(Math.round(a.x), Math.round(a.y));
  134. path.lineTo(Math.round(b.x), Math.round(b.y));
  135. path.lineTo(Math.round(c.x), Math.round(c.y));
  136. context.fill(path);
  137. };
  138. const client = new Tuio.Client({
  139. host: `http://localhost:${config.app.httpPort}`
  140. });
  141. const cursors = {};
  142. const objects = {};
  143. onConnect = function() {
  144. if (config.app.debug) {
  145. console.log('connected to web-socket');
  146. }
  147. },
  148. onAddTuioCursor = function(addCursor) {
  149. /*
  150. var $addCursor = $('<div class="tuioCursor"></div>');
  151. $('body').append($addCursor);
  152. cursors[addCursor.getCursorId()] = $addCursor;
  153. */
  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. /*
  161. var $updateCursor = cursors[updateCursor.getCursorId()];
  162. $updateCursor.css({
  163. left: updateCursor.getScreenX(screenW),
  164. top: updateCursor.getScreenY(screenH)
  165. });
  166. */
  167. if (config.app.debug && config.app.debugLog.frontend.receiver.onUpdateTuioCursor) {
  168. console.log('updateTuioCursor', updateCursor);
  169. }
  170. },
  171. onRemoveTuioCursor = function(removeCursor) {
  172. /*
  173. var $removeCursor = cursors[removeCursor.getCursorId()];
  174. $removeCursor.remove();
  175. delete[removeCursor.getCursorId()];
  176. */
  177. if (config.app.debug && config.app.debugLog.frontend.receiver.onRemoveTuioCursor) {
  178. console.log('removeTuioCursor', removeCursor);
  179. }
  180. },
  181. onAddTuioObject = function(addObject) {
  182. /*
  183. var $addObject = $('<div class="tuioObj"></div>');
  184. $('body').append($addObject);
  185. objects[addObject.symbolID] = $addObject;
  186. */
  187. onUpdateTuioObject(addObject);
  188. if (config.app.debug && config.app.debugLog.frontend.receiver.onAddTuioObject) {
  189. console.log('addTuioObject', addObject);
  190. }
  191. },
  192. onUpdateTuioObject = function(updateObject) {
  193. /*
  194. var $updateObject = objects[updateObject.symbolID];
  195. $updateObject.css({
  196. left: updateObject.getScreenX(screenW),
  197. top: updateObject.getScreenY(screenH),
  198. rotate : updateObject.getAngleDegrees()
  199. });
  200. */
  201. const apexAngle = 35;
  202. const mediatrice = 300;
  203. if (config.app.debugDisplay) {
  204. const x = updateObject.xPos;
  205. const y = updateObject.yPos;
  206. const apex = {
  207. x: Math.cos(updateObject.angle * Math.PI / 180) * (mediatrice / 2) + x,
  208. y: Math.sin(updateObject.angle * Math.PI / 180) * (mediatrice / 2) + y
  209. };
  210. const b = {
  211. x: Math.cos((apexAngle / 2) * Math.PI / 180) * (mediatrice / 2) + apex.x,
  212. y: Math.sin((apexAngle / 2) * Math.PI / 180) * (mediatrice / 2) + apex.y
  213. };
  214. const c = {
  215. x: Math.cos((-1 * apexAngle / 2) * Math.PI / 180) * (mediatrice / 2) + apex.x,
  216. y: Math.sin((-1 * apexAngle / 2) * Math.PI / 180) * (mediatrice / 2) + apex.y
  217. };
  218. drawTriangle(apex, b, c);
  219. }
  220. if (config.app.debug && config.app.debugLog.frontend.receiver.onUpdateTuioObject) {
  221. console.log('updateTuioObject', updateObject);
  222. }
  223. },
  224. onRemoveTuioObject = function(removeObject) {
  225. /*
  226. var $removeObject = objects[removeObject.symbolID];
  227. $removeObject.remove();
  228. delete[removeObject.symbolID];
  229. */
  230. if (config.app.debug && config.app.debugLog.frontend.receiver.onRemoveTuioObject) {
  231. console.log('removeTuioObject', removeObject);
  232. }
  233. },
  234. onRefresh = function(time) {
  235. if (config.app.debug && config.app.debugLog.frontend.receiver.onRefresh) {
  236. console.log('refresh', time);
  237. }
  238. };
  239. client.on('connect', onConnect);
  240. client.on('addTuioCursor', onAddTuioCursor);
  241. client.on('updateTuioCursor', onUpdateTuioCursor);
  242. client.on('removeTuioCursor', onRemoveTuioCursor);
  243. client.on('addTuioObject', onAddTuioObject);
  244. client.on('updateTuioObject', onUpdateTuioObject);
  245. client.on('removeTuioObject', onRemoveTuioObject);
  246. client.on('refresh', onRefresh);
  247. client.connect();
  248. });
  249. </script>
  250. </body>
  251. </html>