Advertisement
DannySherman

Untitled

Oct 10th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.98 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <title>HTML5 Canvas &mdash; Basic Geometry</title>
  5. <meta http-equiv="content-type" content="text/html; charset=UTF8">
  6.  
  7. <script type="text/javascript" src="dat.gui.js"></script>
  8. <script type="text/javascript" src="gl-matrix.js"></script>
  9.  
  10. <style>
  11. canvas {border: solid 1px black;}
  12. .matrixTable {
  13. display: inline-block;
  14. text-align:center;
  15. vertical-align: middle;
  16. border-left: solid 1pt;
  17. border-right: solid 1pt;
  18. }
  19. .matrixTable td {padding: 2px 10px;}
  20. </style>
  21. </head>
  22.  
  23. <script type="text/javascript">
  24. var ctx1, ctx2;
  25. var dragging = false;
  26. var params = {
  27. 'x0': 150,
  28. 'y0': 20,
  29. 'scale' : 1.0,
  30. 'angle': 0,
  31. 'showImage': true
  32. }
  33.  
  34. /* ### GUI ### */
  35. var gui;
  36. function initGUI() {
  37. gui = new dat.GUI({ autoPlace: true });
  38.  
  39. gui.add(params, 'x0').min(0).max(w2).listen().onChange(drawAll);
  40. gui.add(params, 'y0').min(0).max(h2).listen().onChange(drawAll);
  41. gui.add(params, 'scale').min(0.01).max(2.5).step(0.01).listen().onChange(drawAll);
  42. gui.add(params, 'angle').min(-180).max(180).listen().onChange(drawAll);
  43. gui.add(params, 'showImage').onChange(drawAll);
  44.  
  45. //var parent = document.getElementById('controls');
  46. //parent.appendChild(gui.domElement);
  47. }
  48.  
  49. /* ### Mouse ### */
  50. var dragRedPin = 0
  51. function initMouse() {
  52. // To be called in start() after defininf canvas1 and canvas2
  53. canvas1.addEventListener("mousedown", onMouseDown, false);
  54. canvas2.addEventListener("mousedown", onMouseDown, false);
  55. canvas1.addEventListener("mousemove", onMouseMove, false);
  56. canvas2.addEventListener("mousemove", onMouseMove, false);
  57. canvas1.addEventListener("mouseup", onMouseUp, false);
  58. canvas2.addEventListener("mouseup", onMouseUp, false);
  59. //canvas1.addEventListener('mousedown',engage);
  60. //canvas1.addEventListener('mousemove', putPoint);
  61. //canvas1.addEventListener('mouseup',disengage);
  62. }
  63. var dragging = false;
  64.  
  65.  
  66. function onMouseDown(event) {
  67. var rect = event.target.getBoundingClientRect()
  68. xpix = event.clientX - rect.left
  69. ypix = event.clientY - rect.top
  70.  
  71. // Uncomment for debugging
  72. console.log("Mouse Down", event)
  73. console.log("Pixel coordinates", xpix,ypix)
  74.  
  75. if (event.target == canvas1) {
  76.  
  77. // TODO: Q1 - Add functionality to draw polylines instead of just single squares
  78.  
  79. ctx1.strokeStyle = '#ff8000'; // Orange
  80. ctx1.lineWidth = 4;
  81. ctx1.setTransform(1, 0, 0, 1, 0, 0)
  82. drawSquare(ctx1, xpix,ypix, 4)
  83. dragging = true;
  84. onMouseMove();
  85. /*ctx1.lineTo(event.clientX, event.clientY);
  86. ctx1.stroke();
  87. ctx1.beginPath();
  88. ctx1.moveTo(event.clientX, event.clientY);*/
  89.  
  90. drawAll()
  91. } else if (event.target == canvas2) {
  92. // If close to the red pin
  93. if ((Math.abs(xpix-params.x0)<10) && (Math.abs(ypix-params.y0)<10)) {
  94. dragRedPin=1 // Set drag state to 1
  95. // Not really required to change (x0,y0) if this is done in mouseDrag
  96. // Uncomment for debugging
  97. //params.x0 = xpix; params.y0 = ypix;
  98. //drawAll()
  99. }
  100. }
  101. }
  102. function onMouseMove(event) {
  103. // Dispatch move event to drag if we are currently dragging the red pin
  104. if ((dragRedPin>0)&&(event.target == canvas2)) return onDragRedPin(event)
  105. // Ignore else
  106.  
  107. var rect = event.target.getBoundingClientRect()
  108. xpix = event.clientX - rect.left
  109. ypix = event.clientY - rect.top
  110. if(dragging ==true){
  111. ctx1.lineTo(xpix, ypix);
  112. ctx1.stroke();
  113. ctx1.beginPath();
  114. ctx1.moveTo(xpix, ypix);
  115. }
  116. }
  117. function onMouseUp(event) {
  118. // Stop dragging
  119. dragRedPin = 0;
  120. dragging = false;
  121. }
  122. function onDragRedPin(event) {
  123. var rect = event.target.getBoundingClientRect()
  124. xpix = event.clientX - rect.left
  125. ypix = event.clientY - rect.top
  126.  
  127. // Assume (event.target == canvas2)
  128. params.x0 = xpix;
  129. params.y0 = ypix;
  130. drawAll()
  131. }
  132.  
  133. /* ### Drawing ### */
  134. function drawSquare(ctx, x,y, w) {
  135. ctx.beginPath();
  136. ctx.moveTo(x-w/2, y-w/2);
  137. ctx.lineTo(x+w/2, y-w/2);
  138. ctx.lineTo(x+w/2, y+w/2);
  139. ctx.lineTo(x-w/2, y+w/2);
  140. ctx.lineTo(x-w/2, y-w/2);
  141. ctx.stroke();
  142. }
  143.  
  144.  
  145. function drawShape(ctx, pts) {
  146.  
  147. // TODO: Q2 - Draw shape corresponding to pts
  148. //ctx.setTransform(pts[0],pts[1], pts[2], pts[3], pts[4], pts[5]);
  149.  
  150. ctx.beginPath()
  151. ctx.moveTo(0, 0)
  152. ctx.lineTo(pts[0], pts[1])
  153. ctx.lineTo(pts[2], pts[3])
  154. ctx.lineTo(pts[4], pts[5])
  155. ctx.lineTo(pts[6], pts[7])
  156. ctx.closePath();
  157. ctx.stroke();
  158. ctx.closePath();
  159. console.log("pts from ds")
  160. console.log(pts)
  161. }
  162. function transformPoints(M, pts) { //pts is an array containing 4 x's and 4 y's
  163. var pts2=[];
  164.  
  165. // TODO: Q3 - Transform shapes using affine transform
  166.  
  167. pts2[0]= pts[0]*M[0];
  168. pts2[1]= pts[1]*M[1];
  169. pts2[2]= pts[2]*M[2];
  170. pts2[3]= pts[3]*M[3];
  171. pts2[4]= pts[4]*M[4];
  172. pts2[5]= pts[5]*M[5];
  173. pts2[6]= pts[6]*M[6];
  174. pts2[7]= pts[7]*M[7];
  175.  
  176. return pts2
  177. }
  178. // Auxiliary: display mat3 matrix as HTML
  179. mat3.toHTML = function(m) {
  180. var str = '<table class="matrixTable"><tbody>'
  181.  
  182. str += '<tr><td>'+m[0].toFixed(2)+'</td><td>'+m[3].toFixed(2)+'</td><td>'+m[6].toFixed(2)+'</td></tr>';
  183. str += '<tr><td>'+m[1].toFixed(2)+'</td><td>'+m[4].toFixed(2)+'</td><td>'+m[7].toFixed(2)+'</td></tr>';
  184. str += '<tr><td>'+m[2].toFixed(2)+'</td><td>'+m[5].toFixed(2)+'</td><td>'+m[8].toFixed(2)+'</td></tr>';
  185.  
  186. str += '</tbody></table>'
  187.  
  188. return str
  189. }
  190.  
  191. function drawAll() {
  192. // Reset canvases
  193. ctx1.setTransform(1, 0, 0, 1, 0, 0) // Reset transform
  194. ctx2.setTransform(1, 0, 0, 1, 0, 0)
  195. //ctx1.clearRect(0,0,w1,h1) // Clear canvas
  196. ctx2.clearRect(0,0,w2,h2)
  197.  
  198. // Draw red pin on canvas 1
  199. ctx1.strokeStyle = '#ff0000';
  200. ctx1.lineWidth = 3;
  201. drawSquare(ctx1, w1/2,0, 10)
  202.  
  203. // Draw red pin directly to canvas 2
  204. ctx2.strokeStyle = '#ff0000';
  205. ctx2.lineWidth = 3;
  206. drawSquare(ctx2, params.x0,params.y0, 10)
  207.  
  208.  
  209. // TODO: Q2 - Draw bounding box of canvas1 using drawShape()
  210. //var pts = [1,0.5, -0.5,1, 30,10,];
  211. var pts = [0,0, w1,0, w1,h1, 0,h1]; //change to size of canvas!!!!!!
  212. ctx1.strokeStyle = 'blue'
  213. ctx1.lineWidth = 3
  214. drawShape(ctx1, pts);
  215.  
  216. var M = mat3.create()
  217.  
  218.  
  219. // TODO: Q3 - define the content of M
  220. /*mat3.identity(M)
  221. mat3.translate(M, M, [params.x0, params.y0])*/
  222. mat3.identity(M)
  223. mat3.translate(M, M, [w1/2, 0])
  224. mat3.rotate(M, M, params.angle)
  225. mat3.scale(M, M, [params.scale, params.scale])
  226. var pts2 = transformPoints(M, pts)
  227. matElem = document.getElementById('mat')
  228. matElem.innerHTML = 'M = '+mat3.toHTML(M)
  229. console.log(M)
  230. console.log("This is pts2");
  231. console.log(pts2)
  232. console.log(pts)
  233. // TODO: Q3 - Draw bounding box of canvas1 transformed to canvas2
  234. ctx2.strokeStyle = 'blue'
  235. ctx2.lineWidth = 3
  236. drawShape(ctx2, pts2)
  237. // TODO: Q5 - Copy canvas1 transformed into canvas2
  238.  
  239. }
  240.  
  241.  
  242. /* ### Initialization ### */
  243. function start() {
  244. canvas1 = document.getElementById('canvas1');
  245. canvas2 = document.getElementById('canvas2');
  246. ctx1 = canvas1.getContext('2d')
  247. ctx2 = canvas2.getContext('2d')
  248.  
  249. w1 = canvas1.width;
  250. h1 = canvas1.height;
  251. w2 = canvas2.width;
  252. h2 = canvas2.height;
  253.  
  254. initGUI()
  255. initMouse()
  256.  
  257. drawAll()
  258. // Note: no animation here: drawAll is called only when it needs refreshing
  259.  
  260. }
  261. </script>
  262.  
  263. <body onload="start();">
  264. <h2>Mouse interaction and geometry</h2>
  265.  
  266. <div style="display:inline-block">
  267. Original <br/>
  268. <canvas id="canvas1" width="200" height="200"></canvas>
  269. </div>
  270.  
  271. <div style="display:inline-block">
  272. Transformed <br/>
  273. <canvas id="canvas2" width="300" height="300"></canvas>
  274. </div>
  275.  
  276. <div style="display:inline-block">
  277. <!-- GUI <br/> -->
  278. <span id="controls"></span>
  279. </div>
  280.  
  281. <div id='mat'></div>
  282.  
  283. </body>
  284. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement