Skip to content

Commit e92ac0f

Browse files
author
Kevin Busch
committed
Cleanup
1 parent 5c9b11c commit e92ac0f

3 files changed

Lines changed: 36 additions & 12 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ class CanvasSketch {
102102
}
103103
}
104104

105+
public redrawCurrentState(): void {
106+
this._redrawBackgroundImage();
107+
this._redrawSketch();
108+
}
109+
105110
public redrawBackgroundImageUsing(backgroundImageUrl: string): void {
106111
if (this._config.backgroundImage == null) {
107112
return;

src/atoms/forms/canvas-sketch/react-canvas-sketch.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const ReactCanvasSketch: React.FunctionComponent<ReactCanvasSketchProps> = (
6060

6161
// set up effects
6262
useEffect(() => {
63-
console.log('start effect');
63+
console.log("useEffect: initializing");
6464
if (!isInitialized) {
6565
// set up the default options for the background image
6666
const canvasSketchConfig: CanvasSketchConfig = {
@@ -87,13 +87,6 @@ const ReactCanvasSketch: React.FunctionComponent<ReactCanvasSketchProps> = (
8787
setCanvasSketch(newCanvasSketch);
8888
setIsInitialized(true);
8989
}
90-
return () => {
91-
// clean up
92-
console.log('cleanup effect');
93-
if (canvasSketch != null) {
94-
canvasSketch.dispose();
95-
}
96-
}
9790
}, [
9891
canvasSketch,
9992
htmlCanvasBackgroundImage,
@@ -108,14 +101,42 @@ const ReactCanvasSketch: React.FunctionComponent<ReactCanvasSketchProps> = (
108101
props.value.objects,
109102
]);
110103

104+
useEffect(() => {
105+
console.log("useEffect: canvasSketch");
106+
return () => {
107+
// cleanup on unmount
108+
if (canvasSketch != null) {
109+
canvasSketch.dispose();
110+
}
111+
}
112+
}, [canvasSketch]);
113+
114+
useEffect(() => {
115+
if (canvasSketch == null) {
116+
return;
117+
}
118+
canvasSketch.redrawCurrentState();
119+
120+
}, [
121+
canvasSketch,
122+
props.canvasHeight,
123+
props.canvasWidth,
124+
props.containerHeight,
125+
props.containerWidth,
126+
]);
127+
111128
useEffect(() => {
112129
if (canvasSketch == null) {
113130
return;
114131
}
132+
// Only redraw if currentObjectIndex changes but not the objects list itself. Being done to
133+
// prevent rerenders after every single stroke of the same tool. See following URL for more
134+
// inforamtion on tracking previous state/props using hooks
135+
// https://stackoverflow.com/questions/55724642/react-useeffect-hook-when-only-one-of-the-effects-deps-changes-but-not-the-oth
115136
if (prevValueObjects === props.value.objects) {
116137
canvasSketch.redrawSketchAt(props.value.objects, props.value.currentObjectIndex);
117138
}
118-
}, [props.value.currentObjectIndex, props.value.objects, canvasSketch, prevValueObjects]); // https://stackoverflow.com/questions/55724642/react-useeffect-hook-when-only-one-of-the-effects-deps-changes-but-not-the-oth
139+
}, [props.value.currentObjectIndex, props.value.objects, canvasSketch, prevValueObjects]);
119140

120141
useEffect(() => {
121142
if (canvasSketch == null) {
@@ -146,13 +167,10 @@ const ReactCanvasSketch: React.FunctionComponent<ReactCanvasSketchProps> = (
146167
}, [props.toolWidth, canvasSketch]);
147168

148169
useEffect(() => {
149-
console.log(`useEffect: start`);
150170
if (canvasSketch == null) {
151171
return;
152172
}
153173
canvasSketch.setSelectedTool(props.canvasToolType);
154-
console.log(`useEffect: ${props.canvasToolType}`);
155-
console.log(`useEffect: finish`);
156174
}, [props.canvasToolType, canvasSketch]);
157175

158176

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export { Image } from "./atoms/images/image";
1010
export { ProgressBar } from "./atoms/progress-bar/progress-bar";
1111

1212
// Forms
13+
export { ReactCanvasSketch } from "./atoms/forms/canvas-sketch/react-canvas-sketch";
1314
export { CheckboxButton } from "./atoms/forms/checkbox-button";
1415
export { CheckboxInput } from "./atoms/forms/checkbox-input";
1516
export { InputCharacterCount } from "./atoms/forms/input-character-count";

0 commit comments

Comments
 (0)