@@ -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 ) : void {
65
+ function drawRect ( rect : Rect , ctx : CanvasRenderingContext2D , scale : number = 1 , lineWidth : number = 4 ) : void {
66
66
const scaledX = rect . x * scale ;
67
67
const scaledY = rect . y * scale ;
68
68
const scaledWidth = rect . width * scale ;
@@ -90,8 +90,12 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, scale: number = 1):
90
90
break ;
91
91
}
92
92
93
+ // Disable shadow after the action is drawn
94
+ ctx . shadowColor = 'transparent' ;
95
+ ctx . shadowBlur = 0 ;
96
+
93
97
ctx . strokeStyle = '#ff0000' ;
94
- ctx . lineWidth = 2 ;
98
+ ctx . lineWidth = lineWidth ;
95
99
ctx . strokeRect ( scaledX + 1 , scaledY + 1 , scaledWidth - 2 , scaledHeight - 2 ) ;
96
100
}
97
101
@@ -100,10 +104,6 @@ function resizeCanvas(canvas: HTMLCanvasElement, imageDimensions: Rect): void {
100
104
canvas . height = imageDimensions . height * DPI ;
101
105
canvas . style . width = `${ imageDimensions . width } px` ;
102
106
canvas . style . height = `${ imageDimensions . height } px` ;
103
- const ctx = canvas . getContext ( '2d' , { alpha : false } ) ;
104
- if ( ctx ) {
105
- ctx . scale ( DPI , DPI ) ;
106
- }
107
107
}
108
108
109
109
export function ScreenshotEditorFactoryv2 ( {
@@ -126,15 +126,16 @@ export function ScreenshotEditorFactoryv2({
126
126
const screenshotRef = hooks . useRef < HTMLCanvasElement > ( null ) ;
127
127
const graywashRef = hooks . useRef < HTMLCanvasElement > ( null ) ;
128
128
const rectDivRef = hooks . useRef < HTMLDivElement > ( null ) ;
129
- const [ imageSource , setImageSource ] = hooks . useState < HTMLCanvasElement | null > ( null ) ;
130
- const [ isShown , setIsShown ] = hooks . useState < boolean > ( true ) ;
129
+ const [ imageSource , setimageSource ] = hooks . useState < HTMLCanvasElement | null > ( null ) ;
130
+ const [ displayEditor , setdisplayEditor ] = hooks . useState < boolean > ( true ) ;
131
131
const [ scaleFactor , setScaleFactor ] = hooks . useState < number > ( 1 ) ;
132
132
133
133
const resize = hooks . useCallback ( ( ) : void => {
134
134
const screenshotCanvas = screenshotRef . current ;
135
135
const graywashCanvas = graywashRef . current ;
136
136
const measurementDiv = measurementRef . current ;
137
- if ( ! screenshotCanvas || ! graywashCanvas || ! imageSource || ! measurementDiv ) {
137
+ const rectDiv = rectDivRef . current ;
138
+ if ( ! screenshotCanvas || ! graywashCanvas || ! imageSource || ! measurementDiv || ! rectDiv ) {
138
139
return ;
139
140
}
140
141
@@ -143,24 +144,19 @@ export function ScreenshotEditorFactoryv2({
143
144
resizeCanvas ( screenshotCanvas , imageDimensions ) ;
144
145
resizeCanvas ( graywashCanvas , imageDimensions ) ;
145
146
147
+ rectDiv . style . width = `${ imageDimensions . width } px` ;
148
+ rectDiv . style . height = `${ imageDimensions . height } px` ;
149
+
146
150
const scale = graywashCanvas . clientWidth / imageBuffer . width ;
147
151
setScaleFactor ( scale ) ;
148
152
149
153
const screenshotContext = screenshotCanvas . getContext ( '2d' , { alpha : false } ) ;
150
154
if ( ! screenshotContext ) {
151
155
return ;
152
156
}
153
-
154
157
screenshotContext . drawImage ( imageSource , 0 , 0 , imageDimensions . width , imageDimensions . height ) ;
155
158
drawScene ( ) ;
156
-
157
- const rectDiv = rectDivRef . current ;
158
- if ( ! rectDiv ) {
159
- return ;
160
- }
161
- rectDiv . style . width = `${ imageDimensions . width } px` ;
162
- rectDiv . style . height = `${ imageDimensions . height } px` ;
163
- } , [ imageSource , isShown , drawCommands ] ) ;
159
+ } , [ imageSource , drawCommands ] ) ;
164
160
165
161
hooks . useEffect ( ( ) => {
166
162
WINDOW . addEventListener ( 'resize' , resize ) ;
@@ -170,12 +166,13 @@ export function ScreenshotEditorFactoryv2({
170
166
} ;
171
167
} , [ resize ] ) ;
172
168
169
+ hooks . useLayoutEffect ( ( ) => {
170
+ resize ( ) ;
171
+ } , [ displayEditor ] ) ;
172
+
173
173
hooks . useEffect ( ( ) => {
174
- const graywashCanvas = graywashRef . current ;
175
- if ( graywashCanvas ) {
176
- drawScene ( ) ;
177
- drawBuffer ( ) ;
178
- }
174
+ drawScene ( ) ;
175
+ drawBuffer ( ) ;
179
176
} , [ drawCommands ] ) ;
180
177
181
178
hooks . useEffect ( ( ) => {
@@ -235,47 +232,44 @@ export function ScreenshotEditorFactoryv2({
235
232
236
233
const scale = graywashCanvas . clientWidth / imageBuffer . width ;
237
234
drawCommands . forEach ( rect => {
238
- drawRect ( rect , ctx , scale ) ;
235
+ drawRect ( rect , ctx , scale , 2 ) ;
239
236
} ) ;
240
237
241
238
if ( currentRect ) {
242
- drawRect ( currentRect , ctx ) ;
239
+ drawRect ( currentRect , ctx , 1 , 2 ) ;
243
240
setCurrentRect ( undefined ) ;
244
241
}
245
242
}
246
243
247
244
useTakeScreenshot ( {
248
245
onBeforeScreenshot : hooks . useCallback ( ( ) => {
249
246
( dialog . el as HTMLElement ) . style . display = 'none' ;
250
- setIsShown ( false ) ;
247
+ setdisplayEditor ( false ) ;
251
248
} , [ ] ) ,
252
249
onScreenshot : hooks . useCallback ( ( imageSource : HTMLVideoElement ) => {
253
250
const bufferCanvas = DOCUMENT . createElement ( 'canvas' ) ;
254
251
bufferCanvas . width = imageSource . videoWidth ;
255
252
bufferCanvas . height = imageSource . videoHeight ;
256
253
bufferCanvas . getContext ( '2d' , { alpha : false } ) ?. drawImage ( imageSource , 0 , 0 ) ;
257
- setImageSource ( bufferCanvas ) ;
254
+ setimageSource ( bufferCanvas ) ;
255
+
258
256
imageBuffer . width = imageSource . videoWidth ;
259
257
imageBuffer . height = imageSource . videoHeight ;
260
258
} , [ ] ) ,
261
259
onAfterScreenshot : hooks . useCallback ( ( ) => {
262
260
( dialog . el as HTMLElement ) . style . display = 'block' ;
263
- setIsShown ( true ) ;
261
+ setdisplayEditor ( true ) ;
264
262
} , [ ] ) ,
265
263
onError : hooks . useCallback ( error => {
266
264
( dialog . el as HTMLElement ) . style . display = 'block' ;
267
- setIsShown ( true ) ;
265
+ setdisplayEditor ( true ) ;
268
266
onError ( error ) ;
269
267
} , [ ] ) ,
270
268
} ) ;
271
269
272
270
const onDraw = ( e : MouseEvent ) : void => {
273
- if ( ! action ) {
274
- return ;
275
- }
276
-
277
271
const graywashCanvas = graywashRef . current ;
278
- if ( ! graywashCanvas ) {
272
+ if ( ! action || ! graywashCanvas ) {
279
273
return ;
280
274
}
281
275
@@ -299,7 +293,7 @@ export function ScreenshotEditorFactoryv2({
299
293
const handleMouseUp = ( e : MouseEvent ) : void => {
300
294
const endX = Math . max ( 0 , Math . min ( e . clientX - boundingRect . left , graywashCanvas . width / DPI ) ) ;
301
295
const endY = Math . max ( 0 , Math . min ( e . clientY - boundingRect . top , graywashCanvas . height / DPI ) ) ;
302
- // prevent drawing rect when clicking on the canvas (ie clicking delete)
296
+ // prevent drawing rect when clicking on the canvas (ie. clicking delete)
303
297
if ( startX != endX && startY != endY ) {
304
298
// scale to image buffer
305
299
const scale = imageBuffer . width / graywashCanvas . clientWidth ;
@@ -331,51 +325,23 @@ export function ScreenshotEditorFactoryv2({
331
325
< div class = "editor" >
332
326
< style nonce = { options . styleNonce } dangerouslySetInnerHTML = { styles } />
333
327
< div class = "editor__image-container" >
334
- < div class = "editor__canvas-container" >
335
- < div ref = { measurementRef } style = { { position : 'absolute' , width : '100%' , height : '100%' } } > </ div >
336
- < canvas style = { { position : 'absolute' , zIndex : '1' , objectFit : 'contain' } } ref = { screenshotRef } > </ canvas >
337
- < canvas
338
- style = { { position : 'absolute' , zIndex : '2' , objectFit : 'contain' } }
339
- ref = { graywashRef }
340
- onMouseDown = { onDraw }
341
- > </ canvas >
342
- < div
343
- ref = { rectDivRef }
344
- style = { { position : 'absolute' , zIndex : '2' , objectFit : 'contain' } }
345
- onMouseDown = { onDraw }
346
- >
328
+ < div class = "editor__canvas-container" ref = { measurementRef } >
329
+ < canvas ref = { screenshotRef } > </ canvas >
330
+ < canvas class = "editor__canvas-annotate" ref = { graywashRef } onMouseDown = { onDraw } > </ canvas >
331
+ < div class = "editor__rect-container" ref = { rectDivRef } onMouseDown = { onDraw } >
347
332
{ drawCommands . map ( ( rect , index ) => (
348
333
< div
349
334
key = { index }
350
- class = "rect "
335
+ class = "editor__rect "
351
336
style = { {
352
- position : 'absolute' ,
353
337
top : `${ rect . y * scaleFactor } px` ,
354
338
left : `${ rect . x * scaleFactor } px` ,
355
339
width : `${ rect . width * scaleFactor } px` ,
356
340
height : `${ rect . height * scaleFactor } px` ,
357
- zIndex : 2 ,
358
341
} }
359
342
onMouseDown = { onDraw }
360
343
>
361
- < button
362
- type = "button"
363
- style = { {
364
- position : 'absolute' ,
365
- top : '-12px' ,
366
- right : '-12px' ,
367
- width : '25px' ,
368
- height : '25px' ,
369
- cursor : 'pointer' ,
370
- borderRadius : 999999 ,
371
- padding : 0 ,
372
- placeContent : 'center' ,
373
- zIndex : 3 ,
374
- border : 'none' ,
375
- background : 'none' ,
376
- } }
377
- onClick = { ( ) => handleDeleteRect ( index ) }
378
- >
344
+ < button type = "button" onClick = { ( ) => handleDeleteRect ( index ) } >
379
345
< IconClose />
380
346
</ button >
381
347
</ div >
0 commit comments