test.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. canvas {
  15. border: gray;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <!--<div id="box"></div>-->
  21. <canvas id="myCanvas" width="1000" height="800"></canvas>
  22. <script>
  23. var canvasElement = document.querySelector("#myCanvas");
  24. canvasElement.addEventListener('touchstart', (evt) => {
  25. return eventHandler(evt, 'touchstart');
  26. });
  27. canvasElement.addEventListener('touchmove', (evt) => {
  28. return eventHandler(evt, 'touchmove');
  29. });
  30. canvasElement.addEventListener('touchend', (evt) => {
  31. return eventHandler(evt, 'touchend');
  32. });
  33. var context = canvasElement.getContext("2d");
  34. context.font = '48px serif';
  35. function distance(touches, id1, id2){
  36. return Math.sqrt( (touches[id1].pageX-touches[id2].pageX)**2 + (touches[id1].pageY-touches[id2].pageY)**2 );
  37. }
  38. function angle(touches, id){
  39. var AB = distance(touches, id, (id+1)%3);
  40. var BC = distance(touches, (id+1)%3, (id+2)%3);
  41. var AC = distance(touches, (id+2)%3, id);
  42. return Math.acos((BC*BC+AB*AB-AC*AC)/(2*BC*AB))*180/Math.PI;
  43. }
  44. const eventHandler = (evt, type) => {
  45. console.log("test");
  46. if(evt.touches.length === 3){
  47. console.log(evt.touches);
  48. context.clearRect(0, 0, canvasElement.width, canvasElement.height);
  49. // the triangle
  50. context.beginPath();
  51. context.moveTo(evt.touches[0].pageX, evt.touches[0].pageY);
  52. context.fillText(distance(evt.touches, 0, 1), 10, 50);
  53. context.fillText(angle(evt.touches, 0), 10, 100);
  54. context.lineTo(evt.touches[1].pageX, evt.touches[1].pageY);
  55. context.fillText(distance(evt.touches, 1, 2), 10, 150);
  56. context.fillText(angle(evt.touches, 1), 10, 200);
  57. context.lineTo(evt.touches[2].pageX, evt.touches[2].pageY);
  58. context.fillText(distance(evt.touches, 2, 0), 10, 250);
  59. context.fillText(angle(evt.touches, 2), 10, 300);
  60. context.closePath();
  61. // the outline
  62. context.lineWidth = 1;
  63. context.strokeStyle = '#666666';
  64. context.stroke();
  65. // the fill color
  66. context.fillStyle = "#FFCC00";
  67. context.fill();
  68. }
  69. evt.preventDefault();
  70. };
  71. </script>
  72. </body>
  73. </html>