1- import React , { FunctionComponent } from "react" ;
1+ import React , { FunctionComponent , useEffect } from "react" ;
22import { Stage , Layer , Image } from "react-konva" ;
33import Rectangle , { MIN_RECT_SIDE_PIXEL } from "./Rectangle" ;
44import { IgnoreArea } from "../types/ignoreArea" ;
@@ -47,6 +47,10 @@ interface IDrawArea {
4747 { x : number ; y : number } ,
4848 React . Dispatch < React . SetStateAction < { x : number ; y : number } > >
4949 ] ;
50+ stageScrollPosState : [
51+ { x : number ; y : number } ,
52+ React . Dispatch < React . SetStateAction < { x : number ; y : number } > >
53+ ] ;
5054 stageScaleState : [ number , React . Dispatch < React . SetStateAction < number > > ] ;
5155 drawModeState : [ boolean , React . Dispatch < React . SetStateAction < boolean > > ] ;
5256}
@@ -65,26 +69,34 @@ export const DrawArea: FunctionComponent<IDrawArea> = ({
6569 stageOffsetState,
6670 stageInitPosState,
6771 stagePosState,
72+ stageScrollPosState,
6873 drawModeState,
6974} ) => {
7075 const classes = useStyles ( ) ;
7176 const [ stageInitPos , setStageInitPos ] = stageInitPosState ;
7277 const [ stageOffset , setStageOffset ] = stageOffsetState ;
7378 const [ stagePos , setStagePos ] = stagePosState ;
79+ const [ stageScollPos , setStageScrollPos ] = stageScrollPosState ;
7480 const [ stageScale , setStageScale ] = stageScaleState ;
7581 const [ isDrag , setIsDrag ] = React . useState ( false ) ;
7682
7783 const [ isDrawMode , setIsDrawMode ] = drawModeState ;
7884 const [ isDrawing , setIsDrawing ] = React . useState ( isDrawMode ) ;
7985 const [ image , imageStatus ] = imageState ;
8086
87+ const scrollContainerRef = React . useRef < HTMLDivElement > ( null ) ;
88+
89+ useEffect ( ( ) => {
90+ scrollContainerRef . current ?. scrollTo ( stageScollPos . x , stageScollPos . y ) ;
91+ } , [ stageScollPos ] ) ;
92+
8193 const handleContentMousedown = ( e : any ) => {
8294 if ( ! isDrawMode ) return ;
8395
8496 const newArea : IgnoreArea = {
8597 id : Date . now ( ) . toString ( ) ,
86- x : Math . round ( ( e . evt . layerX - stageOffset . x ) / stageScale ) ,
87- y : Math . round ( ( e . evt . layerY - stageOffset . y ) / stageScale ) ,
98+ x : e . evt . offsetX ,
99+ y : e . evt . offsetY ,
88100 width : MIN_RECT_SIDE_PIXEL ,
89101 height : MIN_RECT_SIDE_PIXEL ,
90102 } ;
@@ -105,14 +117,11 @@ export const DrawArea: FunctionComponent<IDrawArea> = ({
105117
106118 if ( isDrawing ) {
107119 // update the current rectangle's width and height based on the mouse position + stage scale
108- const mouseX = ( e . evt . layerX - stageOffset . x ) / stageScale ;
109- const mouseY = ( e . evt . layerY - stageOffset . y ) / stageScale ;
110-
111120 const newShapesList = ignoreAreas . map ( ( i ) => {
112121 if ( i . id === selectedRectId ) {
113122 // new width and height
114- i . width = Math . max ( Math . round ( mouseX - i . x ) , MIN_RECT_SIDE_PIXEL ) ;
115- i . height = Math . max ( Math . round ( mouseY - i . y ) , MIN_RECT_SIDE_PIXEL ) ;
123+ i . width = Math . max ( Math . round ( e . evt . offsetX - i . x ) , MIN_RECT_SIDE_PIXEL ) ;
124+ i . height = Math . max ( Math . round ( e . evt . offsetY - i . y ) , MIN_RECT_SIDE_PIXEL ) ;
116125 return i ;
117126 }
118127 return i ;
@@ -138,7 +147,15 @@ export const DrawArea: FunctionComponent<IDrawArea> = ({
138147 ) }
139148 { ( ! imageName || imageStatus === "failed" ) && < NoImagePlaceholder /> }
140149 { imageName && imageStatus === "loaded" && (
141- < div className = { classes . canvasContainer } >
150+ < div className = { classes . canvasContainer }
151+ ref = { scrollContainerRef }
152+ onScroll = { ( event ) => {
153+ setStageScrollPos ( {
154+ x : ( event . target as HTMLElement ) . scrollLeft ,
155+ y :( event . target as HTMLElement ) . scrollTop
156+ } ) ;
157+ } }
158+ >
142159 < div className = { classes . imageDetailsContainer } >
143160 < ImageDetails
144161 type = { type }
@@ -187,7 +204,7 @@ export const DrawArea: FunctionComponent<IDrawArea> = ({
187204 e . evt . preventDefault ( ) ;
188205 const scaleBy = 1.04 ;
189206 const newScale =
190- e . evt . deltaY > 0
207+ e . evt . deltaY < 0
191208 ? stageScale * scaleBy
192209 : stageScale / scaleBy ;
193210 setStageScale ( newScale ) ;
0 commit comments