Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.42 KB | None | 0 0
  1.  
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <!--
  6. This web page does the minimal setup for using mouse events along
  7. with 2D canvas graphics.
  8. -->
  9. <head>
  10. <meta charset="UTF-8">
  11. <title>CS424, Lab 2, Exercise 2</title>
  12. <style>
  13. /* This style section is here to make the canvas more obvious on the
  14. page. It is white on a light gray page background, with a thin
  15. black border. Also, turn off text selection to avoid having
  16. selection interfere with mouse action. */
  17. body {
  18. background-color: #DDDDDD;
  19. -webkit-user-select: none; /* turn off text selection / Webkit */
  20. -moz-user-select: none; /* Firefox */
  21. -ms-user-select: none; /* IE 10 */
  22. -o-user-select: none; /* Opera */
  23. user-select: none;
  24. }
  25. canvas {
  26. background-color: white;
  27. display: block;
  28. }
  29. #canvasholder {
  30. border:2px solid black;
  31. float: left; /* This makes the border exactly fit the canvas. */
  32. }
  33. </style>
  34. <script>
  35.  
  36. "use strict"; // gives improved error-checking in scripts.
  37.  
  38. var canvas; // The canvas element on which we will draw.
  39. var graphics; // A 2D graphics context for drawing on the canvas.
  40.  
  41. /**
  42. * This function returns a string representing a random RGB color.
  43. * The returned string can be assigned as the value of graphics.fillStyle
  44. * or graphics.strokeStyle.
  45. */
  46. function randomColorString() {
  47. var r = Math.floor(256*Math.random());
  48. var g = Math.floor(256*Math.random());
  49. var b = Math.floor(256*Math.random());
  50. return "rgb(" + r + "," + g + "," + b + ")";
  51. }
  52.  
  53. /**
  54. * This function is called in init() to set up mouse event handling
  55. * on the canvas. You can modify the nested functions doMouseDown,
  56. * doMouseDrag, and possibly doMouseUp to change the reponse to
  57. * mouse events. As an example, this program does some simple drawing.
  58. */
  59. function installMouseHandler() {
  60.  
  61. var dragging = false; // set to true when a drag action is in progress.
  62. var startX, startY; // coordinates of mouse at start of drag.
  63. var prevX, prevY; // previous mouse position during a drag.
  64.  
  65. var colorChoice; // Integer code for the selected color in the "colorChoide"
  66. // popup menu. The value is assigned in doMouseDown.
  67. var figureChoice;
  68.  
  69. function doMouseDown(evt) {
  70. // This function is called when the user presses a button on the mouse.
  71. // Only the main mouse button will start a drag.
  72. if (dragging) {
  73. return; // if a drag is in progress, don't start another.
  74. }
  75. if (evt.button != 0) {
  76. return; // don't respond unless the button is the main (left) mouse button.
  77. }
  78. var x,y; // mouse position in canvas coordinates
  79. var r = canvas.getBoundingClientRect();
  80. x = Math.round(evt.clientX - r.left); // translate mouse position from screen coords to canvas coords.
  81. y = Math.round(evt.clientY - r.top); // round to integer values; some browsers would give non-integers.
  82. dragging = true; // (this won't be the case for all mousedowns in all programs)
  83. if (dragging) {
  84. startX = prevX = x;
  85. startY = prevY = y;
  86. document.addEventListener("mousemove", doMouseMove, false);
  87. document.addEventListener("mouseup", doMouseUp, false);
  88. }
  89.  
  90.  
  91. figureChoice = Number(document.getElementById("figureChoice").value);
  92. colorChoice = Number(document.getElementById("colorChoice").value);
  93.  
  94. // TODO: Anything else to do when mouse is first pressed?
  95. }
  96.  
  97. function doMouseMove(evt) {
  98. // This function is called when the user moves the mouse during a drag.
  99. if (!dragging) {
  100. return; // (shouldn't be possible)
  101. }
  102. var x,y; // mouse position in canvas coordinates
  103. var r = canvas.getBoundingClientRect();
  104. x = Math.round(evt.clientX - r.left);
  105. y = Math.round(evt.clientY - r.top);
  106.  
  107. /*------------------------------------------------------------*/
  108. /* TODO: Add support for more drawing tools. */
  109.  
  110. if ( Math.abs(x-prevX) + Math.abs(y-prevY) < 3 ) {
  111. return; // don't draw squares too close together
  112. }
  113.  
  114. if (colorChoice == 0) {
  115. graphics.fillStyle = randomColorString();
  116. }
  117. else if (colorChoice == 1) {
  118. graphics.fillStyle = "red";
  119. }
  120. else if (colorChoice == 2) {
  121. graphics.fillStyle = "green";
  122. }
  123. else if (colorChoice == 3) {
  124. graphics.fillStyle = "blue";
  125. }
  126. else if (colorChoice == 4) {
  127. graphics.fillStyle = "yellow";
  128. }
  129.  
  130. if (figureChoice == 0) {
  131. graphics.fillRect(x-20,y-20,40,40); //Podstawowy kwadrat
  132. graphics.strokeRect(x-20,y-20,40,40);
  133. }
  134. else if (figureChoice == 1)
  135. {
  136. /*<polygon points="
  137. x+19.498,y+4.450,
  138. x+15.636,y+12.469,
  139. x+8.677,y+18.0193,
  140. x+1.224,y+20,
  141. x-8.677,y+18.0193,
  142. x-15.636,y+12.469,
  143. x-19.498,y+4.450,
  144. x-19.498,y-4.450,
  145. x-15.636,y-12.4697,
  146. x-8.677,y-18.0193,
  147. x+1.408,y-20,
  148. x+8.677,y-18.019,
  149. x+15.636,y-12.469,
  150. x+19.498,y-4.450" />*/
  151. graphics.strokePoly(x+19.498,y+4.450,
  152. x+15.636,y+12.469,
  153. x+8.677,y+18.0193,
  154. x+1.224,y+20,
  155. x-8.677,y+18.0193,
  156. x-15.636,y+12.469,
  157. x-19.498,y+4.450,
  158. x-19.498,y-4.450,
  159. x-15.636,y-12.4697,
  160. x-8.677,y-18.0193,
  161. x+1.408,y-20,
  162. x+8.677,y-18.019,
  163. x+15.636,y-12.469,
  164. x+19.498,y-4.450); //14-kat
  165. graphics.fillPoly(x+19.498,y+4.450,
  166. x+15.636,y+12.469,
  167. x+8.677,y+18.0193,
  168. x+1.224,y+20,
  169. x-8.677,y+18.0193,
  170. x-15.636,y+12.469,
  171. x-19.498,y+4.450,
  172. x-19.498,y-4.450,
  173. x-15.636,y-12.4697,
  174. x-8.677,y-18.0193,
  175. x+1.408,y-20,
  176. x+8.677,y-18.019,
  177. x+15.636,y-12.469,
  178. x+19.498,y-4.450);
  179. }
  180.  
  181. /*------------------------------------------------------------*/
  182.  
  183. prevX = x; // update prevX,prevY to prepare for next call to doMouseMove
  184. prevY = y;
  185. }
  186.  
  187.  
  188. function doMouseUp(evt) {
  189. // This function is called when the user releases a mouse button during a drag.
  190. if (!dragging) {
  191. return; // (shouldn't be possible)
  192. }
  193. dragging = false;
  194. document.removeEventListener("mousemove", doMouseMove, false);
  195. document.removeEventListener("mouseup", doMouseMove, false);
  196. }
  197.  
  198. canvas.addEventListener("mousedown", doMouseDown, false);
  199.  
  200. } // end installMouseHandler
  201.  
  202.  
  203. /**
  204. * This function can be called to add a collection of extra drawing function to
  205. * a graphics context, to make it easier to draw basic shapes with that context.
  206. * The parameter, graphics, must be a canvas 2d graphics context.
  207. *
  208. * The following new functions are added to the graphics context:
  209. *
  210. * graphics.strokeLine(x1,y1,x2,y2) -- stroke the line from (x1,y1) to (x2,y2).
  211. * graphics.fillCircle(x,y,r) -- fill the circle with center (x,y) and radius r.
  212. * graphics.strokeCircle(x,y,r) -- stroke the circle.
  213. * graphics.fillOval(x,y,r1,r2) -- fill oval with center (x,y) and radii r1 and r2.
  214. * graphics.stokeOval(x,y,r1,r2) -- stroke the oval
  215. * graphics.fillPoly(x1,y1,x2,y2,...) -- fill polygon with vertices (x1,y1), (x2,y2), ...
  216. * graphics.strokePoly(x1,y1,x2,y2,...) -- stroke the polygon.
  217. * graphics.getRGB(x,y) -- returns the color components of pixel at (x,y) as an array of
  218. * four integers in the range 0 to 255, in the order red, green, blue, alpha.
  219. *
  220. * (Note that "this" in a function that is called as a member of an object refers to that
  221. * object. Here, this will refer to the graphics context.)
  222. */
  223. function addGraphicsContextExtras(graphics) {
  224. graphics.strokeLine = function(x1,y1,x2,y2) {
  225. this.beginPath();
  226. this.moveTo(x1,y1);
  227. this.lineTo(x2,y2);
  228. this.stroke();
  229. }
  230. graphics.fillCircle = function(x,y,r) {
  231. this.beginPath();
  232. this.arc(x,y,r,0,2*Math.PI,false);
  233. this.fill();
  234. }
  235. graphics.strokeCircle = function(x,y,radius) {
  236. this.beginPath();
  237. this.arc(x,y,radius,0,2*Math.PI,false);
  238. this.stroke();
  239. }
  240. graphics.fillPoly = function() {
  241. if (arguments.length < 5)
  242. return;
  243. this.beginPath();
  244. this.moveTo(arguments[0],arguments[1]);
  245. for (var i = 2; i+1 < arguments.length; i = i + 2) {
  246. this.lineTo(arguments[i],arguments[i+1]);
  247. }
  248. this.closePath();
  249. this.fill();
  250. }
  251. graphics.strokePoly = function() {
  252. if (arguments.length < 5)
  253. return;
  254. this.beginPath();
  255. this.moveTo(arguments[0],arguments[1]);
  256. for (var i = 2; i+1 < arguments.length; i = i + 2) {
  257. this.lineTo(arguments[i],arguments[i+1]);
  258. }
  259. this.closePath();
  260. this.stroke();
  261. }
  262. graphics.fillOval = function(x,y,horizontalRadius,verticalRadius) {
  263. this.save();
  264. this.translate(x,y);
  265. this.scale(horizontalRadius,verticalRadius);
  266. this.beginPath();
  267. this.arc(0,0,1,0,2*Math.PI,false);
  268. this.restore();
  269. this.fill();
  270. }
  271. graphics.strokeOval = function(x,y,horizontalRadius,verticalRadius) {
  272. this.save();
  273. this.translate(x,y);
  274. this.scale(horizontalRadius,verticalRadius);
  275. this.beginPath();
  276. this.arc(0,0,1,0,2*Math.PI,false);
  277. this.restore();
  278. this.stroke();
  279. }
  280. graphics.getRGB = function(x,y) {
  281. var color = this.getImageData(x,y,1,1);
  282. return color.data;
  283. }
  284. } // end of addGraphicsContextExtras()
  285.  
  286.  
  287. /**
  288. * The init() function is called after the page has been
  289. * loaded. It initializes the canvas and graphics variables,
  290. * and it installs mouse and key listeners. If an error
  291. * occurs, a message is displayed in place of the canvas.
  292. */
  293. function init() {
  294. try {
  295. canvas = document.getElementById("canvas");
  296. graphics = canvas.getContext("2d");
  297. } catch(e) {
  298. document.getElementById("canvasholder").innerHTML =
  299. "<p>Canvas graphics is not supported.<br>" +
  300. "An error occurred while initializing graphics.</p>";
  301. return;
  302. }
  303. addGraphicsContextExtras(graphics);
  304. installMouseHandler();
  305. graphics.fillStyle = "white";
  306. graphics.fillRect(0,0,canvas.width,canvas.height);
  307. }
  308.  
  309. function Clear(){
  310. graphics.clearRect(0,0,canvas.width,canvas.height);
  311. }
  312.  
  313. </script>
  314. </head>
  315. <body onload="init()"> <!-- the onload attribute here is what calls the init() function -->
  316.  
  317. <h2>Lab 2, Exercise 2: Mousing</h2>
  318.  
  319. <noscript>
  320. <!-- This message will be shown in the page if JavaScript is not available. -->
  321. <p>JavaScript is required to use this page.</p>
  322. </noscript>
  323. <p><b>Figura:</b>
  324. <select id="figureChoice">
  325. <option value="0">Square</option>
  326. <option value="1">14-kat</option>
  327. </select>
  328. <p><b>Color:</b>
  329. <select id="colorChoice">
  330. <option value="0">Random</option>
  331. <option value="1">Red</option>
  332. <option value="2">Green</option>
  333. <option value="3">Blue</option>
  334. <option value="4">Yellow</option>
  335. </select>
  336. <button onclick="Clear()">Wyczyść</button>
  337. </p>
  338.  
  339. <div id="canvasholder">
  340. <canvas id="canvas" width="800" height="600">
  341. <!-- This message is shown on the page if the browser doesn't support the canvas element. -->
  342. Canvas not supported.
  343. </canvas>
  344. </div>
  345.  
  346. </body>
  347. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement