index.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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>Touch detect and send to Node server</title>
  7. </head>
  8. <body style="height: 100%;">
  9. <div id="box" style="width: 100%; height: 100%; background-color: #EEEEEE;"></div>
  10. <script src="/jquery-1.7.2.js"></script>
  11. <script>
  12. /* simulation de touch events */
  13. /*
  14. function sendTouchEvent(x, y, element, eventType) {
  15. const touchObj = new Touch({
  16. identifier: Date.now(),
  17. target: element,
  18. clientX: x,
  19. clientY: y,
  20. radiusX: 2.5,
  21. radiusY: 2.5,
  22. rotationAngle: 10,
  23. force: 0.5,
  24. });
  25. const touchEvent = new TouchEvent(eventType, {
  26. cancelable: true,
  27. bubbles: true,
  28. touches: [touchObj],
  29. targetTouches: [],
  30. changedTouches: [touchObj],
  31. shiftKey: true,
  32. });
  33. element.dispatchEvent(touchEvent);
  34. }
  35. */
  36. const box = document.getElementById('box');
  37. const urlParams = new URLSearchParams(window.location.search);
  38. const sectionParam = urlParams.get('section');
  39. const section = sectionParam ? parseInt(sectionParam, 10) : 0;
  40. console.log(section);
  41. box.addEventListener('touchstart', function(evt) {
  42. console.log(evt);
  43. box.innerHTML = evt.touches.length;
  44. fetch('http://localhost:5001/json', {
  45. method: 'POST',
  46. mode: 'cors',
  47. cache: 'no-cache',
  48. credentials: 'same-origin',
  49. headers: {
  50. 'Content-Type': 'application/json'
  51. },
  52. redirect: 'follow',
  53. referrerPolicy: 'no-referrer',
  54. body: JSON.stringify({
  55. event: 'touchstart',
  56. clientX: evt.changedTouches[0].clientX,
  57. clientY: evt.changedTouches[0].clientY,
  58. force: evt.changedTouches[0].force,
  59. identifier: section + evt.changedTouches[0].identifier,
  60. pageX: evt.changedTouches[0].pageX,
  61. pageY: evt.changedTouches[0].pageY,
  62. radiusX: evt.changedTouches[0].radiusX,
  63. radiusY: evt.changedTouches[0].radiusY,
  64. rotationAngle: evt.changedTouches[0].rotationAngle,
  65. screenX: evt.changedTouches[0].screenX,
  66. screenY: evt.changedTouches[0].screenY
  67. })
  68. });
  69. evt.preventDefault();
  70. });
  71. box.addEventListener('touchmove', function(evt) {
  72. console.log(evt);
  73. const touches = [];
  74. for (var i = 0; i < evt.changedTouches.length; i++) {
  75. touches[i] = {
  76. clientX: evt.changedTouches[i].clientX,
  77. clientY: evt.changedTouches[i].clientY,
  78. force: evt.changedTouches[i].force,
  79. identifier: section + evt.changedTouches[i].identifier,
  80. pageX: evt.changedTouches[i].pageX,
  81. pageY: evt.changedTouches[i].pageY,
  82. radiusX: evt.changedTouches[i].radiusX,
  83. radiusY: evt.changedTouches[i].radiusY,
  84. rotationAngle: evt.changedTouches[i].rotationAngle,
  85. screenX: evt.changedTouches[i].screenX,
  86. screenY: evt.changedTouches[i].screenY
  87. };
  88. };
  89. box.innerHTML = evt.touches.length;
  90. fetch('http://localhost:5001/json', {
  91. method: 'POST',
  92. mode: 'cors',
  93. cache: 'no-cache',
  94. credentials: 'same-origin',
  95. headers: {
  96. 'Content-Type': 'application/json'
  97. },
  98. redirect: 'follow',
  99. referrerPolicy: 'no-referrer',
  100. body: JSON.stringify({
  101. event: 'touchmove',
  102. screenW: $(document).width(),
  103. screenH: $(document).height(),
  104. changedTouches: touches
  105. })
  106. });
  107. evt.preventDefault();
  108. });
  109. box.addEventListener('touchend', function(evt) {
  110. console.log(evt);
  111. box.innerHTML = evt.touches.length;
  112. fetch('http://localhost:5001/json', {
  113. method: 'POST',
  114. mode: 'cors',
  115. cache: 'no-cache',
  116. credentials: 'same-origin',
  117. headers: {
  118. 'Content-Type': 'application/json'
  119. },
  120. redirect: 'follow',
  121. referrerPolicy: 'no-referrer',
  122. body: JSON.stringify({
  123. event: 'touchend',
  124. clientX: evt.changedTouches[0].clientX,
  125. clientY: evt.changedTouches[0].clientY,
  126. force: evt.changedTouches[0].force,
  127. identifier: section + evt.changedTouches[0].identifier,
  128. pageX: evt.changedTouches[0].pageX,
  129. pageY: evt.changedTouches[0].pageY,
  130. radiusX: evt.changedTouches[0].radiusX,
  131. radiusY: evt.changedTouches[0].radiusY,
  132. rotationAngle: evt.changedTouches[0].rotationAngle,
  133. screenX: evt.changedTouches[0].screenX,
  134. screenY: evt.changedTouches[0].screenY
  135. })
  136. });
  137. evt.preventDefault();
  138. });
  139. if (!(typeof box.ontouchstart != 'undefined')) {
  140. box.style.border = '1px solid red';
  141. }
  142. /*
  143. sendTouchEvent(150, 150, box, 'touchstart');
  144. sendTouchEvent(220, 200, box, 'touchmove');
  145. sendTouchEvent(220, 200, box, 'touchend');
  146. */
  147. </script>
  148. </body>
  149. </html>