@@ -38,6 +38,7 @@ export default function IDE({ docId, modal, toggleModal, python, setpython, inpu
3838 const colorsRef = useRef ( null ) ;
3939 const [ userId , setUserId ] = useState ( null ) ;
4040 const [ myvideoon , setMyvideoon ] = useState ( true ) ;
41+ const [ pencilColor , setPencilColor ] = useState ( '#000000' ) ;
4142
4243
4344 useEffect ( ( ) => {
@@ -338,36 +339,58 @@ export default function IDE({ docId, modal, toggleModal, python, setpython, inpu
338339 } , [ socket ] )
339340
340341
342+ useEffect ( ( ) => {
343+ if ( socket === null ) return ;
344+ console . log ( "in" ) ;
345+ socket . emit ( 'pencil-color-change' , pencilColor ) ;
346+ socket . on ( 'pencil-color-change' , ( color ) => {
347+ console . log ( color ) ;
348+ setPencilColor ( color ) ;
349+ } )
350+ } , [ pencilColor ] ) ;
351+
341352 useEffect ( ( ) => {
342353
343354
344355 if ( socket === null || colorsRef === null ) return ;
345356 const canvas = document . getElementById ( 'whiteboard-canvas' )
346357 const context = canvas . getContext ( '2d' ) ;
358+ const colorPicker = document . getElementById ( 'pencil-color-picker' ) ;
347359
348360 const colors = document . getElementsByClassName ( 'color' ) ;
349361 // console.log(colors, 'the colors');
350362 // console.log(test);
351363 const current = {
352- color : 'black ' ,
364+ color : '#000000 ' ,
353365 width : 5 ,
354366 } ;
355367
356368 const onColorUpdate = ( e ) => {
357369 let objectColor ;
358370 for ( let i = 0 ; i < e . path . length ; i ++ ) {
359371 if ( e . path [ i ] . dataset . color ) {
360- objectColor = e . path [ i ] . dataset . color ;
372+ if ( e . path [ i ] . dataset . color === "white" ) objectColor = "#ffffff"
373+ else objectColor = pencilColor ;
361374 break ;
362375 }
363376 }
364377 current . color = objectColor ;
365- if ( current . color === 'black ') current . width = 5 ;
378+ if ( current . color !== '#ffffff ') current . width = 5 ;
366379 else current . width = 25 ;
367380 } ;
368381
382+ const onPencilColorChange = ( e ) => {
383+ setPencilColor ( e . target . value ) ;
384+ current . color = e . target . value ;
385+ if ( current . color !== '#ffffff' ) current . width = 5 ;
386+ else current . width = 25 ;
387+ }
388+
369389 for ( let i = 0 ; i < colors . length ; i ++ ) {
390+ colors [ i ] . removeEventListener ( 'click' , onColorUpdate ) ;
391+ colorPicker . removeEventListener ( 'change' , onPencilColorChange ) ;
370392 colors [ i ] . addEventListener ( 'click' , onColorUpdate , false ) ;
393+ colorPicker . addEventListener ( 'change' , onPencilColorChange , false ) ;
371394 }
372395 let drawing = false ;
373396
@@ -392,7 +415,7 @@ export default function IDE({ docId, modal, toggleModal, python, setpython, inpu
392415 y0 : y0 / h ,
393416 x1 : x1 / w ,
394417 y1 : y1 / h ,
395- color ,
418+ pencilColor ,
396419 width
397420 } ) ;
398421 } ;
@@ -562,6 +585,8 @@ export default function IDE({ docId, modal, toggleModal, python, setpython, inpu
562585 < div ref = { colorsRef } className = "colors absolute flex select-none left-10 top-10" >
563586 < Icon icon = { penFill } data-color = "black" className = "block cursor-pointer color black text-orange-standard" height = "28" />
564587 < Icon icon = { eraser24Filled } data-color = "white" className = "block cursor-pointer color white ml-4" height = "30" />
588+ { /* color picker input element */ }
589+ < input type = "color" className = "" id = "pencil-color-picker" />
565590 </ div >
566591 < div className = "absolute right-10 select-none top-10" >
567592 < img onClick = { toggleModal } src = { closeIcon } className = "w-6 cursor-pointer" alt = "close icon" />
0 commit comments