55import * as React from "react" ;
66import { CanvasToolType } from "./enums/canvas-tool-type" ;
77import { CanvasDrawToolSettings } from "./tools/base-canvas-draw-tool" ;
8- import { useEffect } from "@storybook/addons " ;
8+ import { useEffect } from "react " ;
99
1010// -------------------------------------------------------------------------------------------------
1111// #region Interfaces
@@ -38,8 +38,8 @@ export interface ReactCanvasSketchProps {
3838// #region Component
3939// -------------------------------------------------------------------------------------------------
4040
41- const ReactCanvasSketch : React . FC < ReactCanvasSketchProps > = (
42- props : ReactCanvasSketchProps
41+ const ReactCanvasSketch : React . FunctionComponent < ReactCanvasSketchProps > = (
42+ props : React . PropsWithChildren < ReactCanvasSketchProps >
4343) => {
4444
4545 // track the ref of the canvas elements
@@ -60,6 +60,7 @@ const ReactCanvasSketch: React.FC<ReactCanvasSketchProps> = (
6060
6161 // set up effects
6262 useEffect ( ( ) => {
63+ console . log ( 'start effect' ) ;
6364 if ( ! isInitialized ) {
6465 // set up the default options for the background image
6566 const canvasSketchConfig : CanvasSketchConfig = {
@@ -88,63 +89,103 @@ const ReactCanvasSketch: React.FC<ReactCanvasSketchProps> = (
8889 }
8990 return ( ) => {
9091 // clean up
91- canvasSketch . dispose ( ) ;
92+ console . log ( 'cleanup effect' ) ;
93+ if ( canvasSketch != null ) {
94+ canvasSketch . dispose ( ) ;
95+ }
9296 }
93- } ) ;
97+ } , [
98+ canvasSketch ,
99+ htmlCanvasBackgroundImage ,
100+ htmlCanvasSketch ,
101+ isInitialized ,
102+ props . backgroundImageUrl ,
103+ props . canvasToolType ,
104+ props . onAddedStroke ,
105+ props . toolColor ,
106+ props . toolWidth ,
107+ props . value . currentObjectIndex ,
108+ props . value . objects ,
109+ ] ) ;
94110
95111 useEffect ( ( ) => {
112+ if ( canvasSketch == null ) {
113+ return ;
114+ }
96115 if ( prevValueObjects === props . value . objects ) {
97116 canvasSketch . redrawSketchAt ( props . value . objects , props . value . currentObjectIndex ) ;
98117 }
99- } , [ props . value . currentObjectIndex , props . value . objects ] ) ; // https://stackoverflow.com/questions/55724642/react-useeffect-hook-when-only-one-of-the-effects-deps-changes-but-not-the-oth
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
100119
101120 useEffect ( ( ) => {
121+ if ( canvasSketch == null ) {
122+ return ;
123+ }
102124 canvasSketch . redrawSketchAt ( props . value . objects , props . value . currentObjectIndex ) ;
103- } , [ props . redrawIncrement ] ) ;
125+ } , [ props . redrawIncrement , canvasSketch , props . value . objects , props . value . currentObjectIndex ] ) ;
104126
105127 useEffect ( ( ) => {
128+ if ( canvasSketch == null ) {
129+ return ;
130+ }
106131 canvasSketch . redrawBackgroundImageUsing ( props . backgroundImageUrl ) ;
107- } , [ props . backgroundImageUrl ] ) ;
132+ } , [ props . backgroundImageUrl , canvasSketch ] ) ;
108133
109134 useEffect ( ( ) => {
135+ if ( canvasSketch == null ) {
136+ return ;
137+ }
110138 canvasSketch . setToolColor ( props . toolColor ) ;
111- } , [ props . toolColor ] ) ;
139+ } , [ props . toolColor , canvasSketch ] ) ;
112140
113141 useEffect ( ( ) => {
142+ if ( canvasSketch == null ) {
143+ return ;
144+ }
114145 canvasSketch . setToolWidth ( props . toolWidth ) ;
115- } , [ props . toolWidth ] ) ;
146+ } , [ props . toolWidth , canvasSketch ] ) ;
116147
117148 useEffect ( ( ) => {
149+ console . log ( `useEffect: start` ) ;
150+ if ( canvasSketch == null ) {
151+ return ;
152+ }
118153 canvasSketch . setSelectedTool ( props . canvasToolType ) ;
119- } , [ props . canvasToolType ] ) ;
154+ console . log ( `useEffect: ${ props . canvasToolType } ` ) ;
155+ console . log ( `useEffect: finish` ) ;
156+ } , [ props . canvasToolType , canvasSketch ] ) ;
120157
121158
122159 // configure styles for elemtns
123- const canvasStyles = {
160+ const canvasContainerStyles : React . CSSProperties = {
124161 height : props . canvasHeight ,
162+ position : "relative" ,
125163 width : props . canvasWidth ,
126164 } ;
127- const containerStyles = {
165+ const sketchStyles : React . CSSProperties = {
128166 height : props . containerHeight ,
129167 width : props . containerWidth ,
130168 } ;
169+ const canvasStyles : React . CSSProperties = {
170+ position : "absolute" ,
171+ } ;
131172
132173 return (
133174 < div
134175 className = { `c-react-canvas-sketch ${ props . className } ` }
135- style = { containerStyles } >
136- < div className = "c-react-canvas-sketch__canvas-container" style = { canvasStyles } >
176+ style = { sketchStyles } >
177+ < div className = "c-react-canvas-sketch__canvas-container" style = { canvasContainerStyles } >
137178 < canvas
138179 className = "c-react-canvas-sketch__background-image"
139180 height = { props . canvasHeight }
140181 ref = { htmlCanvasBackgroundImage }
141- style = { { zIndex : 0 } }
182+ style = { { ... canvasStyles , zIndex : 0 } }
142183 width = { props . canvasWidth } />
143184 < canvas
144185 className = "c-react-canvas-sketch__field"
145186 height = { props . canvasHeight }
146187 ref = { htmlCanvasSketch }
147- style = { { zIndex : 1 } }
188+ style = { { ... canvasStyles , zIndex : 1 } }
148189 width = { props . canvasWidth } >
149190 Sorry, Canvas HTML5 element is not supported by your browser :(
150191 </ canvas >
0 commit comments