@@ -5,14 +5,13 @@ import { getRandomColor } from './utils'
55// @ts -expect-error -> no support for EaselJS in TypeScript -> https://github.com/CreateJS/EaselJS/issues/796
66import { Stage , Shape , Ticker } from '@createjs/easeljs'
77import { Shape as ShapeInterface } from './interfaces'
8- import { ShapeType } from './utils/constants'
8+ import { defaultPathThickness , ShapeType } from './utils/constants'
99import Sidebar from './components/Sidebar'
1010import ThreeJSViewer from './components/ThreeJSViewer'
1111import { isEmpty } from 'lodash'
1212
1313const Canvas : React . FC = ( ) => {
14- const pathThickness = 25
15-
14+ const [ pathThickness , setPathThickness ] = useState ( defaultPathThickness )
1615 const canvasRef = useRef < HTMLCanvasElement > ( null )
1716 const stageRef = useRef < Stage | null > ( null )
1817 const [ shapes , setShapes ] = useState < ShapeInterface [ ] > ( [ ] )
@@ -49,12 +48,13 @@ const Canvas: React.FC = () => {
4948 if ( props . endX === undefined || props . endY === undefined ) return
5049
5150 g . beginStroke ( props . strokeColor )
51+ . setStrokeStyle ( props ?. thickness ?? pathThickness )
5252 . moveTo ( 0 , 0 )
5353 . lineTo ( props . endX - props . x , props . endY - props . y )
5454 break
5555 case 'path' :
5656 if ( props ?. points && props . points ?. length > 1 ) {
57- g . beginStroke ( props . strokeColor ) . setStrokeStyle ( pathThickness )
57+ g . beginStroke ( props . strokeColor ) . setStrokeStyle ( props ?. thickness ?? pathThickness )
5858 g . moveTo ( props . points [ 0 ] . x , props . points [ 0 ] . y )
5959
6060 props . points . forEach ( ( point : { x : number ; y : number } , index : number ) => {
@@ -102,6 +102,7 @@ const Canvas: React.FC = () => {
102102 const g = this . graphics
103103 g . clear ( )
104104 . beginStroke ( props . strokeColor )
105+ . setStrokeStyle ( pathThickness )
105106 . moveTo ( 0 , 0 )
106107 . lineTo ( props . endX - props . x , props . endY - props . y )
107108 } else if ( props . type === 'path' && props . points ) {
@@ -177,7 +178,7 @@ const Canvas: React.FC = () => {
177178 break
178179 }
179180 case 'line' :
180- g . beginStroke ( pathColor . current ) . moveTo ( startX , startY ) . lineTo ( x , y )
181+ g . beginStroke ( pathColor . current ) . setStrokeStyle ( pathThickness ) . moveTo ( startX , startY ) . lineTo ( x , y )
181182 break
182183 case 'path' : {
183184 const newPoints = g . beginStroke ( pathColor . current ) . setStrokeStyle ( pathThickness )
@@ -227,11 +228,12 @@ const Canvas: React.FC = () => {
227228 shapeProps = {
228229 type : 'line' ,
229230 fillColor : 'transparent' ,
230- strokeColor : getRandomColor ( ) ,
231+ strokeColor : pathColor . current ,
231232 x : startPoint . x ,
232233 y : startPoint . y ,
233234 endX : x ,
234235 endY : y ,
236+ thickness : pathThickness ,
235237 }
236238 break
237239 default :
@@ -249,12 +251,11 @@ const Canvas: React.FC = () => {
249251 // Mouse event handlers
250252 const handleCanvasMouseDown = useCallback (
251253 ( event : React . MouseEvent < HTMLCanvasElement > ) => {
254+ if ( ! isDrawing ) pathColor . current = getRandomColor ( )
252255 const { offsetX, offsetY } = event . nativeEvent
253256
254257 if ( shapeType === 'path' ) {
255258 pathPointsRef . current . push ( { x : offsetX , y : offsetY } )
256- } else {
257- pathColor . current = getRandomColor ( )
258259 }
259260
260261 const clickedShape = stageRef . current ?. getObjectsUnderPoint ( offsetX , offsetY , 1 ) ?. [ 0 ] as ShapeInterface
@@ -383,6 +384,7 @@ const Canvas: React.FC = () => {
383384 y : 0 ,
384385 points : pathPointsRef . current ,
385386 instance : stageRef . current ?. children [ stageRef . current ?. children . length - 1 ] as Shape ,
387+ thickness : pathThickness ,
386388 }
387389
388390 stageRef . current ?. removeChild ( currentShape )
@@ -422,6 +424,8 @@ const Canvas: React.FC = () => {
422424 shapeType = { shapeType }
423425 toggleViewMode = { toggleViewMode }
424426 is3DMode = { is3DMode }
427+ pathThickness = { pathThickness }
428+ setPathThickness = { setPathThickness }
425429 />
426430 < div
427431 id = 'CanvasContainer'
0 commit comments