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