Skip to content

Commit 20c0337

Browse files
author
Kevin Busch
committed
Added JSDocs
1 parent 5c5a206 commit 20c0337

1 file changed

Lines changed: 131 additions & 21 deletions

File tree

src/atoms/forms/canvas-sketch/canvas-sketch.ts

Lines changed: 131 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export interface CanvasSketchConfig {
2626
// #endregion Interfaces
2727

2828
/**
29-
* Binds to the provided HTML canvases element and gives various tool capabilities
29+
* Binds to the provided HTML canvases element in the contructor and provides access to switch
30+
* the selected tool and selected tool options like color and width through it's API
3031
*/
3132
class CanvasSketch {
3233

@@ -45,6 +46,12 @@ class CanvasSketch {
4546
private _sketchContext!: CanvasRenderingContext2D;
4647
private _toolConfig!: ToolConfig;
4748

49+
/**
50+
* Tracks
51+
*
52+
* @constructor
53+
* @param {CanvasSketchConfig} canvasSketchConfig - configuration of object
54+
*/
4855
constructor(canvasSketchConfig: CanvasSketchConfig) {
4956
this._drawToolUiSettings = {
5057
color: "FFFFFF", // default
@@ -75,6 +82,10 @@ class CanvasSketch {
7582
// #region Public Methods
7683
// ---------------------------------------------------------------------------------------------
7784

85+
/**
86+
* Disposes of any active bound events through the selected tool and anything else that this
87+
* object may need to clean up
88+
*/
7889
public dispose(): void {
7990
if (!this._isBrowserSupported) {
8091
return;
@@ -83,6 +94,11 @@ class CanvasSketch {
8394
this.selectedTool.dispose();
8495
}
8596

97+
/**
98+
* Gets the appropriate canva tool based on the provided tool type
99+
*
100+
* @param {CanvasToolType} toolType - The tool type of the tool caller is looking to use
101+
*/
86102
public getTool(toolType: CanvasToolType): CanvasTool | null {
87103
if (!this._isBrowserSupported) {
88104
return null;
@@ -102,11 +118,11 @@ class CanvasSketch {
102118
}
103119
}
104120

105-
public redrawCurrentState(): void {
106-
this._redrawBackgroundImage();
107-
this._redrawSketch();
108-
}
109-
121+
/**
122+
* Redraws the background image based on the URL provided
123+
*
124+
* @param {string} backgroundImageUrl - The URL of the background image to draw
125+
*/
110126
public redrawBackgroundImageUsing(backgroundImageUrl: string): void {
111127
if (this._config.backgroundImage == null) {
112128
return;
@@ -115,6 +131,22 @@ class CanvasSketch {
115131
this._redrawBackgroundImage();
116132
}
117133

134+
/**
135+
* Forces a redraw of the current state of the canvases. This is useful in cases where the
136+
* caller needs to redraw on demand
137+
*/
138+
public redrawCurrentState(): void {
139+
this._redrawBackgroundImage();
140+
this._redrawSketch();
141+
}
142+
143+
/**
144+
* Allows the caller to redraw the sketchpad at any point within the provided stack and
145+
* point in time within that stack
146+
*
147+
* @param {CanvasToolSettings[]} objects - The entire stack of drawn objects
148+
* @param {number} newCurrentObjectIndex - The point in time / point in stack to draw
149+
*/
118150
public redrawSketchAt(objects: CanvasToolSettings[], newCurrentObjectIndex: number): void {
119151
if (!this._isBrowserSupported) {
120152
return;
@@ -143,12 +175,17 @@ class CanvasSketch {
143175
this._redrawSketch();
144176
}
145177

178+
/**
179+
* Allows the caller to select one of the availalbe tool type
180+
*
181+
* @param {CanvasToolType} toolType - The tool to be selected
182+
*/
146183
public setSelectedTool(toolType: CanvasToolType): void {
147184
if (!this._isBrowserSupported) {
148185
return;
149186
}
150187

151-
if (this.selectedTool) {
188+
if (this.selectedTool != null) {
152189
this.selectedTool.dispose();
153190
}
154191

@@ -157,11 +194,18 @@ class CanvasSketch {
157194
return;
158195
}
159196
this.selectedTool = selectedTool;
160-
if (this.selectedTool) {
197+
if (this.selectedTool != null) {
161198
this.selectedTool.initialize();
162199
}
163200
}
164201

202+
/**
203+
* Allows the caller to define the color being used by the tool
204+
*
205+
* @param {string} color - The color to be selected (NOTE: must be in hex format with hash)
206+
* @example
207+
* canvasSketch.setToolColor('#5eeb34');
208+
*/
165209
public setToolColor(color: string): void {
166210
if (!this._isBrowserSupported) {
167211
return;
@@ -170,6 +214,14 @@ class CanvasSketch {
170214
this._drawToolUiSettings.color = color;
171215
}
172216

217+
/**
218+
* Allows the caller to define what happens when a tool successfully adds a stroke to the
219+
* drawn stack of objects
220+
*
221+
* @param {(strokeSettings: CanvasDrawToolSettings) => void} onAddedStroke - The function that
222+
* is called when a new drawn object is added to the stack. Returns the settings of that drawn
223+
* object including things like color, width, and the type of tool used
224+
*/
173225
public setOnAddedToolStroke(onAddedStroke: (strokeSettings: CanvasDrawToolSettings) => void): void {
174226
if (!this._isBrowserSupported) {
175227
return;
@@ -187,6 +239,13 @@ class CanvasSketch {
187239
};
188240
}
189241

242+
/**
243+
* Allows the caller to define the width being used by the tool
244+
*
245+
* @param {number} width - The width to be seelcted
246+
* @example
247+
* canvasSketch.setToolWidth(10);
248+
*/
190249
public setToolWidth(width: number): void {
191250
if (!this._isBrowserSupported) {
192251
return;
@@ -202,18 +261,9 @@ class CanvasSketch {
202261
// #region Private Methods
203262
// ---------------------------------------------------------------------------------------------
204263

205-
private _clearCanvas(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D): void {
206-
context.clearRect(0, 0, canvas.width, canvas.height);
207-
}
208-
209-
private _clearSketchCanvas(): void {
210-
if (!this._isBrowserSupported) {
211-
return;
212-
}
213-
214-
this._clearCanvas(this._config.sketchCanvas, this._sketchContext);
215-
}
216-
264+
/**
265+
* Removes the background image from the background image canvas so that it is no longer visible
266+
*/
217267
private _clearBackgroundImageCanvas(): void {
218268
if (!this._isBrowserSupported) {
219269
return;
@@ -226,6 +276,32 @@ class CanvasSketch {
226276
this._clearCanvas(this._config.backgroundImageCanvas, this._backgroundImageContext);
227277
}
228278

279+
/**
280+
* Removes all drawing on the provided canvas and context
281+
*
282+
* @param {HTMLCanvasElement} canvas - The canvas that will have its drawing removed
283+
* @param {CanvasRenderingContext2D} context - The context of the canvas to remove the drawing
284+
*/
285+
private _clearCanvas(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D): void {
286+
context.clearRect(0, 0, canvas.width, canvas.height);
287+
}
288+
289+
/**
290+
* Removes all drawing from the sketch canvas so that it is no longer visible
291+
*/
292+
private _clearSketchCanvas(): void {
293+
if (!this._isBrowserSupported) {
294+
return;
295+
}
296+
297+
this._clearCanvas(this._config.sketchCanvas, this._sketchContext);
298+
}
299+
300+
/**
301+
* Draws the provided objects onto the sketch canvas
302+
*
303+
* @param {CanvasToolSettings[]} objectStack - The provided objects to be drawn
304+
*/
229305
private _drawObjects(objectStack: CanvasToolSettings[]): void {
230306
if (!this._isBrowserSupported) {
231307
return;
@@ -244,6 +320,11 @@ class CanvasSketch {
244320
this.imageTool.drawImages(images);
245321
}
246322

323+
/**
324+
* Handler bound to tools drawing so capture the drawn stroke int he canvas sketch stack
325+
*
326+
* @param {CanvasDrawToolSettings} strokeSettings - The settings of the drawn stroke that was just added
327+
*/
247328
private _onAddStroke(strokeSettings: CanvasDrawToolSettings): void {
248329
// track the changes in it's own stack.. making sure to consider the current object index (history in view currently)
249330
this._config.objectStack = this._getStackToDraw();
@@ -252,17 +333,29 @@ class CanvasSketch {
252333
this._config.currentObjectIndex = this._config.currentObjectIndex + 1;
253334
}
254335

336+
/**
337+
* Returns an immutable stack of drawn strokes to draw at the point / point in time based on the current object index
338+
*/
255339
private _getStackToDraw(): CanvasToolSettings[] {
256340
const stackToDraw = [...this._config.objectStack].slice(0, this._config.currentObjectIndex + 1);
257341
return stackToDraw;
258342
}
259343

344+
/**
345+
* Determines if the provided canvas tool is a drawable tool
346+
*
347+
* @param {CanvasTool} tool - The tool to evaluate
348+
*/
260349
private _isInstanceOfDrawTool(tool: CanvasTool): tool is CanvasDrawTool {
261350
return "color" in tool
262351
&& "width" in tool
263352
&& "drawStrokes" in tool;
264353
}
265354

355+
/**
356+
* Uses the current canvas instances to establish the sketch and background image contexts and
357+
* while determining browser compatability with canvas at the same time
358+
*/
266359
private _initializeCanvasContexts(): void {
267360
if (this._config.sketchCanvas.getContext != null) {
268361
// browser supports the canvas tag, get the 2d drawing context for this canvas
@@ -295,6 +388,9 @@ class CanvasSketch {
295388
}
296389
}
297390

391+
/**
392+
* Uses the current object stack (if provided) to redraw the strokes onto the sketch canvas
393+
*/
298394
private _initializeCurrentDrawing(): void {
299395
if (this._config.objectStack?.length > 0) {
300396
// default the current stroke based on the initialized stroke stack's last object
@@ -309,9 +405,11 @@ class CanvasSketch {
309405

310406
this._drawObjects(stackToDraw);
311407
}
312-
313408
}
314409

410+
/**
411+
* Uses the current sketch canvas and context to set up all available tools to the caller
412+
*/
315413
private _initializeTools(): void {
316414
// setup default config settings for all tools
317415
this._toolConfig = {
@@ -352,6 +450,12 @@ class CanvasSketch {
352450
}
353451
}
354452

453+
/**
454+
* Positions the canvases (background image and sketch) to the provided coordinate values
455+
*
456+
* @param {number} panXDelta - The left pixel number
457+
* @param {number} panYDelta - The top pixel number
458+
*/
355459
private _panTo(panXDelta: number, panYDelta: number): void {
356460
const newPanX = this._config.panX + panXDelta;
357461
const newPanY = this._config.panY + panYDelta;
@@ -367,6 +471,9 @@ class CanvasSketch {
367471
}
368472
}
369473

474+
/**
475+
* Clears the sketch canvas and redraws all current stack objects to it
476+
*/
370477
private _redrawSketch(): void {
371478
if (!this._isBrowserSupported) {
372479
return;
@@ -379,6 +486,9 @@ class CanvasSketch {
379486
this._drawObjects(stackToDraw);
380487
}
381488

489+
/**
490+
* Clears the background image canvas and redraws the background image to it
491+
*/
382492
private _redrawBackgroundImage(): void {
383493
if (!this._isBrowserSupported) {
384494
return;

0 commit comments

Comments
 (0)