|
1008 | 1008 | } |
1009 | 1009 | exports._plot = _plot; |
1010 | 1010 | function _stroke(x, y) { |
1011 | | - if (this._lastCoords === null) |
1012 | | - this._lastCoords = [x, y]; |
1013 | | - this.graphPixels = this._strokeKernel(this._cloneTexture(this.graphPixels), this._lastCoords, [x, y], this.mode === 'paint' ? this.brushSize : this.eraserSize, this.mode === 'paint' ? this.brushColor : this.bgColor); |
| 1011 | + if (!this._lastCoords.has('mouse')) |
| 1012 | + this._lastCoords.set('mouse', [x, y]); |
| 1013 | + this.graphPixels = this._strokeKernel(this._cloneTexture(this.graphPixels), this._lastCoords.get('mouse'), [x, y], this.mode === 'paint' ? this.brushSize : this.eraserSize, this.mode === 'paint' ? this.brushColor : this.bgColor); |
1014 | 1014 | this._display(this.graphPixels); |
1015 | 1015 | } |
1016 | 1016 | exports._stroke = _stroke; |
|
1031 | 1031 | _this.brushColor = path.color; |
1032 | 1032 | _this.brushSize = path.brushSize; |
1033 | 1033 | _this.eraserSize = path.eraserSize; |
1034 | | - _this._lastCoords = null; |
| 1034 | + _this._lastCoords.delete('mouse'); |
1035 | 1035 | path.pathCoords.forEach(function (coord) { |
1036 | 1036 | if (coord[2] === false) { |
1037 | 1037 | _this._stroke(coord[0], coord[1]); // Replay all strokes |
1038 | | - _this._lastCoords = [coord[0], coord[1]]; |
| 1038 | + _this._lastCoords.set('mouse', [coord[0], coord[1]]); |
1039 | 1039 | } |
1040 | 1040 | else |
1041 | 1041 | _this._plot(coord[0], coord[1]); |
|
1046 | 1046 | this.brushSize = originalBrushSize; |
1047 | 1047 | this.eraserSize = originalEraserSize; |
1048 | 1048 | this._pathIndex -= numUndo; |
1049 | | - this._lastCoords = null; |
| 1049 | + this._lastCoords.delete('mouse'); |
1050 | 1050 | this._display(this.graphPixels); |
1051 | 1051 | if (this._isDrawing) |
1052 | 1052 | this.startRender(); |
|
1089 | 1089 | this._strokeHappening = false; |
1090 | 1090 | this._drawnPaths = []; |
1091 | 1091 | this._pathIndex = -1; |
1092 | | - this._lastCoords = null; |
| 1092 | + this._lastCoords.clear(); |
1093 | 1093 | this.graphPixels = this._blankGraph(); |
1094 | 1094 | this._display(this.graphPixels); |
1095 | 1095 | return this; |
|
1107 | 1107 | this._strokeHappening = false; |
1108 | 1108 | this._drawnPaths = []; |
1109 | 1109 | this._pathIndex = -1; |
1110 | | - this._lastCoords = null; |
| 1110 | + this._lastCoords.clear(); |
1111 | 1111 | this.stopRender(); |
1112 | 1112 | } |
1113 | 1113 | exports._resetBoard = _resetBoard; |
|
1151 | 1151 | brushSize: this.brushSize, |
1152 | 1152 | eraserSize: this.eraserSize |
1153 | 1153 | }; |
1154 | | - this._lastCoords = coords; |
| 1154 | + this._lastCoords.set('mouse', coords); |
1155 | 1155 | } |
1156 | 1156 | exports._startStroke = _startStroke; |
1157 | 1157 | function _endStroke(endCoords) { |
1158 | | - if (this._lastCoords[0] === endCoords[0] && |
1159 | | - this._lastCoords[1] === endCoords[1]) { |
| 1158 | + if (this._lastCoords.get('mouse')[0] === endCoords[0] && |
| 1159 | + this._lastCoords.get('mouse')[1] === endCoords[1]) { |
1160 | 1160 | this._plot.apply(this, endCoords); |
1161 | 1161 | this._drawnPaths[this._pathIndex + 1].pathCoords.push(__spreadArrays(endCoords, [true])); |
1162 | 1162 | } |
1163 | 1163 | if (this._strokeHappening) { |
1164 | | - this.canvas.removeEventListener('mousemove', this._mouseMoveEventListener); |
1165 | | - this._lastCoords = null; |
| 1164 | + this._lastCoords.delete('mouse'); |
1166 | 1165 | if (this._drawnPaths[this._pathIndex + 1].pathCoords.length === 0) |
1167 | 1166 | this._drawnPaths.splice(-1, 1); |
1168 | 1167 | else { |
|
1177 | 1176 | this._strokeHappening = true; |
1178 | 1177 | this._drawnPaths[this._pathIndex + 1].pathCoords.push(__spreadArrays(coords, [false])); |
1179 | 1178 | this._stroke.apply(this, coords); |
1180 | | - this._lastCoords = coords; |
| 1179 | + this._lastCoords.set('mouse', coords); |
1181 | 1180 | } |
1182 | 1181 | exports._doStroke = _doStroke; |
1183 | 1182 | }); |
1184 | 1183 |
|
1185 | 1184 | var _coords = createCommonjsModule(function (module, exports) { |
1186 | 1185 | Object.defineProperty(exports, "__esModule", { value: true }); |
1187 | | - exports._getMouseCoords = void 0; |
| 1186 | + exports._getTouchCoords = exports._getMouseCoords = void 0; |
1188 | 1187 | function _getMouseCoords(e) { |
1189 | 1188 | var x = e.offsetX; // in pixels |
1190 | 1189 | var y = this.dimensions[1] - e.offsetY; // in pixels |
|
1193 | 1192 | return [x, y]; // In graph coordinates |
1194 | 1193 | } |
1195 | 1194 | exports._getMouseCoords = _getMouseCoords; |
| 1195 | + function _getTouchCoords(e) { |
| 1196 | + var allCoords = []; |
| 1197 | + for (var i = 0; i < e.touches.length; i++) { |
| 1198 | + var touch = e.touches.item(i); |
| 1199 | + var x = touch.clientX - this.canvas.getBoundingClientRect().left; |
| 1200 | + var y = this.dimensions[1] - (touch.clientY - this.canvas.getBoundingClientRect().top); |
| 1201 | + x = x / this.xScaleFactor - (this.dimensions[0] * (this.yOffset / 100)) / this.xScaleFactor; |
| 1202 | + y = y / this.yScaleFactor - (this.dimensions[1] * (this.xOffset / 100)) / this.yScaleFactor; |
| 1203 | + allCoords.push([x, y]); |
| 1204 | + } |
| 1205 | + return allCoords; |
| 1206 | + } |
| 1207 | + exports._getTouchCoords = _getTouchCoords; |
1196 | 1208 | }); |
1197 | 1209 |
|
1198 | 1210 | var RealDrawBoard_1 = createCommonjsModule(function (module, exports) { |
|
1254 | 1266 | _this._strokeHappening = false; |
1255 | 1267 | _this._drawnPaths = []; |
1256 | 1268 | _this._pathIndex = -1; // Index of path in _drawnPaths |
1257 | | - _this._lastCoords = null; |
| 1269 | + /** key -> identifier, value -> coordinate |
| 1270 | + * For mouse, the key is 'mouse', for touches, stringified identifier -> https://developer.mozilla.org/en-US/docs/Web/API/Touch/identifier |
| 1271 | + */ |
| 1272 | + _this._lastCoords = new Map(); /* key -> identifier, value -> coordinate*/ |
1258 | 1273 | _this._initializeKernels = _initializeKernels_1._initializeKernels; |
1259 | 1274 | _this._stroke = _draw._stroke; |
1260 | 1275 | _this._plot = _draw._plot; |
|
1265 | 1280 | _this._endStroke = stroke._endStroke; |
1266 | 1281 | _this._doStroke = stroke._doStroke; |
1267 | 1282 | _this._getMouseCoords = _coords._getMouseCoords; |
| 1283 | + _this._getTouchCoords = _coords._getTouchCoords; |
1268 | 1284 | _this.undo = undo_1.undo; |
1269 | 1285 | _this.redo = undo_1.redo; |
1270 | 1286 | _this.changeBrushColor = boardManip.changeBrushColor; |
1271 | 1287 | _this.changeBrushSize = boardManip.changeBrushSize; |
1272 | 1288 | _this.changeEraserSize = boardManip.changeEraserSize; |
1273 | 1289 | _this.changeMode = boardManip.changeMode; |
1274 | 1290 | _this.clear = boardManip.clear; |
| 1291 | + // --- DOM Event Listeners --- |
| 1292 | + // --- Mouse Events --- |
1275 | 1293 | _this._mouseDownEventListener = function (e) { |
1276 | 1294 | if (e.button === 0 /* Left Click */) { |
1277 | 1295 | _this.canvas.addEventListener('mousemove', _this._mouseMoveEventListener); |
|
1282 | 1300 | if (e.button === 0 /* Left Click */) { |
1283 | 1301 | var endCoords = _this._getMouseCoords(e); |
1284 | 1302 | _this._endStroke(endCoords); |
| 1303 | + if (!_this._strokeHappening) |
| 1304 | + _this.canvas.removeEventListener('mousemove', _this._mouseMoveEventListener); |
1285 | 1305 | } |
1286 | 1306 | }; |
1287 | 1307 | _this._mouseEnterEventListener = function (e) { |
1288 | | - _this._lastCoords = _this._getMouseCoords(e); |
| 1308 | + _this._lastCoords.set('mouse', _this._getMouseCoords(e)); |
1289 | 1309 | }; |
1290 | 1310 | _this._mouseLeaveEventListener = function (e) { |
1291 | 1311 | _this._endStroke(_this._getMouseCoords(e)); |
|
1294 | 1314 | var coords = _this._getMouseCoords(e); |
1295 | 1315 | _this._doStroke(coords); |
1296 | 1316 | }; |
| 1317 | + // --- Mouse Events --- |
| 1318 | + // --- Touch Events --- |
| 1319 | + _this._touchStartEventListener = function (e) { |
| 1320 | + // this.canvas.addEventListener(); |
| 1321 | + }; |
| 1322 | + _this._touchEndEventListener = function (e) { |
| 1323 | + var endCoords = _this._getTouchCoords(e)[0]; |
| 1324 | + _this._endStroke(endCoords); |
| 1325 | + }; |
| 1326 | + _this._touchMoveEventListener = function (e) { |
| 1327 | + }; |
1297 | 1328 | options = __assign(__assign({}, RealDrawBoardDefaults.RealDrawBoardDefaults), options); |
1298 | 1329 | _this.options = options; |
1299 | 1330 | _this.brushSize = options.brushSize; |
|
1304 | 1335 | _this._initializeKernels(); |
1305 | 1336 | return _this; |
1306 | 1337 | } |
| 1338 | + // --- Touch Events --- |
| 1339 | + // --- DOM Event Listeners --- |
1307 | 1340 | RealDrawBoard.prototype.startRender = function () { |
1308 | 1341 | this._addDOMEvents(); |
1309 | 1342 | this._isDrawing = true; |
|
0 commit comments