|
1068 | 1068 | exports.name = 'brush'; |
1069 | 1069 | function _startStroke(coords, identifier) { |
1070 | 1070 | this._doPreview = false; |
1071 | | - if (this._currentSnapshotIndex < this._snapshots.length - 1 && this._maxSnapshots > 0) |
1072 | | - this._snapshots.splice(this._currentSnapshotIndex + 1); // Delete all redo snapshots |
1073 | 1071 | this._plot(coords[0], coords[1], this.brushSize, this.brushColor); |
1074 | 1072 | this._lastCoords.set(identifier, coords); |
1075 | 1073 | } |
1076 | 1074 | exports._startStroke = _startStroke; |
1077 | 1075 | function _endStroke(endCoords, identifier) { |
1078 | 1076 | this._plot(endCoords[0], endCoords[1], this.brushSize, this.brushColor); |
1079 | 1077 | this._lastCoords.delete(identifier); |
1080 | | - if (this._maxSnapshots > 0) |
1081 | | - this._snapshots[++this._currentSnapshotIndex] = this.getData(); |
1082 | | - if (this._snapshots.length > this._maxSnapshots) { |
1083 | | - this._snapshots.shift(); |
1084 | | - this._currentSnapshotIndex--; |
1085 | | - } |
1086 | 1078 | this._doPreview = true; |
1087 | 1079 | } |
1088 | 1080 | exports._endStroke = _endStroke; |
|
1104 | 1096 | exports.name = 'eraser'; |
1105 | 1097 | function _startStroke(coords, identifier) { |
1106 | 1098 | this._doPreview = false; |
1107 | | - if (this._currentSnapshotIndex < this._snapshots.length - 1 && this._maxSnapshots > 0) |
1108 | | - this._snapshots.splice(this._currentSnapshotIndex + 1); // Delete all redo snapshots |
1109 | 1099 | this._plot(coords[0], coords[1], this.eraserSize, this.bgColor); |
1110 | 1100 | this._lastCoords.set(identifier, coords); |
1111 | 1101 | } |
|
1114 | 1104 | this._doPreview = true; |
1115 | 1105 | this._plot(endCoords[0], endCoords[1], this.eraserSize, this.bgColor); |
1116 | 1106 | this._lastCoords.delete(identifier); |
1117 | | - if (this._maxSnapshots > 0) |
1118 | | - this._snapshots[++this._currentSnapshotIndex] = this.getData(); |
1119 | | - if (this._snapshots.length > this._maxSnapshots) { |
1120 | | - this._snapshots.shift(); |
1121 | | - this._currentSnapshotIndex--; |
1122 | | - } |
1123 | 1107 | } |
1124 | 1108 | exports._endStroke = _endStroke; |
1125 | 1109 | function _doStroke(coords, identifier) { |
|
1143 | 1127 | */ |
1144 | 1128 | var _startCoords = new Map(); /* key -> identifier, value -> coordinate*/ |
1145 | 1129 | function _startStroke(coords, identifier) { |
1146 | | - if (this._currentSnapshotIndex < this._snapshots.length - 1 && this._maxSnapshots > 0) |
1147 | | - this._snapshots.splice(this._currentSnapshotIndex + 1); // Delete all redo snapshots |
1148 | 1130 | this._plot(coords[0], coords[1], this.brushSize, this.brushColor); |
1149 | 1131 | this._lastCoords.set(identifier, coords); |
1150 | 1132 | _startCoords.set(identifier, coords); |
|
1350 | 1332 | _super.call(this, options) || this; |
1351 | 1333 | _this.tool = RealDrawBoardDefaults.RealDrawBoardDefaults.tool; |
1352 | 1334 | _this._isDrawing = false; |
1353 | | - _this._isStroking = false; // If a tool is drawing a strwoke |
| 1335 | + _this._ = false; // If a tool is drawing a strwoke |
1354 | 1336 | _this._snapshots = []; // Undo snapshots |
1355 | 1337 | _this._currentSnapshotIndex = 0; // Current snapshot |
1356 | 1338 | /** key -> identifier, value -> coordinate |
|
1382 | 1364 | _this._mouseDownEventListener = function (e) { |
1383 | 1365 | if (e.button === 0 /* Left Click */) { |
1384 | 1366 | _this.canvas.addEventListener('mousemove', _this._mouseMoveEventListener); |
1385 | | - _this._isStroking = true; |
| 1367 | + if (_this._currentSnapshotIndex < _this._snapshots.length - 1 && _this._maxSnapshots > 0) |
| 1368 | + _this._snapshots.splice(_this._currentSnapshotIndex + 1); // Delete all redo snapshots |
1386 | 1369 | _this._startStroke(_this._getMouseCoords(e), 'mouse'); |
1387 | 1370 | } |
1388 | 1371 | }; |
1389 | 1372 | _this._mouseUpEventListener = function (e) { |
1390 | 1373 | if (e.button === 0 /* Left Click */) { |
1391 | 1374 | var endCoords = _this._getMouseCoords(e); |
| 1375 | + _this.canvas.removeEventListener('mousemove', _this._mouseMoveEventListener); |
| 1376 | + _this._removeDOMEvents(); |
1392 | 1377 | if (_this._lastCoords.has('mouse')) |
1393 | 1378 | _this._endStroke(endCoords, 'mouse'); |
1394 | | - _this._isStroking = false; |
1395 | 1379 | _this._display(_this.graphPixels); |
1396 | | - _this.canvas.removeEventListener('mousemove', _this._mouseMoveEventListener); |
| 1380 | + setTimeout(function () { |
| 1381 | + if (_this._maxSnapshots > 0) |
| 1382 | + _this._snapshots[++_this._currentSnapshotIndex] = _this.getData(); // Take snapshot |
| 1383 | + if (_this._snapshots.length > _this._maxSnapshots) { |
| 1384 | + _this._snapshots.shift(); |
| 1385 | + _this._currentSnapshotIndex--; |
| 1386 | + } |
| 1387 | + _this._addDOMEvents(); |
| 1388 | + }, 20); |
1397 | 1389 | } |
1398 | 1390 | }; |
1399 | 1391 | _this._mouseLeaveEventListener = function (e) { |
1400 | 1392 | _this.canvas.removeEventListener('mousemove', _this._mouseMoveEventListener); |
1401 | | - _this._isStroking = false; |
1402 | | - _this._display(_this.graphPixels); |
1403 | | - if (_this._lastCoords.has('mouse')) |
| 1393 | + if (_this._lastCoords.has('mouse')) { |
| 1394 | + _this._removeDOMEvents(); |
1404 | 1395 | _this._endStroke(_this._getMouseCoords(e), 'mouse'); |
| 1396 | + _this._display(_this.graphPixels); |
| 1397 | + setTimeout(function () { |
| 1398 | + if (_this._maxSnapshots > 0) |
| 1399 | + _this._snapshots[++_this._currentSnapshotIndex] = _this.getData(); // Take snapshot |
| 1400 | + if (_this._snapshots.length > _this._maxSnapshots) { |
| 1401 | + _this._snapshots.shift(); |
| 1402 | + _this._currentSnapshotIndex--; |
| 1403 | + } |
| 1404 | + _this._addDOMEvents(); |
| 1405 | + }, 20); |
| 1406 | + } |
1405 | 1407 | }; |
1406 | 1408 | _this._mouseMoveEventListener = function (e) { |
1407 | 1409 | var coords = _this._getMouseCoords(e); |
|
1420 | 1422 | _this._touchStartEventListener = function (e) { |
1421 | 1423 | e.preventDefault(); |
1422 | 1424 | for (var i = 0; i < e.touches.length; i++) { |
1423 | | - _this._isStroking = true; |
| 1425 | + if (_this._currentSnapshotIndex < _this._snapshots.length - 1 && _this._maxSnapshots > 0) |
| 1426 | + _this._snapshots.splice(_this._currentSnapshotIndex + 1); // Delete all redo snapshots |
1424 | 1427 | _this._startStroke(_this._getTouchCoords(e.touches.item(i)), e.touches.item(i).identifier.toString()); |
1425 | 1428 | } |
1426 | 1429 | }; |
1427 | 1430 | _this._touchEndEventListener = function (e) { |
1428 | 1431 | for (var i = 0; i < e.changedTouches.length; i++) { |
1429 | 1432 | _this._endStroke(_this._getTouchCoords(e.changedTouches.item(i)), e.changedTouches.item(i).identifier.toString()); |
1430 | | - _this._isStroking = false; |
1431 | 1433 | } |
| 1434 | + setTimeout(function () { |
| 1435 | + if (_this._maxSnapshots > 0) |
| 1436 | + _this._snapshots[++_this._currentSnapshotIndex] = _this.getData(); // Take snapshot |
| 1437 | + if (_this._snapshots.length > _this._maxSnapshots) { |
| 1438 | + _this._snapshots.shift(); |
| 1439 | + _this._currentSnapshotIndex--; |
| 1440 | + } |
| 1441 | + _this._addDOMEvents(); |
| 1442 | + }, 20); |
1432 | 1443 | }; |
1433 | 1444 | _this._touchMoveEventListener = function (e) { |
1434 | 1445 | for (var i = 0; i < e.touches.length; i++) { |
|
0 commit comments