@@ -62,7 +62,7 @@ const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanva
62
62
return { action : '' , x : x , y : y , width : width , height : height } ;
63
63
} ;
64
64
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 {
66
66
const scaledX = rect . x * scale ;
67
67
const scaledY = rect . y * scale ;
68
68
const scaledWidth = rect . width * scale ;
@@ -97,12 +97,13 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, scale: number = 1,
97
97
let strokeColor ;
98
98
const sentryFeedback = DOCUMENT . getElementById ( 'sentry-feedback' ) ;
99
99
if ( sentryFeedback ) {
100
+ const computedStyle = getComputedStyle ( sentryFeedback ) ;
100
101
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' ) ;
103
104
}
104
105
ctx . strokeStyle = strokeColor || 'white' ;
105
- ctx . lineWidth = lineWidth ;
106
+ ctx . lineWidth = 2 ;
106
107
ctx . strokeRect ( scaledX + 1 , scaledY + 1 , scaledWidth - 2 , scaledHeight - 2 ) ;
107
108
}
108
109
@@ -138,6 +139,10 @@ export function ScreenshotEditorFactoryv2({
138
139
const [ scaleFactor , setScaleFactor ] = hooks . useState < number > ( 1 ) ;
139
140
140
141
const resize = hooks . useCallback ( ( ) : void => {
142
+ if ( ! displayEditor ) {
143
+ return ;
144
+ }
145
+
141
146
const screenshotCanvas = screenshotRef . current ;
142
147
const graywashCanvas = graywashRef . current ;
143
148
const measurementDiv = measurementRef . current ;
@@ -163,7 +168,7 @@ export function ScreenshotEditorFactoryv2({
163
168
}
164
169
screenshotContext . drawImage ( imageSource , 0 , 0 , imageDimensions . width , imageDimensions . height ) ;
165
170
drawScene ( ) ;
166
- } , [ imageSource , drawCommands ] ) ;
171
+ } , [ imageSource , drawCommands , displayEditor ] ) ;
167
172
168
173
hooks . useEffect ( ( ) => {
169
174
WINDOW . addEventListener ( 'resize' , resize ) ;
@@ -175,7 +180,7 @@ export function ScreenshotEditorFactoryv2({
175
180
176
181
hooks . useLayoutEffect ( ( ) => {
177
182
resize ( ) ;
178
- } , [ displayEditor ] ) ;
183
+ } , [ resize ] ) ;
179
184
180
185
hooks . useEffect ( ( ) => {
181
186
drawScene ( ) ;
@@ -212,13 +217,14 @@ export function ScreenshotEditorFactoryv2({
212
217
grayCtx . fillRect ( 0 , 0 , imageBuffer . width , imageBuffer . height ) ;
213
218
}
214
219
220
+ grayCtx . lineWidth = 4 ;
215
221
drawCommands . forEach ( rect => {
216
222
drawRect ( rect , grayCtx ) ;
217
223
} ) ;
218
224
ctx . drawImage ( grayWashBufferBig , 0 , 0 ) ;
219
225
}
220
226
221
- function drawScene ( ) : void {
227
+ const drawScene = hooks . useCallback ( ( ) : void => {
222
228
const graywashCanvas = graywashRef . current ;
223
229
if ( ! graywashCanvas ) {
224
230
return ;
@@ -239,14 +245,13 @@ export function ScreenshotEditorFactoryv2({
239
245
240
246
const scale = graywashCanvas . clientWidth / imageBuffer . width ;
241
247
drawCommands . forEach ( rect => {
242
- drawRect ( rect , ctx , scale , 2 ) ;
248
+ drawRect ( rect , ctx , scale ) ;
243
249
} ) ;
244
250
245
251
if ( currentRect ) {
246
- drawRect ( currentRect , ctx , 1 , 2 ) ;
247
- setCurrentRect ( undefined ) ;
252
+ drawRect ( currentRect , ctx , 1 ) ;
248
253
}
249
- }
254
+ } , [ drawCommands , currentRect ] ) ;
250
255
251
256
useTakeScreenshot ( {
252
257
onBeforeScreenshot : hooks . useCallback ( ( ) => {
@@ -274,7 +279,7 @@ export function ScreenshotEditorFactoryv2({
274
279
} , [ ] ) ,
275
280
} ) ;
276
281
277
- const onDraw = ( e : MouseEvent ) : void => {
282
+ const handleMouseDown = ( e : MouseEvent ) : void => {
278
283
const graywashCanvas = graywashRef . current ;
279
284
if ( ! action || ! graywashCanvas ) {
280
285
return ;
@@ -298,6 +303,7 @@ export function ScreenshotEditorFactoryv2({
298
303
} ;
299
304
300
305
const handleMouseUp = ( e : MouseEvent ) : void => {
306
+ setCurrentRect ( undefined ) ;
301
307
const endX = Math . max ( 0 , Math . min ( e . clientX - boundingRect . left , graywashCanvas . width / DPI ) ) ;
302
308
const endY = Math . max ( 0 , Math . min ( e . clientY - boundingRect . top , graywashCanvas . height / DPI ) ) ;
303
309
// prevent drawing rect when clicking on the canvas (ie. clicking delete)
@@ -334,8 +340,8 @@ export function ScreenshotEditorFactoryv2({
334
340
< div class = "editor__image-container" >
335
341
< div class = "editor__canvas-container" ref = { measurementRef } >
336
342
< 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 } >
339
345
{ drawCommands . map ( ( rect , index ) => (
340
346
< div
341
347
key = { index }
@@ -346,7 +352,7 @@ export function ScreenshotEditorFactoryv2({
346
352
width : `${ rect . width * scaleFactor } px` ,
347
353
height : `${ rect . height * scaleFactor } px` ,
348
354
} }
349
- onMouseDown = { onDraw }
355
+ onMouseDown = { handleMouseDown }
350
356
>
351
357
< button type = "button" onClick = { ( ) => handleDeleteRect ( index ) } >
352
358
< IconClose />
0 commit comments