@@ -88,21 +88,31 @@ export function ScreenshotEditorFactory({
88
88
89
89
hooks . useEffect ( ( ) => {
90
90
WINDOW . addEventListener ( 'resize' , resize , false ) ;
91
+
92
+ // return () => {
93
+ // WINDOW.removeEventListener('resize', resize, false);
94
+ // };
91
95
} , [ ] ) ;
92
96
93
- function resize ( ) : void {
94
- const cropper = croppingRef . current ;
95
- const imageDimensions = constructRect ( getContainedSize ( imageBuffer ) ) ;
96
- if ( cropper ) {
97
- cropper . width = imageDimensions . width * DPI ;
98
- cropper . height = imageDimensions . height * DPI ;
99
- cropper . style . width = `${ imageDimensions . width } px` ;
100
- cropper . style . height = `${ imageDimensions . height } px` ;
101
- const ctx = cropper . getContext ( '2d' ) ;
97
+ function resizeCanvas ( canvasRef : Hooks . Ref < HTMLCanvasElement > , imageDimensions : Rect ) : void {
98
+ const canvas = canvasRef . current ;
99
+ if ( canvas ) {
100
+ canvas . width = imageDimensions . width * DPI ;
101
+ canvas . height = imageDimensions . height * DPI ;
102
+ canvas . style . width = `${ imageDimensions . width } px` ;
103
+ canvas . style . height = `${ imageDimensions . height } px` ;
104
+ const ctx = canvas . getContext ( '2d' ) ;
102
105
if ( ctx ) {
103
106
ctx . scale ( DPI , DPI ) ;
104
107
}
105
108
}
109
+ }
110
+
111
+ function resize ( ) : void {
112
+ const imageDimensions = constructRect ( getContainedSize ( imageBuffer ) ) ;
113
+
114
+ resizeCanvas ( croppingRef , imageDimensions ) ;
115
+ resizeCanvas ( annotatingRef , imageDimensions ) ;
106
116
107
117
const cropContainer = cropContainerRef . current ;
108
118
if ( cropContainer ) {
@@ -111,18 +121,6 @@ export function ScreenshotEditorFactory({
111
121
}
112
122
113
123
setCroppingRect ( { startX : 0 , startY : 0 , endX : imageDimensions . width , endY : imageDimensions . height } ) ;
114
-
115
- const annotater = annotatingRef . current ;
116
- if ( annotater ) {
117
- annotater . width = imageDimensions . width * DPI ;
118
- annotater . height = imageDimensions . height * DPI ;
119
- annotater . style . width = `${ imageDimensions . width } px` ;
120
- annotater . style . height = `${ imageDimensions . height } px` ;
121
- const ctx = annotater . getContext ( '2d' ) ;
122
- if ( ctx ) {
123
- ctx . scale ( DPI , DPI ) ;
124
- }
125
- }
126
124
}
127
125
128
126
hooks . useEffect ( ( ) => {
@@ -262,11 +260,10 @@ export function ScreenshotEditorFactory({
262
260
DOCUMENT . addEventListener ( 'mouseup' , handleMouseUp ) ;
263
261
}
264
262
265
- function onAnnotateStart ( e : MouseEvent ) : void {
263
+ function onAnnotateStart ( ) : void {
266
264
if ( ! isAnnotating ) return ;
267
265
268
266
const handleMouseMove = ( moveEvent : MouseEvent ) : void => {
269
- console . log ( moveEvent . clientX , moveEvent . clientY ) ;
270
267
const annotateCanvas = annotatingRef . current ;
271
268
if ( annotateCanvas ) {
272
269
const rect = annotateCanvas . getBoundingClientRect ( ) ;
@@ -293,7 +290,7 @@ export function ScreenshotEditorFactory({
293
290
294
291
// draws the annotation onto the image buffer
295
292
// TODO: move this to a better place
296
- submitAnnotate ( ) ;
293
+ applyAnnotation ( ) ;
297
294
298
295
DOCUMENT . removeEventListener ( 'mousemove' , handleMouseMove ) ;
299
296
DOCUMENT . removeEventListener ( 'mouseup' , handleMouseUp ) ;
@@ -303,7 +300,7 @@ export function ScreenshotEditorFactory({
303
300
DOCUMENT . addEventListener ( 'mouseup' , handleMouseUp ) ;
304
301
}
305
302
306
- function submitCrop ( ) : void {
303
+ function applyCrop ( ) : void {
307
304
const cutoutCanvas = DOCUMENT . createElement ( 'canvas' ) ;
308
305
const imageBox = constructRect ( getContainedSize ( imageBuffer ) ) ;
309
306
const croppingBox = constructRect ( croppingRect ) ;
@@ -337,7 +334,7 @@ export function ScreenshotEditorFactory({
337
334
}
338
335
}
339
336
340
- function submitAnnotate ( ) : void {
337
+ function applyAnnotation ( ) : void {
341
338
// draw the annotations onto the image (ie "squash" the canvases)
342
339
const imageCtx = imageBuffer . getContext ( '2d' ) ;
343
340
const annotateCanvas = annotatingRef . current ;
@@ -406,14 +403,10 @@ export function ScreenshotEditorFactory({
406
403
> </ button >
407
404
) }
408
405
< div class = "editor__canvas-container" ref = { canvasContainerRef } >
409
- < div
410
- class = "editor__crop-container"
411
- style = { { position : 'absolute' , zIndex : isAnnotating ? 1 : 2 } }
412
- ref = { cropContainerRef }
413
- >
406
+ < div class = "editor__crop-container" style = { { zIndex : isAnnotating ? 1 : 2 } } ref = { cropContainerRef } >
414
407
< canvas
415
408
onMouseDown = { onDragStart }
416
- style = { { position : 'absolute' , cursor : confirmCrop ? 'move' : 'auto' } }
409
+ style = { { cursor : confirmCrop ? 'move' : 'auto' } }
417
410
ref = { croppingRef }
418
411
> </ canvas >
419
412
< CropCorner
@@ -468,7 +461,7 @@ export function ScreenshotEditorFactory({
468
461
< button
469
462
onClick = { e => {
470
463
e . preventDefault ( ) ;
471
- submitCrop ( ) ;
464
+ applyCrop ( ) ;
472
465
setConfirmCrop ( false ) ;
473
466
} }
474
467
class = "btn btn--primary"
@@ -478,8 +471,9 @@ export function ScreenshotEditorFactory({
478
471
</ div >
479
472
</ div >
480
473
< canvas
474
+ class = "editor__annotation"
481
475
onMouseDown = { onAnnotateStart }
482
- style = { { position : 'absolute' , zIndex : isAnnotating ? '2' : '1' } }
476
+ style = { { zIndex : isAnnotating ? '2' : '1' } }
483
477
ref = { annotatingRef }
484
478
> </ canvas >
485
479
</ div >
0 commit comments