Advertisement
Guest User

Untitled

a guest
Aug 1st, 2013
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. /* © 2009 ROBO Design
  2. * http://www.robodesign.ro
  3. */
  4.  
  5. // Keep everything in anonymous function, called on window load.
  6. if(window.addEventListener) {
  7. window.addEventListener('load', function () {
  8. var canvas, context, canvaso, contexto;
  9.  
  10. // The active tool instance.
  11. var tool;
  12. var tool_default = 'line';
  13.  
  14. function init () {
  15. // Find the canvas element.
  16. canvaso = document.getElementById('imageView');
  17. if (!canvaso) {
  18. alert('Error: I cannot find the canvas element!');
  19. return;
  20. }
  21.  
  22. if (!canvaso.getContext) {
  23. alert('Error: no canvas.getContext!');
  24. return;
  25. }
  26.  
  27. // Get the 2D canvas context.
  28. contexto = canvaso.getContext('2d');
  29. if (!contexto) {
  30. alert('Error: failed to getContext!');
  31. return;
  32. }
  33.  
  34. // Add the temporary canvas.
  35. var container = canvaso.parentNode;
  36. canvas = document.createElement('canvas');
  37. if (!canvas) {
  38. alert('Error: I cannot create a new canvas element!');
  39. return;
  40. }
  41.  
  42. canvas.id = 'imageTemp';
  43. canvas.width = canvaso.width;
  44. canvas.height = canvaso.height;
  45. container.appendChild(canvas);
  46.  
  47. context = canvas.getContext('2d');
  48.  
  49. // Get the tool select input.
  50. var tool_select = document.getElementById('dtool');
  51. if (!tool_select) {
  52. alert('Error: failed to get the dtool element!');
  53. return;
  54. }
  55. tool_select.addEventListener('change', ev_tool_change, false);
  56.  
  57. // Activate the default tool.
  58. if (tools[tool_default]) {
  59. tool = new tools[tool_default]();
  60. tool_select.value = tool_default;
  61. }
  62.  
  63. // Attach the mousedown, mousemove and mouseup event listeners.
  64. canvas.addEventListener('mousedown', ev_canvas, false);
  65. canvas.addEventListener('mousemove', ev_canvas, false);
  66. canvas.addEventListener('mouseup', ev_canvas, false);
  67. }
  68.  
  69. // The general-purpose event handler. This function just determines the mouse
  70. // position relative to the canvas element.
  71. function ev_canvas (ev) {
  72. if (ev.layerX || ev.layerX == 0) { // Firefox
  73. ev._x = ev.layerX;
  74. ev._y = ev.layerY;
  75. } else if (ev.offsetX || ev.offsetX == 0) { // Opera
  76. ev._x = ev.offsetX;
  77. ev._y = ev.offsetY;
  78. }
  79.  
  80. // Call the event handler of the tool.
  81. var func = tool[ev.type];
  82. if (func) {
  83. func(ev);
  84. }
  85. }
  86.  
  87. // The event handler for any changes made to the tool selector.
  88. function ev_tool_change (ev) {
  89. if (tools[this.value]) {
  90. tool = new tools[this.value]();
  91. }
  92. }
  93.  
  94. // This function draws the #imageTemp canvas on top of #imageView, after which
  95. // #imageTemp is cleared. This function is called each time when the user
  96. // completes a drawing operation.
  97. function img_update () {
  98. contexto.drawImage(canvas, 0, 0);
  99. context.clearRect(0, 0, canvas.width, canvas.height);
  100. }
  101.  
  102. // This object holds the implementation of each drawing tool.
  103. var tools = {};
  104.  
  105. // The drawing pencil.
  106. tools.pencil = function () {
  107. var tool = this;
  108. this.started = false;
  109.  
  110. // This is called when you start holding down the mouse button.
  111. // This starts the pencil drawing.
  112. this.mousedown = function (ev) {
  113. context.beginPath();
  114. context.moveTo(ev._x, ev._y);
  115. tool.started = true;
  116. };
  117.  
  118. // This function is called every time you move the mouse. Obviously, it only
  119. // draws if the tool.started state is set to true (when you are holding down
  120. // the mouse button).
  121. this.mousemove = function (ev) {
  122. if (tool.started) {
  123. context.lineTo(ev._x, ev._y);
  124. context.stroke();
  125. }
  126. };
  127.  
  128. // This is called when you release the mouse button.
  129. this.mouseup = function (ev) {
  130. if (tool.started) {
  131. tool.mousemove(ev);
  132. tool.started = false;
  133. img_update();
  134. }
  135. };
  136. };
  137.  
  138. // The rectangle tool.
  139. tools.rect = function () {
  140. var tool = this;
  141. this.started = false;
  142.  
  143. this.mousedown = function (ev) {
  144. tool.started = true;
  145. tool.x0 = ev._x;
  146. tool.y0 = ev._y;
  147. };
  148.  
  149. this.mousemove = function (ev) {
  150. if (!tool.started) {
  151. return;
  152. }
  153.  
  154. var x = Math.min(ev._x, tool.x0),
  155. y = Math.min(ev._y, tool.y0),
  156. w = Math.abs(ev._x - tool.x0),
  157. h = Math.abs(ev._y - tool.y0);
  158.  
  159. context.clearRect(0, 0, canvas.width, canvas.height);
  160.  
  161. if (!w || !h) {
  162. return;
  163. }
  164.  
  165. context.strokeRect(x, y, w, h);
  166. };
  167.  
  168. this.mouseup = function (ev) {
  169. if (tool.started) {
  170. tool.mousemove(ev);
  171. tool.started = false;
  172. img_update();
  173. }
  174. };
  175. };
  176.  
  177. // The line tool.
  178. tools.line = function () {
  179. var tool = this;
  180. this.started = false;
  181.  
  182. this.mousedown = function (ev) {
  183. tool.started = true;
  184. tool.x0 = ev._x;
  185. tool.y0 = ev._y;
  186. };
  187.  
  188. this.mousemove = function (ev) {
  189. if (!tool.started) {
  190. return;
  191. }
  192.  
  193. context.clearRect(0, 0, canvas.width, canvas.height);
  194.  
  195. context.beginPath();
  196. context.moveTo(tool.x0, tool.y0);
  197. context.lineTo(ev._x, ev._y);
  198. context.stroke();
  199. context.closePath();
  200. };
  201.  
  202. this.mouseup = function (ev) {
  203. if (tool.started) {
  204. tool.mousemove(ev);
  205. tool.started = false;
  206. img_update();
  207. }
  208. };
  209. };
  210.  
  211. init();
  212.  
  213. }, false); }
  214.  
  215. // vim:set spell spl=en fo=wan1croql tw=80 ts=2 sw=2 sts=2 sta et ai cin fenc=utf-8 ff=unix:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement