PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms 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
  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/**
 35 * @author <a lang="ro" href="http://www.robodesign.ro/mihai">Mihai ─╣┬×ucan</a>
 36 * @fileOverview Holds the eraser tool implementation.
 37 */
 38
 39/**
 40 * @class The eraser tool.
 41 *
 42 * @param {PaintWeb} app Reference to the main paint application object.
 43 */
 44pwlib.tools.eraser = function (app) {
 45  var _self         = this,
 46      bufferContext = app.buffer.context,
 47      clearInterval = app.win.clearInterval,
 48      config        = app.config,
 49      history       = app.history.pos,
 50      image         = app.image,
 51      layerContext  = app.layer.context,
 52      mouse         = app.mouse,
 53      setInterval   = app.win.setInterval;
 54
 55  /**
 56   * The interval ID used for running the erasing operation every few 
 57   * milliseconds.
 58   *
 59   * @private
 60   * @see PaintWeb.config.toolDrawDelay
 61   */
 62  var timer = null;
 63
 64  /**
 65   * Holds the points needed to be drawn. Each point is added by the 
 66   * <code>mousemove</code> event handler.
 67   *
 68   * @private
 69   * @type Array
 70   */
 71  var points = [];
 72
 73  /**
 74   * Holds the starting point on the <var>x</var> axis of the image, for the 
 75   * current drawing operation.
 76   *
 77   * @private
 78   * @type Number
 79   */
 80  var x0 = 0;
 81
 82  /**
 83   * Holds the starting point on the <var>y</var> axis of the image, for the 
 84   * current drawing operation.
 85   *
 86   * @private
 87   * @type Number
 88   */
 89  var y0 = 0;
 90
 91  var globalOp_  = null,
 92      lineWidth_ = null;
 93
 94  /**
 95   * The tool deactivation event handler. This function clears timers, clears 
 96   * the canvas and allows shadows to be rendered again.
 97   */
 98  this.deactivate = function () {
 99    if (timer) {
100      clearInterval(timer);
101      timer = null;
102    }
103
104    if (mouse.buttonDown) {
105      if (globalOp_) {
106        layerContext.globalCompositeOperation = globalOp_;
107      }
108      if (lineWidth_) {
109        layerContext.lineWidth = lineWidth_;
110      }
111
112      app.historyGoto(history.pos);
113    }
114
115    points = [];
116
117    // Allow Canvas shadows.
118    app.shadowAllow();
119  };
120
121  /**
122   * The tool activation event handler. This is run after the tool construction 
123   * and after the deactivation of the previous tool. This function simply 
124   * disallows the rendering of shadows.
125   */
126  this.activate = function () {
127    // Do not allow Canvas shadows.
128    app.shadowDisallow();
129  };
130
131  /**
132   * Initialize the drawing operation.
133   */
134  this.mousedown = function () {
135    globalOp_  = layerContext.globalCompositeOperation;
136    lineWidth_ = layerContext.lineWidth;
137
138    layerContext.globalCompositeOperation = 'destination-out';
139    layerContext.lineWidth = bufferContext.lineWidth;
140
141    x0 = mouse.x;
142    y0 = mouse.y;
143
144    points = [];
145    if (!timer) {
146      timer = setInterval(_self.draw, config.toolDrawDelay);
147    }
148
149    return true;
150  };
151
152  /**
153   * Save the mouse coordinates in the array.
154   */
155  this.mousemove = function () {
156    if (mouse.buttonDown) {
157      points.push(mouse.x, mouse.y);
158    }
159  };
160
161  /**
162   * Draw the points in the stack. This function is called every few 
163   * milliseconds.
164   *
165   * @see PaintWeb.config.toolDrawDelay
166   */
167  this.draw = function () {
168    var i = 0, n = points.length;
169    if (!n) {
170      return;
171    }
172
173    layerContext.beginPath();
174    layerContext.moveTo(x0, y0);
175
176    while (i < n) {
177      x0 = points[i++];
178      y0 = points[i++];
179      layerContext.lineTo(x0, y0);
180    }
181
182    layerContext.stroke();
183    layerContext.closePath();
184
185    points = [];
186  };
187
188  /**
189   * End the drawing operation, once the user releases the mouse button.
190   */
191  this.mouseup = function () {
192    if (mouse.x == x0 && mouse.y == y0) {
193      points.push(x0+1, y0+1);
194    }
195
196    if (timer) {
197      clearInterval(timer);
198      timer = null;
199    }
200    _self.draw();
201
202    layerContext.globalCompositeOperation = globalOp_;
203    layerContext.lineWidth = lineWidth_;
204
205    app.historyAdd();
206
207    return true;
208  };
209
210  /**
211   * Allows the user to press <kbd>Escape</kbd> to cancel the drawing operation.
212   *
213   * @param {Event} ev The DOM Event object.
214   *
215   * @returns {Boolean} True if the drawing operation was cancelled, or false if 
216   * not.
217   */
218  this.keydown = function (ev) {
219    if (!mouse.buttonDown || ev.kid_ != 'Escape') {
220      return false;
221    }
222
223    if (timer) {
224      clearInterval(timer);
225      timer = null;
226    }
227
228    layerContext.globalCompositeOperation = globalOp_;
229    layerContext.lineWidth = lineWidth_;
230
231    mouse.buttonDown = false;
232    points = [];
233
234    app.historyGoto(history.pos);
235
236    return true;
237  };
238};
239
240// vim:set spell spl=en fo=wan1croqlt tw=80 ts=2 sw=2 sts=2 sta et ai cin fenc=utf-8 ff=unix:
241
242