Skip to content

Commit 69d37ca

Browse files
author
HarshKhandeparkar
committed
refactor<RealDrawBoard>: too many refactors
are harmful
1 parent cbf4849 commit 69d37ca

File tree

5 files changed

+44
-57
lines changed

5 files changed

+44
-57
lines changed

dist/gpujs-real-renderer-browser.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,6 @@
10861086
}
10871087
exports.changeMode = changeMode;
10881088
function clear() {
1089-
this._strokeHappening = false;
10901089
this._drawnPaths = [];
10911090
this._pathIndex = -1;
10921091
this._lastCoords.clear();
@@ -1104,7 +1103,6 @@
11041103
this.eraserSize = this.options.eraserSize;
11051104
this.mode = this.options.mode;
11061105
this._isDrawing = false;
1107-
this._strokeHappening = false;
11081106
this._drawnPaths = [];
11091107
this._pathIndex = -1;
11101108
this._lastCoords.clear();
@@ -1142,41 +1140,36 @@
11421140
};
11431141
Object.defineProperty(exports, "__esModule", { value: true });
11441142
exports._doStroke = exports._endStroke = exports._startStroke = void 0;
1145-
function _startStroke(coords) {
1146-
this._strokeHappening = true;
1143+
function _startStroke(coords, identifier) {
11471144
this._drawnPaths[this._pathIndex + 1] = {
11481145
pathCoords: [],
11491146
color: this.brushColor.map(function (x) { return x; }),
11501147
mode: this.mode,
11511148
brushSize: this.brushSize,
11521149
eraserSize: this.eraserSize
11531150
};
1154-
this._lastCoords.set('mouse', coords);
1151+
this._lastCoords.set(identifier, coords);
11551152
}
11561153
exports._startStroke = _startStroke;
1157-
function _endStroke(endCoords) {
1158-
if (this._lastCoords.get('mouse')[0] === endCoords[0] &&
1159-
this._lastCoords.get('mouse')[1] === endCoords[1]) {
1154+
function _endStroke(endCoords, identifier) {
1155+
if (this._lastCoords.get(identifier)[0] === endCoords[0] &&
1156+
this._lastCoords.get(identifier)[1] === endCoords[1]) {
11601157
this._plot.apply(this, endCoords);
11611158
this._drawnPaths[this._pathIndex + 1].pathCoords.push(__spreadArrays(endCoords, [true]));
11621159
}
1163-
if (this._strokeHappening) {
1164-
this._lastCoords.delete('mouse');
1165-
if (this._drawnPaths[this._pathIndex + 1].pathCoords.length === 0)
1166-
this._drawnPaths.splice(-1, 1);
1167-
else {
1168-
this._drawnPaths = this._drawnPaths.slice(0, this._pathIndex + 2); // Overwrite further paths to prevent wrong redos
1169-
this._pathIndex++;
1170-
}
1171-
this._strokeHappening = false;
1160+
this._lastCoords.delete(identifier);
1161+
if (this._drawnPaths[this._pathIndex + 1].pathCoords.length === 0)
1162+
this._drawnPaths.splice(-1, 1);
1163+
else {
1164+
this._drawnPaths = this._drawnPaths.slice(0, this._pathIndex + 2); // Overwrite further paths to prevent wrong redos
1165+
this._pathIndex++;
11721166
}
11731167
}
11741168
exports._endStroke = _endStroke;
1175-
function _doStroke(coords) {
1176-
this._strokeHappening = true;
1169+
function _doStroke(coords, identifier) {
11771170
this._drawnPaths[this._pathIndex + 1].pathCoords.push(__spreadArrays(coords, [false]));
11781171
this._stroke.apply(this, coords);
1179-
this._lastCoords.set('mouse', coords);
1172+
this._lastCoords.set(identifier, coords);
11801173
}
11811174
exports._doStroke = _doStroke;
11821175
});
@@ -1263,7 +1256,6 @@
12631256
// *****DEFAULTS*****
12641257
_super.call(this, options) || this;
12651258
_this._isDrawing = false;
1266-
_this._strokeHappening = false;
12671259
_this._drawnPaths = [];
12681260
_this._pathIndex = -1; // Index of path in _drawnPaths
12691261
/** key -> identifier, value -> coordinate
@@ -1293,26 +1285,25 @@
12931285
_this._mouseDownEventListener = function (e) {
12941286
if (e.button === 0 /* Left Click */) {
12951287
_this.canvas.addEventListener('mousemove', _this._mouseMoveEventListener);
1296-
_this._startStroke(_this._getMouseCoords(e));
1288+
_this._startStroke(_this._getMouseCoords(e), 'mouse');
12971289
}
12981290
};
12991291
_this._mouseUpEventListener = function (e) {
13001292
if (e.button === 0 /* Left Click */) {
13011293
var endCoords = _this._getMouseCoords(e);
1302-
_this._endStroke(endCoords);
1303-
if (!_this._strokeHappening)
1304-
_this.canvas.removeEventListener('mousemove', _this._mouseMoveEventListener);
1294+
_this._endStroke(endCoords, 'mouse');
1295+
_this.canvas.removeEventListener('mousemove', _this._mouseMoveEventListener);
13051296
}
13061297
};
13071298
_this._mouseEnterEventListener = function (e) {
13081299
_this._lastCoords.set('mouse', _this._getMouseCoords(e));
13091300
};
13101301
_this._mouseLeaveEventListener = function (e) {
1311-
_this._endStroke(_this._getMouseCoords(e));
1302+
_this._endStroke(_this._getMouseCoords(e), 'mouse');
13121303
};
13131304
_this._mouseMoveEventListener = function (e) {
13141305
var coords = _this._getMouseCoords(e);
1315-
_this._doStroke(coords);
1306+
_this._doStroke(coords, 'mouse');
13161307
};
13171308
// --- Mouse Events ---
13181309
// --- Touch Events ---
@@ -1321,7 +1312,7 @@
13211312
};
13221313
_this._touchEndEventListener = function (e) {
13231314
var endCoords = _this._getTouchCoords(e)[0];
1324-
_this._endStroke(endCoords);
1315+
_this._endStroke(endCoords, 'mouse');
13251316
};
13261317
_this._touchMoveEventListener = function (e) {
13271318
};

