Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /* * Paper.js - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey * http://juerglehni.com/ & https://puckey.studio/ * * Distributed under the MIT license. See LICENSE file for details. * * All rights reserved. */ // TODO: remove eslint-disable comment and deal with errors over time /* eslint-disable */ import { ref } from '~/globals'; import { PaperScopeItem } from '~/core/PaperScopeItem'; import { ToolEvent } from './ToolEvent'; /** * @name Tool * * @class The Tool object refers to a script that the user can interact with by * using the mouse and keyboard and can be accessed through the global * `tool` variable. All its properties are also available in the paper * scope. * * The global `tool` variable only exists in scripts that contain mouse handler * functions ({@link #onMouseMove}, {@link #onMouseDown}, {@link #onMouseDrag}, * {@link #onMouseUp}) or a keyboard handler function ({@link #onKeyDown}, * {@link #onKeyUp}). * * @classexample {@paperscript height=300} * var path; * * // Only execute onMouseDrag when the mouse * // has moved at least 10 points: * tool.minDistance = 10; * * tool.onMouseDown = function(event) { * // Create a new path every time the mouse is clicked * path = new Path(); * path.add(event.point); * path.strokeColor = 'black'; * } * * tool.onMouseDrag = function(event) { * // Add a point to the path every time the mouse is dragged * path.add(event.point); * } */ export const Tool = PaperScopeItem.extend( /** @lends Tool# */ { _class: 'Tool', _list: 'tools', // TODO: This should be `activeTool` instead? _reference: 'tool', _events: [ 'onMouseDown', 'onMouseUp', 'onMouseDrag', 'onMouseMove', 'onActivate', 'onDeactivate', 'onEditOptions', 'onKeyDown', 'onKeyUp', ], // DOCS: rewrite Tool constructor explanation initialize: function Tool(props) { PaperScopeItem.call(this); // -1 so first event is 0: this._moveCount = -1; this._downCount = -1; this.set(props); }, /** * Activates this tool, meaning {@link PaperScope#tool} will * point to it and it will be the one that receives tool events. * * @name Tool#activate * @function */ /** * Removes this tool from the {@link PaperScope#tools} list. * * @name Tool#remove * @function */ /** * The minimum distance the mouse has to drag before firing the onMouseDrag * event, since the last onMouseDrag event. * * @bean * @type Number */ getMinDistance: function () { return this._minDistance; }, setMinDistance: function (minDistance) { this._minDistance = minDistance; if (minDistance != null && this._maxDistance != null && minDistance > this._maxDistance) { this._maxDistance = minDistance; } }, /** * The maximum distance the mouse has to drag before firing the onMouseDrag * event, since the last onMouseDrag event. * * @bean * @type Number */ getMaxDistance: function () { return this._maxDistance; }, setMaxDistance: function (maxDistance) { this._maxDistance = maxDistance; if (this._minDistance != null && maxDistance != null && maxDistance < this._minDistance) { this._minDistance = maxDistance; } }, // DOCS: document Tool#fixedDistance /** * @bean * @type Number */ getFixedDistance: function () { return this._minDistance == this._maxDistance ? this._minDistance : null; }, setFixedDistance: function (distance) { this._minDistance = this._maxDistance = distance; }, /** * {@grouptitle Mouse Event Handlers} * * The function to be called when the mouse button is pushed down. The * function receives a {@link ToolEvent} object which contains information * about the tool event. * * @name Tool#onMouseDown * @property * @type ?Function * * @example {@paperscript} * // Creating circle shaped paths where the user presses the mouse button: * tool.onMouseDown = function(event) { * // Create a new circle shaped path with a radius of 10 * // at the position of the mouse (event.point): * var path = new Path.Circle({ * center: event.point, * radius: 10, * fillColor: 'black' * }); * } */ /** * The function to be called when the mouse position changes while the mouse * is being dragged. The function receives a {@link ToolEvent} object which * contains information about the tool event. * * @name Tool#onMouseDrag * @property * @type ?Function * * @example {@paperscript} * // Draw a line by adding a segment to a path on every mouse drag event: * * // Create an empty path: * var path = new Path({ * strokeColor: 'black' * }); * * tool.onMouseDrag = function(event) { * // Add a segment to the path at the position of the mouse: * path.add(event.point); * } */ /** * The function to be called the mouse moves within the project view. The * function receives a {@link ToolEvent} object which contains information * about the tool event. * * @name Tool#onMouseMove * @property * @type ?Function * * @example {@paperscript} * // Moving a path to the position of the mouse: * * // Create a circle shaped path with a radius of 10 at {x: 0, y: 0}: * var path = new Path.Circle({ * center: [0, 0], * radius: 10, * fillColor: 'black' * }); * * tool.onMouseMove = function(event) { * // Whenever the user moves the mouse, move the path * // to that position: * path.position = event.point; * } */ /** * The function to be called when the mouse button is released. The function * receives a {@link ToolEvent} object which contains information about the * tool event. * * @name Tool#onMouseUp * @property * @type ?Function * * @example {@paperscript} * // Creating circle shaped paths where the user releases the mouse: * tool.onMouseUp = function(event) { * // Create a new circle shaped path with a radius of 10 * // at the position of the mouse (event.point): * var path = new Path.Circle({ * center: event.point, * radius: 10, * fillColor: 'black' * }); * } */ /** * {@grouptitle Keyboard Event Handlers} * * The function to be called when the user presses a key on the keyboard. * The function receives a {@link KeyEvent} object which contains * information about the keyboard event. * * If the function returns `false`, the keyboard event will be prevented * from bubbling up. This can be used for example to stop the window from * scrolling, when you need the user to interact with arrow keys. * * @name Tool#onKeyDown * @property * @type ?Function * * @example {@paperscript} * // Scaling a path whenever the user presses the space bar: * * // Create a circle shaped path: * var path = new Path.Circle({ * center: new Point(50, 50), * radius: 30, * fillColor: 'red' * }); * * tool.onKeyDown = function(event) { * if (event.key == 'space') { * // Scale the path by 110%: * path.scale(1.1); * * // Prevent the key event from bubbling * return false; * } * } */ /** * The function to be called when the user releases a key on the keyboard. * The function receives a {@link KeyEvent} object which contains * information about the keyboard event. * * If the function returns `false`, the keyboard event will be prevented * from bubbling up. This can be used for example to stop the window from * scrolling, when you need the user to interact with arrow keys. * * @name Tool#onKeyUp * @property * @type ?Function * * @example * tool.onKeyUp = function(event) { * if (event.key == 'space') { * console.log('The spacebar was released!'); * } * } */ /** * Private method to handle tool-events. * * @return {@true if at least one event handler was called}. */ _handleMouseEvent: function (type, event, point, mouse) { // Update global reference to this scope. ref.paper = this._scope; // If there is no mousedrag event installed, fall back to mousemove, // with which we share the actual event handling code anyhow. if (mouse.drag && !this.responds(type)) type = 'mousemove'; var move = mouse.move || mouse.drag, responds = this.responds(type), called = false, tool = this; // Updates the internal tool state, taking into account minDistance and // maxDistance and interpolating "fake" events along the moved distance // to respect their settings, if necessary. // Returns true as long as events should be fired, false when the target // is reached. function update(minDistance, maxDistance) { var pt = point, // Set toolPoint to the previous point for moves or downPoint // for clicks, so mouseup has a delta spanning over the full // drag. Use the current point if this is the first mousedown, // so there's always a delta. toolPoint = move ? tool._point : tool._downPoint || pt; if (move) { // After first move event was emitted, tool._moveCount = 0, so // we need to include 0 in this check. if (tool._moveCount >= 0 && pt.equals(toolPoint)) { return false; } if (toolPoint && (minDistance != null || maxDistance != null)) { var vector = pt.subtract(toolPoint), distance = vector.getLength(); if (distance < (minDistance || 0)) return false; // Produce a new point on the way to point if point is // further away than maxDistance if (maxDistance) { pt = toolPoint.add(vector.normalize(Math.min(distance, maxDistance))); } } tool._moveCount++; } tool._point = pt; tool._lastPoint = toolPoint || pt; if (mouse.down) { tool._moveCount = -1; tool._downPoint = pt; tool._downCount++; } return true; } function emit() { if (responds) { called = tool.emit(type, new ToolEvent(tool, type, event)) || called; } } if (mouse.down) { // @ts-expect-error update(); emit(); } else if (mouse.up) { update(null, this._maxDistance); emit(); } else if (responds) { while (update(this._minDistance, this._maxDistance)) emit(); } return called; }, /** * {@grouptitle Event Handling} * * Attach an event handler to the tool. * * @name Tool#on * @function * @param {String} type the event type: {@values 'mousedown', 'mouseup', * 'mousedrag', 'mousemove', 'keydown', 'keyup'} * @param {Function} function the function to be called when the event * occurs, receiving a {@link ToolEvent} object as its sole argument * @return {Tool} this tool itself, so calls can be chained */ /** * Attach one or more event handlers to the tool. * * @name Tool#on * @function * @param {Object} param an object literal containing one or more of the * following properties: {@values mousedown, mouseup, mousedrag, * mousemove, keydown, keyup} * @return {Tool} this tool itself, so calls can be chained */ /** * Detach an event handler from the tool. * * @name Tool#off * @function * @param {String} type the event type: {@values 'mousedown', 'mouseup', * 'mousedrag', 'mousemove', 'keydown', 'keyup'} * @param {Function} function the function to be detached * @return {Tool} this tool itself, so calls can be chained */ /** * Detach one or more event handlers from the tool. * * @name Tool#off * @function * @param {Object} param an object literal containing one or more of the * following properties: {@values mousedown, mouseup, mousedrag, * mousemove, keydown, keyup} * @return {Tool} this tool itself, so calls can be chained */ /** * Emit an event on the tool. * * @name Tool#emit * @function * @param {String} type the event type: {@values 'mousedown', 'mouseup', * 'mousedrag', 'mousemove', 'keydown', 'keyup'} * @param {Object} event an object literal containing properties describing * the event * @return {Boolean} {@true if the event had listeners} */ /** * Check if the tool has one or more event handlers of the specified type. * * @name Tool#responds * @function * @param {String} type the event type: {@values 'mousedown', 'mouseup', * 'mousedrag', 'mousemove', 'keydown', 'keyup'} * @return {Boolean} {@true if the tool has one or more event handlers of * the specified type} */ } ); |