@@ -62,7 +62,7 @@ const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanva
6262 return { action : '' , x : x , y : y , width : width , height : height } ;
6363} ;
6464
65- function drawRect ( rect : Rect , ctx : CanvasRenderingContext2D , scale : number = 1 , lineWidth : number = 4 ) : void {
65+ function drawRect ( rect : Rect , ctx : CanvasRenderingContext2D , scale : number = 1 ) : void {
6666 const scaledX = rect . x * scale ;
6767 const scaledY = rect . y * scale ;
6868 const scaledWidth = rect . width * scale ;
@@ -97,12 +97,13 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, scale: number = 1,
9797 let strokeColor ;
9898 const sentryFeedback = DOCUMENT . getElementById ( 'sentry-feedback' ) ;
9999 if ( sentryFeedback ) {
100+ const computedStyle = getComputedStyle ( sentryFeedback ) ;
100101 strokeColor =
101- getComputedStyle ( sentryFeedback ) . getPropertyValue ( '--button-primary-background' ) ||
102- getComputedStyle ( sentryFeedback ) . getPropertyValue ( '--accent-background' ) ;
102+ computedStyle . getPropertyValue ( '--button-primary-background' ) ||
103+ computedStyle . getPropertyValue ( '--accent-background' ) ;
103104 }
104105 ctx . strokeStyle = strokeColor || 'white' ;
105- ctx . lineWidth = lineWidth ;
106+ ctx . lineWidth = 2 ;
106107 ctx . strokeRect ( scaledX + 1 , scaledY + 1 , scaledWidth - 2 , scaledHeight - 2 ) ;
107108}
108109
@@ -138,6 +139,10 @@ export function ScreenshotEditorFactoryv2({
138139 const [ scaleFactor , setScaleFactor ] = hooks . useState < number > ( 1 ) ;
139140
140141 const resize = hooks . useCallback ( ( ) : void => {
142+ if ( ! displayEditor ) {
143+ return ;
144+ }
145+
141146 const screenshotCanvas = screenshotRef . current ;
142147 const graywashCanvas = graywashRef . current ;
143148 const measurementDiv = measurementRef . current ;
@@ -163,7 +168,7 @@ export function ScreenshotEditorFactoryv2({
163168 }
164169 screenshotContext . drawImage ( imageSource , 0 , 0 , imageDimensions . width , imageDimensions . height ) ;
165170 drawScene ( ) ;
166- } , [ imageSource , drawCommands ] ) ;
171+ } , [ imageSource , drawCommands , displayEditor ] ) ;
167172
168173 hooks . useEffect ( ( ) => {
169174 WINDOW . addEventListener ( 'resize' , resize ) ;
@@ -175,7 +180,7 @@ export function ScreenshotEditorFactoryv2({
175180
176181 hooks . useLayoutEffect ( ( ) => {
177182 resize ( ) ;
178- } , [ displayEditor ] ) ;
183+ } , [ resize ] ) ;
179184
180185 hooks . useEffect ( ( ) => {
181186 drawScene ( ) ;
@@ -212,13 +217,14 @@ export function ScreenshotEditorFactoryv2({
212217 grayCtx . fillRect ( 0 , 0 , imageBuffer . width , imageBuffer . height ) ;
213218 }
214219
220+ grayCtx . lineWidth = 4 ;
215221 drawCommands . forEach ( rect => {
216222 drawRect ( rect , grayCtx ) ;
217223 } ) ;
218224 ctx . drawImage ( grayWashBufferBig , 0 , 0 ) ;
219225 }
220226
221- function drawScene ( ) : void {
227+ const drawScene = hooks . useCallback ( ( ) : void => {
222228 const graywashCanvas = graywashRef . current ;
223229 if ( ! graywashCanvas ) {
224230 return ;
@@ -239,14 +245,13 @@ export function ScreenshotEditorFactoryv2({
239245
240246 const scale = graywashCanvas . clientWidth / imageBuffer . width ;
241247 drawCommands . forEach ( rect => {
242- drawRect ( rect , ctx , scale , 2 ) ;
248+ drawRect ( rect , ctx , scale ) ;
243249 } ) ;
244250
245251 if ( currentRect ) {
246- drawRect ( currentRect , ctx , 1 , 2 ) ;
247- setCurrentRect ( undefined ) ;
252+ drawRect ( currentRect , ctx , 1 ) ;
248253 }
249- }
254+ } , [ drawCommands , currentRect ] ) ;
250255
251256 useTakeScreenshot ( {
252257 onBeforeScreenshot : hooks . useCallback ( ( ) => {
@@ -274,7 +279,7 @@ export function ScreenshotEditorFactoryv2({
274279 } , [ ] ) ,
275280 } ) ;
276281
277- const onDraw = ( e : MouseEvent ) : void => {
282+ const handleMouseDown = ( e : MouseEvent ) : void => {
278283 const graywashCanvas = graywashRef . current ;
279284 if ( ! action || ! graywashCanvas ) {
280285 return ;
@@ -298,6 +303,7 @@ export function ScreenshotEditorFactoryv2({
298303 } ;
299304
300305 const handleMouseUp = ( e : MouseEvent ) : void => {
306+ setCurrentRect ( undefined ) ;
301307 const endX = Math . max ( 0 , Math . min ( e . clientX - boundingRect . left , graywashCanvas . width / DPI ) ) ;
302308 const endY = Math . max ( 0 , Math . min ( e . clientY - boundingRect . top , graywashCanvas . height / DPI ) ) ;
303309 // prevent drawing rect when clicking on the canvas (ie. clicking delete)
@@ -334,8 +340,8 @@ export function ScreenshotEditorFactoryv2({
334340 < div class = "editor__image-container" >
335341 < div class = "editor__canvas-container" ref = { measurementRef } >
336342 < canvas ref = { screenshotRef } > </ canvas >
337- < canvas class = "editor__canvas-annotate" ref = { graywashRef } onMouseDown = { onDraw } > </ canvas >
338- < div class = "editor__rect-container" ref = { rectDivRef } onMouseDown = { onDraw } >
343+ < canvas class = "editor__canvas-annotate" ref = { graywashRef } onMouseDown = { handleMouseDown } > </ canvas >
344+ < div class = "editor__rect-container" ref = { rectDivRef } onMouseDown = { handleMouseDown } >
339345 { drawCommands . map ( ( rect , index ) => (
340346 < div
341347 key = { index }
@@ -346,7 +352,7 @@ export function ScreenshotEditorFactoryv2({
346352 width : `${ rect . width * scaleFactor } px` ,
347353 height : `${ rect . height * scaleFactor } px` ,
348354 } }
349- onMouseDown = { onDraw }
355+ onMouseDown = { handleMouseDown }
350356 >
351357 < button type = "button" onClick = { ( ) => handleDeleteRect ( index ) } >
352358 < IconClose />
0 commit comments