Skip to content

Commit 7e6175d

Browse files
author
HarshKhandeparkar
committed
refactor<RealDrawBoard>: split into multiple files
1 parent 20ea718 commit 7e6175d

File tree

11 files changed

+529
-418
lines changed

11 files changed

+529
-418
lines changed

dist/gpujs-real-renderer-browser.js

Lines changed: 171 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,157 @@
986986
Object.defineProperty(exports, "__esModule", { value: true });
987987
});
988988

989+
var _initializeKernels_1 = createCommonjsModule(function (module, exports) {
990+
Object.defineProperty(exports, "__esModule", { value: true });
991+
exports._initializeKernels = void 0;
992+
993+
994+
function _initializeKernels() {
995+
this._plotKernel = plot.getPlotKernel(this.gpu, this.dimensions, this.xScaleFactor, this.yScaleFactor, this.xOffset, this.yOffset);
996+
this._strokeKernel = interpolate.getInterpolateKernel(this.gpu, this.dimensions, this.xScaleFactor, this.yScaleFactor, this.xOffset, this.yOffset);
997+
}
998+
exports._initializeKernels = _initializeKernels;
999+
});
1000+
1001+
var _stroke_1 = createCommonjsModule(function (module, exports) {
1002+
Object.defineProperty(exports, "__esModule", { value: true });
1003+
exports._stroke = void 0;
1004+
function _stroke(x, y) {
1005+
if (this._lastCoords === null)
1006+
this._lastCoords = [x, y];
1007+
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);
1008+
this._display(this.graphPixels);
1009+
}
1010+
exports._stroke = _stroke;
1011+
});
1012+
1013+
var _plot_1 = createCommonjsModule(function (module, exports) {
1014+
Object.defineProperty(exports, "__esModule", { value: true });
1015+
exports._plot = void 0;
1016+
function _plot(x, y) {
1017+
this.graphPixels = this._plotKernel(this._cloneTexture(this.graphPixels), x, y, this.mode === 'paint' ? this.brushSize : this.eraserSize, this.mode === 'paint' ? this.brushColor : this.bgColor);
1018+
this._display(this.graphPixels);
1019+
return this;
1020+
}
1021+
exports._plot = _plot;
1022+
});
1023+
1024+
var undo_1 = createCommonjsModule(function (module, exports) {
1025+
Object.defineProperty(exports, "__esModule", { value: true });
1026+
exports.redo = exports.undo = void 0;
1027+
function undo(numUndo) {
1028+
var _this = this;
1029+
if (numUndo === void 0) { numUndo = 1; }
1030+
if (this._pathIndex >= numUndo - 1 && this._pathIndex - numUndo < this._drawnPaths.length) {
1031+
this.graphPixels = this._blankGraph(); // Start with a blank graph
1032+
var originalMode = this.mode, originalBrushColor = this.brushColor, originalBrushSize = this.brushSize, originalEraserSize = this.eraserSize;
1033+
this._removeMouseEvents();
1034+
this._drawnPaths.slice(0, this._pathIndex - numUndo + 1).forEach(function (path) {
1035+
_this.mode = path.mode;
1036+
_this.brushColor = path.color;
1037+
_this.brushSize = path.brushSize;
1038+
_this.eraserSize = path.eraserSize;
1039+
_this._lastCoords = null;
1040+
path.pathCoords.forEach(function (coord) {
1041+
if (coord[2] === false) {
1042+
_this._stroke(coord[0], coord[1]); // Replay all strokes
1043+
_this._lastCoords = [coord[0], coord[1]];
1044+
}
1045+
else
1046+
_this._plot(coord[0], coord[1]);
1047+
});
1048+
});
1049+
this.mode = originalMode;
1050+
this.brushColor = originalBrushColor;
1051+
this.brushSize = originalBrushSize;
1052+
this.eraserSize = originalEraserSize;
1053+
this._pathIndex -= numUndo;
1054+
this._lastCoords = null;
1055+
this._display(this.graphPixels);
1056+
if (this._isDrawing)
1057+
this.startRender();
1058+
}
1059+
return this;
1060+
}
1061+
exports.undo = undo;
1062+
function redo(numRedo) {
1063+
if (numRedo === void 0) { numRedo = 1; }
1064+
this.undo(-numRedo);
1065+
return this;
1066+
}
1067+
exports.redo = redo;
1068+
});
1069+
1070+
var boardManip = createCommonjsModule(function (module, exports) {
1071+
Object.defineProperty(exports, "__esModule", { value: true });
1072+
exports._resetBoard = exports.clear = exports.changeMode = exports.changeEraserSize = exports.changeBrushSize = exports.changeBrushColor = void 0;
1073+
function changeBrushColor(color) {
1074+
this.brushColor = color;
1075+
return this;
1076+
}
1077+
exports.changeBrushColor = changeBrushColor;
1078+
function changeBrushSize(newSize) {
1079+
this.brushSize = newSize;
1080+
return this;
1081+
}
1082+
exports.changeBrushSize = changeBrushSize;
1083+
function changeEraserSize(newSize) {
1084+
this.eraserSize = newSize;
1085+
return this;
1086+
}
1087+
exports.changeEraserSize = changeEraserSize;
1088+
function changeMode(newMode) {
1089+
this.mode = newMode;
1090+
return this;
1091+
}
1092+
exports.changeMode = changeMode;
1093+
function clear() {
1094+
this._strokeHappening = false;
1095+
this._drawnPaths = [];
1096+
this._pathIndex = -1;
1097+
this._lastCoords = null;
1098+
this.graphPixels = this._blankGraph();
1099+
this._display(this.graphPixels);
1100+
return this;
1101+
}
1102+
exports.clear = clear;
1103+
function _resetBoard() {
1104+
this.xScaleFactor = this.options.xScaleFactor;
1105+
this.yScaleFactor = this.options.yScaleFactor;
1106+
this.brushColor = this.options.brushColor;
1107+
this.brushSize = this.options.brushSize;
1108+
this.bgColor = this.options.bgColor;
1109+
this.eraserSize = this.options.eraserSize;
1110+
this.mode = this.options.mode;
1111+
this._isDrawing = false;
1112+
this._strokeHappening = false;
1113+
this._drawnPaths = [];
1114+
this._pathIndex = -1;
1115+
this._lastCoords = null;
1116+
this.stopRender();
1117+
}
1118+
exports._resetBoard = _resetBoard;
1119+
});
1120+
1121+
var _DOMEvents = createCommonjsModule(function (module, exports) {
1122+
Object.defineProperty(exports, "__esModule", { value: true });
1123+
exports._removeMouseEvents = exports._addMouseEvents = void 0;
1124+
function _addMouseEvents() {
1125+
this.canvas.addEventListener('mousedown', this._mouseDownEventListener);
1126+
this.canvas.addEventListener('mouseup', this._mouseUpEventListener);
1127+
this.canvas.addEventListener('mouseenter', this._mouseEnterEventListener);
1128+
this.canvas.addEventListener('mouseleave', this._mouseLeaveEventListener);
1129+
}
1130+
exports._addMouseEvents = _addMouseEvents;
1131+
function _removeMouseEvents() {
1132+
this.canvas.removeEventListener('mousedown', this._mouseDownEventListener);
1133+
this.canvas.removeEventListener('mouseup', this._mouseUpEventListener);
1134+
this.canvas.removeEventListener('mouseenter', this._mouseEnterEventListener);
1135+
this.canvas.removeEventListener('mouseexit', this._mouseLeaveEventListener);
1136+
}
1137+
exports._removeMouseEvents = _removeMouseEvents;
1138+
});
1139+
9891140
var RealDrawBoard_1 = createCommonjsModule(function (module, exports) {
9901141
var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
9911142
var extendStatics = function (d, b) {
@@ -1032,11 +1183,15 @@
10321183
exports.RealDrawBoard = exports.RealDrawBoardTypes = exports.RealRendererTypes = void 0;
10331184

10341185

1035-
1036-
10371186
exports.RealRendererTypes = RealRendererTypes;
10381187
exports.RealDrawBoardTypes = RealDrawBoardTypes;
10391188
__exportStar(RealDrawBoardDefaults, exports);
1189+
1190+
1191+
1192+
1193+
1194+
10401195
var RealDrawBoard = /** @class */ (function (_super) {
10411196
__extends(RealDrawBoard, _super);
10421197
function RealDrawBoard(options) {
@@ -1048,6 +1203,19 @@
10481203
_this._drawnPaths = [];
10491204
_this._pathIndex = -1; // Index of path in _drawnPaths
10501205
_this._lastCoords = null;
1206+
_this._initializeKernels = _initializeKernels_1._initializeKernels;
1207+
_this._stroke = _stroke_1._stroke;
1208+
_this._plot = _plot_1._plot;
1209+
_this._resetBoard = boardManip._resetBoard;
1210+
_this._addMouseEvents = _DOMEvents._addMouseEvents;
1211+
_this._removeMouseEvents = _DOMEvents._removeMouseEvents;
1212+
_this.undo = undo_1.undo;
1213+
_this.redo = undo_1.redo;
1214+
_this.changeBrushColor = boardManip.changeBrushColor;
1215+
_this.changeBrushSize = boardManip.changeBrushSize;
1216+
_this.changeEraserSize = boardManip.changeEraserSize;
1217+
_this.changeMode = boardManip.changeMode;
1218+
_this.clear = boardManip.clear;
10511219
_this._getCoords = function (e) {
10521220
var x = e.offsetX; // in pixels
10531221
var y = _this.dimensions[1] - e.offsetY; // in pixels
@@ -1116,72 +1284,6 @@
11161284
_this._initializeKernels();
11171285
return _this;
11181286
}
1119-
RealDrawBoard.prototype._initializeKernels = function () {
1120-
this._plotKernel = plot.getPlotKernel(this.gpu, this.dimensions, this.xScaleFactor, this.yScaleFactor, this.xOffset, this.yOffset);
1121-
this._strokeKernel = interpolate.getInterpolateKernel(this.gpu, this.dimensions, this.xScaleFactor, this.yScaleFactor, this.xOffset, this.yOffset);
1122-
};
1123-
RealDrawBoard.prototype._addMouseEvents = function () {
1124-
this.canvas.addEventListener('mousedown', this._mouseDownEventListener);
1125-
this.canvas.addEventListener('mouseup', this._mouseUpEventListener);
1126-
this.canvas.addEventListener('mouseenter', this._mouseEnterEventListener);
1127-
this.canvas.addEventListener('mouseleave', this._mouseLeaveEventListener);
1128-
};
1129-
RealDrawBoard.prototype._removeMouseEvents = function () {
1130-
this.canvas.removeEventListener('mousedown', this._mouseDownEventListener);
1131-
this.canvas.removeEventListener('mouseup', this._mouseUpEventListener);
1132-
this.canvas.removeEventListener('mouseenter', this._mouseEnterEventListener);
1133-
this.canvas.removeEventListener('mouseexit', this._mouseLeaveEventListener);
1134-
};
1135-
RealDrawBoard.prototype._stroke = function (x, y) {
1136-
if (this._lastCoords === null)
1137-
this._lastCoords = [x, y];
1138-
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);
1139-
this._display(this.graphPixels);
1140-
};
1141-
RealDrawBoard.prototype._plot = function (x, y) {
1142-
this.graphPixels = this._plotKernel(this._cloneTexture(this.graphPixels), x, y, this.mode === 'paint' ? this.brushSize : this.eraserSize, this.mode === 'paint' ? this.brushColor : this.bgColor);
1143-
this._display(this.graphPixels);
1144-
return this;
1145-
};
1146-
RealDrawBoard.prototype.undo = function (numUndo) {
1147-
var _this = this;
1148-
if (numUndo === void 0) { numUndo = 1; }
1149-
if (this._pathIndex >= numUndo - 1 && this._pathIndex - numUndo < this._drawnPaths.length) {
1150-
this.graphPixels = this._blankGraph(); // Start with a blank graph
1151-
var originalMode = this.mode, originalBrushColor = this.brushColor, originalBrushSize = this.brushSize, originalEraserSize = this.eraserSize;
1152-
this._removeMouseEvents();
1153-
this._drawnPaths.slice(0, this._pathIndex - numUndo + 1).forEach(function (path) {
1154-
_this.mode = path.mode;
1155-
_this.brushColor = path.color;
1156-
_this.brushSize = path.brushSize;
1157-
_this.eraserSize = path.eraserSize;
1158-
_this._lastCoords = null;
1159-
path.pathCoords.forEach(function (coord) {
1160-
if (coord[2] === false) {
1161-
_this._stroke(coord[0], coord[1]); // Replay all strokes
1162-
_this._lastCoords = [coord[0], coord[1]];
1163-
}
1164-
else
1165-
_this._plot(coord[0], coord[1]);
1166-
});
1167-
});
1168-
this.mode = originalMode;
1169-
this.brushColor = originalBrushColor;
1170-
this.brushSize = originalBrushSize;
1171-
this.eraserSize = originalEraserSize;
1172-
this._pathIndex -= numUndo;
1173-
this._lastCoords = null;
1174-
this._display(this.graphPixels);
1175-
if (this._isDrawing)
1176-
this.startRender();
1177-
}
1178-
return this;
1179-
};
1180-
RealDrawBoard.prototype.redo = function (numRedo) {
1181-
if (numRedo === void 0) { numRedo = 1; }
1182-
this.undo(-numRedo);
1183-
return this;
1184-
};
11851287
RealDrawBoard.prototype.startRender = function () {
11861288
this._addMouseEvents();
11871289
this._isDrawing = true;
@@ -1192,45 +1294,8 @@
11921294
this._isDrawing = false;
11931295
return this;
11941296
};
1195-
RealDrawBoard.prototype.changeBrushColor = function (color) {
1196-
this.brushColor = color;
1197-
return this;
1198-
};
1199-
RealDrawBoard.prototype.changeBrushSize = function (newSize) {
1200-
this.brushSize = newSize;
1201-
return this;
1202-
};
1203-
RealDrawBoard.prototype.changeEraserSize = function (newSize) {
1204-
this.eraserSize = newSize;
1205-
return this;
1206-
};
1207-
RealDrawBoard.prototype.changeMode = function (newMode) {
1208-
this.mode = newMode;
1209-
return this;
1210-
};
1211-
RealDrawBoard.prototype.clear = function () {
1212-
this._strokeHappening = false;
1213-
this._drawnPaths = [];
1214-
this._pathIndex = -1;
1215-
this._lastCoords = null;
1216-
this.graphPixels = this._blankGraph();
1217-
this._display(this.graphPixels);
1218-
return this;
1219-
};
12201297
RealDrawBoard.prototype.reset = function () {
1221-
this.xScaleFactor = this.options.xScaleFactor;
1222-
this.yScaleFactor = this.options.yScaleFactor;
1223-
this.brushColor = this.options.brushColor;
1224-
this.brushSize = this.options.brushSize;
1225-
this.bgColor = this.options.bgColor;
1226-
this.eraserSize = this.options.eraserSize;
1227-
this.mode = this.options.mode;
1228-
this._isDrawing = false;
1229-
this._strokeHappening = false;
1230-
this._drawnPaths = [];
1231-
this._pathIndex = -1;
1232-
this._lastCoords = null;
1233-
this.stopRender();
1298+
this._resetBoard();
12341299
_super.prototype.reset.call(this);
12351300
return this;
12361301
};

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.

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { RealRenderer } from './src/renderers/RealRenderer';
22
export { RealLineGraph } from './src/renderers/RealLineGraph';
33
export { RealComplexSpace } from './src/renderers/RealComplexSpace';
4-
export { RealDrawBoard } from './src/renderers/RealDrawBoard';
4+
export { RealDrawBoard } from './src/renderers/RealDrawBoard/RealDrawBoard';

0 commit comments

Comments
 (0)