This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from daita-technologies/update/optimize_split…
…_layer_editor refactor: improve performance when drawing shapes
- Loading branch information
Showing
11 changed files
with
603 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/routes/AnnotationPage/Editor/Layer/DetectedRectangleDrawLayer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { KonvaEventObject } from "konva/lib/Node"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import { Layer, Rect } from "react-konva"; | ||
|
||
import Konva from "konva"; | ||
import { useDispatch } from "react-redux"; | ||
import { setDetectedArea } from "reduxes/annotation/action"; | ||
import { DetectedAreaType } from "reduxes/annotation/type"; | ||
import { convertStrokeColorToFillColor } from "routes/AnnotationPage/LabelAnnotation/ClassLabel"; | ||
import DummyRect from "./DummyRect"; | ||
|
||
const DetectedRectangleDrawLayer = () => { | ||
const dispatch = useDispatch(); | ||
const refDetectedArea = useRef<Konva.Rect | null>(null); | ||
const [localDetectedArea, setLocalDetectedArea] = | ||
useState<DetectedAreaType | null>(null); | ||
|
||
const mousedownHandler = (e: KonvaEventObject<MouseEvent>) => { | ||
const position = e.currentTarget.getRelativePointerPosition(); | ||
if (position) { | ||
setLocalDetectedArea({ | ||
x: position.x, | ||
y: position.y, | ||
width: 3, | ||
height: 3, | ||
}); | ||
} | ||
}; | ||
const mousemoveHandler = (e: KonvaEventObject<MouseEvent>) => { | ||
const position = e.currentTarget.getRelativePointerPosition(); | ||
if (position) { | ||
if (localDetectedArea) | ||
setLocalDetectedArea({ | ||
...localDetectedArea, | ||
width: position.x - localDetectedArea.x, | ||
height: position.y - localDetectedArea.y, | ||
}); | ||
} | ||
}; | ||
const mouseupHandler = (e: KonvaEventObject<MouseEvent>) => { | ||
if (localDetectedArea && refDetectedArea.current) { | ||
dispatch( | ||
setDetectedArea({ | ||
detectedArea: { ...refDetectedArea.current.getClientRect() }, | ||
}) | ||
); | ||
} | ||
setLocalDetectedArea(null); | ||
}; | ||
const layer = useRef<Konva.Layer | null>(null); | ||
useEffect(() => { | ||
layer.current?.moveToTop(); | ||
}, []); | ||
return ( | ||
<Layer | ||
ref={layer} | ||
onMouseMove={mousemoveHandler} | ||
onMouseDown={mousedownHandler} | ||
onMouseUp={mouseupHandler} | ||
> | ||
<DummyRect /> | ||
{localDetectedArea && ( | ||
<Rect | ||
ref={refDetectedArea} | ||
{...localDetectedArea} | ||
fill={convertStrokeColorToFillColor("#000000")} | ||
strokeWidth={4} | ||
stroke="#000000" | ||
/> | ||
)} | ||
</Layer> | ||
); | ||
}; | ||
export default DetectedRectangleDrawLayer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Layer } from "react-konva"; | ||
import { useSelector } from "react-redux"; | ||
import { | ||
selectorCurrentDrawState, | ||
selectorcurrentDrawType, | ||
} from "reduxes/annotation/selector"; | ||
import { DrawState, DrawType } from "reduxes/annotation/type"; | ||
import DetectedRectangleDrawLayer from "./DetectedRectangleDrawLayer"; | ||
import EllipseDrawLayer from "./EllipseDrawLayer"; | ||
import PolygonDrawLayer from "./PolygonDrawLayer"; | ||
import RectangleDrawLayer from "./RectangleDrawLayer"; | ||
|
||
const DrawLayer = () => { | ||
const currentDrawState = useSelector(selectorCurrentDrawState); | ||
const drawType = useSelector(selectorcurrentDrawType); | ||
|
||
const render = () => { | ||
if ( | ||
currentDrawState === DrawState.FREE || | ||
currentDrawState === DrawState.DRAWING | ||
) { | ||
if (drawType == DrawType.POLYGON || drawType == DrawType.LINE_STRIP) { | ||
return <PolygonDrawLayer />; | ||
} else if (drawType === DrawType.RECTANGLE) { | ||
return <RectangleDrawLayer />; | ||
} else if (drawType === DrawType.ELLIPSE) { | ||
return <EllipseDrawLayer />; | ||
} else if (drawType === DrawType.DETECTED_RECTANGLE) { | ||
return <DetectedRectangleDrawLayer />; | ||
} | ||
} | ||
return <Layer></Layer>; | ||
}; | ||
return render(); | ||
}; | ||
export default DrawLayer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Rect } from "react-konva"; | ||
import { useSelector } from "react-redux"; | ||
import { selectorCurrentAnnotationFile } from "reduxes/annotationmanager/selecetor"; | ||
|
||
const DummyRect = () => { | ||
const currentAnnotationFile = useSelector(selectorCurrentAnnotationFile); | ||
if (currentAnnotationFile) { | ||
const { width, height } = currentAnnotationFile; | ||
return <Rect width={width} height={height} />; | ||
} | ||
return <></>; | ||
}; | ||
export default DummyRect; |
Oops, something went wrong.