Guest User

Untitled

a guest
Jul 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="jp" xml:lang="jp">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Sketch</title>
  6. <style>
  7. body{
  8. margin: 0px; padding: 0px;
  9. }
  10. canvas {
  11. margin: 0px; padding: 0px;
  12. border-style: double;
  13. border-width: 5px;
  14. border-color: red;
  15. }
  16. </style>
  17. <meta name="viewport" content="width=device-width,user-scalable=no" />
  18. </head>
  19. <body>
  20. <!-- w = 128x2-10, h = 64x2-10 -->
  21. <canvas id="sketch_canvas" width="246" height="118"></canvas>
  22. <!-- <script type="text/javascript" src="sketch.js"></script> -->
  23. <script type="text/javascript">
  24. /**
  25. * Sketch JS
  26. */
  27.  
  28. var ctx = document.getElementById("sketch_canvas").getContext("2d");
  29.  
  30. var pX = new Array();
  31. var pY = new Array();
  32. var isDrag = new Array();
  33. var sequence = "";
  34.  
  35. function isIpodSafari() {
  36. var ua = navigator.userAgent.match(/iP/);
  37. return (ua == null) ? false : true;
  38. }
  39.  
  40. function addPoint(x, y, dragging) {
  41. pX.push(x);
  42. pY.push(y);
  43. isDrag.push(dragging);
  44. }
  45.  
  46. function redraw() {
  47. ctx.strokeStyle = "#00aaaa";
  48. ctx.lineJoin = "round";
  49. ctx.lineWidth = 5;
  50. var ppX, ppY;
  51. for(var i = 0; i < pX.length; i++) {
  52. ctx.beginPath();
  53. if(isDrag[i] && i > 0) {
  54. ppX = pX[i-1];
  55. ppY = pY[i-1];
  56. } else {
  57. ppX = pX[i] - 1;
  58. ppY = pY[i];
  59. }
  60. ctx.moveTo(ppX, ppY);
  61. ctx.lineTo(pX[i], pY[i]);
  62. if(ppX >= 0 && ppX <= 246 && ppY >= 0 && ppY <= 118) {
  63. sequence += Math.round(ppX/2) + "," + Math.round(ppY/2) + ":" +
  64. Math.round(pX[i]/2) + "," + Math.round(pY[i]/2) + ";";
  65. }
  66. ctx.closePath();
  67. ctx.stroke();
  68. }
  69. if(sendable) {
  70. ws.send(JSON.stringify({value:sequence + "."}));
  71. sequence = "";
  72. sendable = false;
  73. }
  74. pX = new Array();
  75. pY = new Array();
  76. }
  77.  
  78. var sendable = false;
  79. var ws = new WebSocket("ws://192.168.254.23:8888/glcd");
  80. ws.onopen = function() {};
  81. ws.onmessage = function (evt) {
  82. if(evt.data == "ok") {
  83. sendable = true;
  84. } else {
  85. sendable = false;
  86. }
  87. };
  88.  
  89.  
  90. var isPaint;
  91.  
  92. function pendown(e) {
  93. if(isIpodSafari()) {
  94. e.preventDefault();
  95. e = e.targetTouches[0];
  96. }
  97. isPaint = true;
  98. addPoint(e.pageX, e.pageY, false);
  99. redraw();
  100. }
  101. document.getElementById("sketch_canvas").addEventListener("mousedown", pendown, false);
  102. document.getElementById("sketch_canvas").addEventListener("touchstart", pendown, false);
  103.  
  104. function penmove(e) {
  105. if(isIpodSafari()) {
  106. e.preventDefault();
  107. e = e.targetTouches[0];
  108. }
  109. if(isPaint){
  110. addPoint(e.pageX, e.pageY, true);
  111. redraw();
  112. }
  113. }
  114. document.getElementById("sketch_canvas").addEventListener("mousemove", penmove, false);
  115. document.getElementById("sketch_canvas").addEventListener("touchmove", penmove, false);
  116.  
  117. function penup(e) {
  118. isPaint = false;
  119. }
  120. document.getElementById("sketch_canvas").addEventListener("mouseup", penup, false);
  121. document.getElementById("sketch_canvas").addEventListener("touchend", penup, false);
  122.  
  123. </script>
  124. </body>
  125. </html>
Add Comment
Please, Sign In to add comment