dist/gpujs-real-renderer-browser.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderers/RealDrawBoard/RealDrawBoard.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export class RealDrawBoard extends RealRenderer {
4242
eraserSize: number;
4343
mode: DrawMode;
4444
_isDrawing: boolean = false;
45-
_strokeHappening: boolean = false;
4645
_drawnPaths: {
4746
pathCoords: [number, number, boolean][], // [x, y, isAPoint][]
4847
color: Color,
@@ -106,16 +105,19 @@ export class RealDrawBoard extends RealRenderer {
106105
if (e.button === 0 /* Left Click */) {
107106
this.canvas.addEventListener('mousemove', this._mouseMoveEventListener);
108107

109-
this._startStroke(this._getMouseCoords(e));
108+
this._startStroke(
109+
this._getMouseCoords(e),
110+
'mouse'
111+
)
110112
}
111113
}
112114

113115
_mouseUpEventListener = (e: MouseEvent) => {
114116
if (e.button === 0 /* Left Click */) {
115117
const endCoords = this._getMouseCoords(e);
116-
this._endStroke(endCoords);
118+
this._endStroke(endCoords, 'mouse');
117119

118-
if (!this._strokeHappening) this.canvas.removeEventListener('mousemove', this._mouseMoveEventListener);
120+
this.canvas.removeEventListener('mousemove', this._mouseMoveEventListener);
119121
}
120122
}
121123

@@ -124,12 +126,12 @@ export class RealDrawBoard extends RealRenderer {
124126
}
125127

126128
_mouseLeaveEventListener = (e: MouseEvent) => {
127-
this._endStroke(this._getMouseCoords(e));
129+
this._endStroke(this._getMouseCoords(e), 'mouse');
128130
}
129131

130132
_mouseMoveEventListener = (e: MouseEvent) => {
131133
const coords = this._getMouseCoords(e);
132-
this._doStroke(coords);
134+
this._doStroke(coords, 'mouse');
133135
}
134136
// --- Mouse Events ---
135137

@@ -140,7 +142,7 @@ export class RealDrawBoard extends RealRenderer {
140142

141143
_touchEndEventListener = (e: TouchEvent) => {
142144
const endCoords = this._getTouchCoords(e)[0];
143-
this._endStroke(endCoords);
145+
this._endStroke(endCoords, 'mouse');
144146
}
145147

146148
_touchMoveEventListener = (e: TouchEvent) => {

src/renderers/RealDrawBoard/boardManip.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export function changeMode(this: RealDrawBoard, newMode: DrawMode) {
2424
}
2525

2626
export function clear(this: RealDrawBoard) {
27-
this._strokeHappening = false;
2827
this._drawnPaths = [];
2928
this._pathIndex = -1;
3029
this._lastCoords.clear();
@@ -45,7 +44,6 @@ export function _resetBoard(this: RealDrawBoard) {
4544
this.mode = this.options.mode;
4645

4746
this._isDrawing = false;
48-
this._strokeHappening = false;
4947
this._drawnPaths = [];
5048
this._pathIndex = -1;
5149
this._lastCoords.clear();

src/renderers/RealDrawBoard/stroke.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { Color } from '../../types/RealRendererTypes';
33

44
export function _startStroke(
55
this: RealDrawBoard,
6-
coords: [number, number]
6+
coords: [number, number],
7+
identifier: string
78
) {
8-
this._strokeHappening = true;
9-
109
this._drawnPaths[this._pathIndex + 1] = {
1110
pathCoords: [],
1211
color: <Color>this.brushColor.map(x => x),
@@ -15,40 +14,37 @@ export function _startStroke(
1514
eraserSize: this.eraserSize
1615
}
1716

18-
this._lastCoords.set('mouse', coords);
17+
this._lastCoords.set(identifier, coords);
1918
}
2019

2120
export function _endStroke(
2221
this: RealDrawBoard,
23-
endCoords: [number, number]
22+
endCoords: [number, number],
23+
identifier: string
2424
) {
2525
if (
26-
this._lastCoords.get('mouse')[0] === endCoords[0] &&
27-
this._lastCoords.get('mouse')[1] === endCoords[1]
26+
this._lastCoords.get(identifier)[0] === endCoords[0] &&
27+
this._lastCoords.get(identifier)[1] === endCoords[1]
2828
) {
2929
this._plot(...endCoords);
3030
this._drawnPaths[this._pathIndex + 1].pathCoords.push([...endCoords, true]);
3131
}
3232

33-
if (this._strokeHappening) {
34-
this._lastCoords.delete('mouse');
35-
36-
if (this._drawnPaths[this._pathIndex + 1].pathCoords.length === 0) this._drawnPaths.splice(-1, 1);
37-
else {
38-
this._drawnPaths = this._drawnPaths.slice(0, this._pathIndex + 2); // Overwrite further paths to prevent wrong redos
39-
this._pathIndex++;
40-
}
33+
this._lastCoords.delete(identifier);
4134

42-
this._strokeHappening = false;
35+
if (this._drawnPaths[this._pathIndex + 1].pathCoords.length === 0) this._drawnPaths.splice(-1, 1);
36+
else {
37+
this._drawnPaths = this._drawnPaths.slice(0, this._pathIndex + 2); // Overwrite further paths to prevent wrong redos
38+
this._pathIndex++;
4339
}
4440
}
4541

4642
export function _doStroke(
4743
this: RealDrawBoard,
48-
coords: [number, number]
44+
coords: [number, number],
45+
identifier: string
4946
) {
50-
this._strokeHappening = true;
5147
this._drawnPaths[this._pathIndex + 1].pathCoords.push([...coords, false]);
5248
this._stroke(...coords);
53-
this._lastCoords.set('mouse', coords);
49+
this._lastCoords.set(identifier, coords);
5450
}

0 commit comments

Comments
 (0)