PageRenderTime 17ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/tools/eraser.js

https://code.google.com/p/paintweb/
JavaScript | 242 lines | 100 code | 38 blank | 104 comment | 17 complexity | b05a33a399f3262a75b5800f4b359bc5 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0
  1. /*
  2. * Copyright (c) 2009-2014, Mihai Sucan
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of the copyright holder nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * $URL: http://code.google.com/p/paintweb $
  31. * $Date: 2014-01-28 13:04:28 $
  32. */
  33. /**
  34. * @author <a lang="ro" href="http://www.robodesign.ro/mihai">Mihai ─╣┬×ucan</a>
  35. * @fileOverview Holds the eraser tool implementation.
  36. */
  37. /**
  38. * @class The eraser tool.
  39. *
  40. * @param {PaintWeb} app Reference to the main paint application object.
  41. */
  42. pwlib.tools.eraser = function (app) {
  43. var _self = this,
  44. bufferContext = app.buffer.context,
  45. clearInterval = app.win.clearInterval,
  46. config = app.config,
  47. history = app.history.pos,
  48. image = app.image,
  49. layerContext = app.layer.context,
  50. mouse = app.mouse,
  51. setInterval = app.win.setInterval;
  52. /**
  53. * The interval ID used for running the erasing operation every few
  54. * milliseconds.
  55. *
  56. * @private
  57. * @see PaintWeb.config.toolDrawDelay
  58. */
  59. var timer = null;
  60. /**
  61. * Holds the points needed to be drawn. Each point is added by the
  62. * <code>mousemove</code> event handler.
  63. *
  64. * @private
  65. * @type Array
  66. */
  67. var points = [];
  68. /**
  69. * Holds the starting point on the <var>x</var> axis of the image, for the
  70. * current drawing operation.
  71. *
  72. * @private
  73. * @type Number
  74. */
  75. var x0 = 0;
  76. /**
  77. * Holds the starting point on the <var>y</var> axis of the image, for the
  78. * current drawing operation.
  79. *
  80. * @private
  81. * @type Number
  82. */
  83. var y0 = 0;
  84. var globalOp_ = null,
  85. lineWidth_ = null;
  86. /**
  87. * The tool deactivation event handler. This function clears timers, clears
  88. * the canvas and allows shadows to be rendered again.
  89. */
  90. this.deactivate = function () {
  91. if (timer) {
  92. clearInterval(timer);
  93. timer = null;
  94. }
  95. if (mouse.buttonDown) {
  96. if (globalOp_) {
  97. layerContext.globalCompositeOperation = globalOp_;
  98. }
  99. if (lineWidth_) {
  100. layerContext.lineWidth = lineWidth_;
  101. }
  102. app.historyGoto(history.pos);
  103. }
  104. points = [];
  105. // Allow Canvas shadows.
  106. app.shadowAllow();
  107. };
  108. /**
  109. * The tool activation event handler. This is run after the tool construction
  110. * and after the deactivation of the previous tool. This function simply
  111. * disallows the rendering of shadows.
  112. */
  113. this.activate = function () {
  114. // Do not allow Canvas shadows.
  115. app.shadowDisallow();
  116. };
  117. /**
  118. * Initialize the drawing operation.
  119. */
  120. this.mousedown = function () {
  121. globalOp_ = layerContext.globalCompositeOperation;
  122. lineWidth_ = layerContext.lineWidth;
  123. layerContext.globalCompositeOperation = 'destination-out';
  124. layerContext.lineWidth = bufferContext.lineWidth;
  125. x0 = mouse.x;
  126. y0 = mouse.y;
  127. points = [];
  128. if (!timer) {
  129. timer = setInterval(_self.draw, config.toolDrawDelay);
  130. }
  131. return true;
  132. };
  133. /**
  134. * Save the mouse coordinates in the array.
  135. */
  136. this.mousemove = function () {
  137. if (mouse.buttonDown) {
  138. points.push(mouse.x, mouse.y);
  139. }
  140. };
  141. /**
  142. * Draw the points in the stack. This function is called every few
  143. * milliseconds.
  144. *
  145. * @see PaintWeb.config.toolDrawDelay
  146. */
  147. this.draw = function () {
  148. var i = 0, n = points.length;
  149. if (!n) {
  150. return;
  151. }
  152. layerContext.beginPath();
  153. layerContext.moveTo(x0, y0);
  154. while (i < n) {
  155. x0 = points[i++];
  156. y0 = points[i++];
  157. layerContext.lineTo(x0, y0);
  158. }
  159. layerContext.stroke();
  160. layerContext.closePath();
  161. points = [];
  162. };
  163. /**
  164. * End the drawing operation, once the user releases the mouse button.
  165. */
  166. this.mouseup = function () {
  167. if (mouse.x == x0 && mouse.y == y0) {
  168. points.push(x0+1, y0+1);
  169. }
  170. if (timer) {
  171. clearInterval(timer);
  172. timer = null;
  173. }
  174. _self.draw();
  175. layerContext.globalCompositeOperation = globalOp_;
  176. layerContext.lineWidth = lineWidth_;
  177. app.historyAdd();
  178. return true;
  179. };
  180. /**
  181. * Allows the user to press <kbd>Escape</kbd> to cancel the drawing operation.
  182. *
  183. * @param {Event} ev The DOM Event object.
  184. *
  185. * @returns {Boolean} True if the drawing operation was cancelled, or false if
  186. * not.
  187. */
  188. this.keydown = function (ev) {
  189. if (!mouse.buttonDown || ev.kid_ != 'Escape') {
  190. return false;
  191. }
  192. if (timer) {
  193. clearInterval(timer);
  194. timer = null;
  195. }
  196. layerContext.globalCompositeOperation = globalOp_;
  197. layerContext.lineWidth = lineWidth_;
  198. mouse.buttonDown = false;
  199. points = [];
  200. app.historyGoto(history.pos);
  201. return true;
  202. };
  203. };
  204. // vim:set spell spl=en fo=wan1croqlt tw=80 ts=2 sw=2 sts=2 sta et ai cin fenc=utf-8 ff=unix: