index.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. context = canvas.getContext('2d');
  128. const drawTriangle = (a, b, c) => {
  129. // console.log(context, Math.round(a.x), Math.round(a.y), Math.round(b.x), Math.round(b.y), Math.round(c.x), Math.round(c.y));
  130. const path = new Path2D();
  131. path.moveTo(Math.round(a.x), Math.round(a.y));
  132. path.lineTo(Math.round(b.x), Math.round(b.y));
  133. path.lineTo(Math.round(c.x), Math.round(c.y));
  134. context.fill(path);
  135. };
  136. const client = new Tuio.Client({
  137. host: `http://localhost:${config.app.httpPort}`
  138. });
  139. const cursors = {};
  140. const objects = {};
  141. onConnect = function() {
  142. if (config.app.debug) {
  143. console.log('connected to web-socket');
  144. }
  145. },
  146. onAddTuioCursor = function(addCursor) {
  147. /*
  148. var $addCursor = $('<div class="tuioCursor"></div>');
  149. $('body').append($addCursor);
  150. cursors[addCursor.getCursorId()] = $addCursor;
  151. */
  152. onUpdateTuioCursor(addCursor);
  153. if (config.app.debug && config.app.debugLog.frontend.receiver.onAddTuioCursor) {
  154. console.log('addTuioCursor', addCursor);
  155. }
  156. },
  157. onUpdateTuioCursor = function(updateCursor) {
  158. /*
  159. var $updateCursor = cursors[updateCursor.getCursorId()];
  160. $updateCursor.css({
  161. left: updateCursor.getScreenX(screenW),
  162. top: updateCursor.getScreenY(screenH)
  163. });
  164. */
  165. if (config.app.debug && config.app.debugLog.frontend.receiver.onUpdateTuioCursor) {
  166. console.log('updateTuioCursor', updateCursor);
  167. }
  168. },
  169. onRemoveTuioCursor = function(removeCursor) {
  170. /*
  171. var $removeCursor = cursors[removeCursor.getCursorId()];
  172. $removeCursor.remove();
  173. delete[removeCursor.getCursorId()];
  174. */
  175. if (config.app.debug && config.app.debugLog.frontend.receiver.onRemoveTuioCursor) {
  176. console.log('removeTuioCursor', removeCursor);
  177. }
  178. },
  179. onAddTuioObject = function(addObject) {
  180. /*
  181. var $addObject = $('<div class="tuioObj"></div>');
  182. $('body').append($addObject);
  183. objects[addObject.symbolID] = $addObject;
  184. */
  185. onUpdateTuioObject(addObject);
  186. if (config.app.debug && config.app.debugLog.frontend.receiver.onAddTuioObject) {
  187. console.log('addTuioObject', addObject);
  188. }
  189. },
  190. onUpdateTuioObject = function(updateObject) {
  191. /*
  192. var $updateObject = objects[updateObject.symbolID];
  193. $updateObject.css({
  194. left: updateObject.getScreenX(screenW),
  195. top: updateObject.getScreenY(screenH),
  196. rotate : updateObject.getAngleDegrees()
  197. });
  198. */
  199. const apexAngle = 35;
  200. const mediatrice = 300;
  201. if (config.app.debugDisplay) {
  202. const x = updateObject.xPos;
  203. const y = updateObject.yPos;
  204. const apex = {
  205. x: Math.cos(updateObject.angle * Math.PI / 180) * (mediatrice / 2) + x,
  206. y: Math.sin(updateObject.angle * Math.PI / 180) * (mediatrice / 2) + y
  207. };
  208. const b = {
  209. x: Math.cos((apexAngle / 2) * Math.PI / 180) * (mediatrice / 2) + apex.x,
  210. y: Math.sin((apexAngle / 2) * Math.PI / 180) * (mediatrice / 2) + apex.y
  211. };
  212. const c = {
  213. x: Math.cos((-1 * apexAngle / 2) * Math.PI / 180) * (mediatrice / 2) + apex.x,
  214. y: Math.sin((-1 * apexAngle / 2) * Math.PI / 180) * (mediatrice / 2) + apex.y
  215. };
  216. drawTriangle(apex, b, c);
  217. }
  218. if (config.app.debug && config.app.debugLog.frontend.receiver.onUpdateTuioObject) {
  219. console.log('updateTuioObject', updateObject);
  220. }
  221. },
  222. onRemoveTuioObject = function(removeObject) {
  223. /*
  224. var $removeObject = objects[removeObject.symbolID];
  225. $removeObject.remove();
  226. delete[removeObject.symbolID];
  227. */
  228. if (config.app.debug && config.app.debugLog.frontend.receiver.onRemoveTuioObject) {
  229. console.log('removeTuioObject', removeObject);
  230. }
  231. },
  232. onRefresh = function(time) {
  233. if (config.app.debug && config.app.debugLog.frontend.receiver.onRefresh) {
  234. console.log('refresh', time);
  235. }
  236. };
  237. client.on('connect', onConnect);
  238. client.on('addTuioCursor', onAddTuioCursor);
  239. client.on('updateTuioCursor', onUpdateTuioCursor);
  240. client.on('removeTuioCursor', onRemoveTuioCursor);
  241. client.on('addTuioObject', onAddTuioObject);
  242. client.on('updateTuioObject', onUpdateTuioObject);
  243. client.on('removeTuioObject', onRemoveTuioObject);
  244. client.on('refresh', onRefresh);
  245. client.connect();
  246. });
  247. </script>
  248. </body>
  249. </html>