@@ -12,12 +12,31 @@ import {
1212 selectorDrawObjectState ,
1313 selectorSelectedEllipse ,
1414} from "reduxes/annotation/selector" ;
15- import { DrawState } from "reduxes/annotation/type" ;
16- import { CIRCLE_STYLE , CORNER_RADIUS } from "../const" ;
15+ import { DrawObject , DrawState , DrawType } from "reduxes/annotation/type" ;
16+ import { selectorCurrentImageInEditorProps } from "reduxes/annotationmanager/selecetor" ;
17+ import { CIRCLE_STYLE , CORNER_RADIUS , LINE_STYLE } from "../const" ;
1718import { EllipseCompProps , EllipseProps , EllipseSpec } from "../type" ;
1819import useCommonShapeEvent from "../useCommonShapeEvent" ;
1920
20- const EllipseComp = function ( {
21+ export const createEllipse = ( position : {
22+ x : number ;
23+ y : number ;
24+ } ) : DrawObject => {
25+ const id = `ELLIPSE-${ Math . floor ( Math . random ( ) * 100000 ) } ` ;
26+ const shape : EllipseSpec = {
27+ x : position . x ,
28+ y : position . y ,
29+ radiusX : 1 ,
30+ radiusY : 1 ,
31+ rotation : 0 ,
32+ id,
33+ label : { label : id } ,
34+ cssStyle : { ...LINE_STYLE } ,
35+ } ;
36+ return { type : DrawType . ELLIPSE , data : shape } ;
37+ } ;
38+
39+ function EllipseComp ( {
2140 spec,
2241 onMouseOverHandler,
2342 onMouseOutHandler,
@@ -29,16 +48,19 @@ const EllipseComp = function ({
2948 const commonShapeEvent = useCommonShapeEvent ( { drawObject : spec } ) ;
3049 const currentDrawState = useSelector ( selectorCurrentDrawState ) ;
3150 const drawObjectState = useSelector ( selectorDrawObjectState ( spec . id ) ) ;
32-
51+ const currentImageInEditorProps = useSelector (
52+ selectorCurrentImageInEditorProps
53+ ) ;
3354 const [ strokeWidth , setStrokeWidth ] = useState < number > (
3455 spec . cssStyle . strokeWidth
3556 ) ;
3657
37- const isSelected = useMemo ( ( ) => {
38- return currentShape != null && spec . id === currentShape . id ;
39- } , [ currentShape ?. id ] ) ;
58+ const isSelected = useMemo (
59+ ( ) => currentShape != null && spec . id === currentShape . id ,
60+ [ currentShape ?. id ]
61+ ) ;
4062 useEffect ( ( ) => {
41- if ( isSelected == true ) {
63+ if ( isSelected === true ) {
4264 shapeRef . current ?. moveToTop ( ) ;
4365 }
4466 } , [ isSelected ] ) ;
@@ -56,42 +78,74 @@ const EllipseComp = function ({
5678 trRef . current . getLayer ( ) . batchDraw ( ) ;
5779 }
5880 } , [ isSelected ] ) ;
59- const [ stage , setStage ] = useState < Konva . Stage | null > ( null ) ;
6081
6182 const groupDragBound = ( pos : Vector2d ) => {
6283 let { x, y } = pos ;
63- const sw = stage ? stage . width ( ) : 0 ;
64- const sh = stage ? stage . height ( ) : 0 ;
65- if ( shapeRef && shapeRef . current ) {
84+ if ( shapeRef && shapeRef . current && currentImageInEditorProps ) {
85+ const { clientRectOfBaseImage } = currentImageInEditorProps ;
6686 const box = shapeRef . current . getClientRect ( ) ;
6787
68- if ( y - box . height / 2 < 0 ) y = box . height / 2 ;
69- if ( x - box . width / 2 < 0 ) x = box . width / 2 ;
70- if ( y + box . height / 2 > sh ) y = sh - box . height / 2 ;
71- if ( x + box . width / 2 > sw ) x = sw - box . width / 2 ;
72- return { x, y } ;
88+ if ( clientRectOfBaseImage ) {
89+ if ( x - box . width / 2 < clientRectOfBaseImage . x ) {
90+ x = clientRectOfBaseImage . x + box . width / 2 ;
91+ }
92+ if ( y - box . height / 2 < clientRectOfBaseImage . y ) {
93+ y = clientRectOfBaseImage . y + box . height / 2 ;
94+ }
95+ if (
96+ x + box . width >
97+ clientRectOfBaseImage . x + clientRectOfBaseImage . width
98+ ) {
99+ x =
100+ clientRectOfBaseImage . x +
101+ clientRectOfBaseImage . width -
102+ box . width / 2 ;
103+ }
104+ if (
105+ y + box . height >
106+ clientRectOfBaseImage . y + clientRectOfBaseImage . height
107+ ) {
108+ y =
109+ clientRectOfBaseImage . y +
110+ clientRectOfBaseImage . height -
111+ box . height / 2 ;
112+ }
113+ }
73114 }
74- return { x : 0 , y : 0 } ;
75- } ;
76115
77- const handleDragStart = ( e : KonvaEventObject < DragEvent > ) => {
78- setStage ( e . target . getStage ( ) ) ;
79- commonShapeEvent . handleDragStart ( e ) ;
116+ return {
117+ x,
118+ y,
119+ } ;
80120 } ;
121+ // const groupDragBound = (pos: Vector2d) => {
122+ // let { x, y } = pos;
123+ // const sw = stage ? stage.width() : 0;
124+ // const sh = stage ? stage.height() : 0;
125+ // if (shapeRef && shapeRef.current) {
126+ // const box = shapeRef.current.getClientRect();
127+
128+ // if (y - box.height / 2 < 0) y = box.height / 2;
129+ // if (x - box.width / 2 < 0) x = box.width / 2;
130+ // if (y + box.height / 2 > sh) y = sh - box.height / 2;
131+ // if (x + box.width / 2 > sw) x = sw - box.width / 2;
132+ // return { x, y };
133+ // }
134+ // return { x: 0, y: 0 };
135+ // };
136+
81137 const handleTransformEnd = ( e : KonvaEventObject < Event > ) => {
82138 const node = shapeRef . current ;
83139 if ( ! node ) return ;
84-
85- let radiusX = ( node . width ( ) * node . scaleX ( ) ) / 2.0 ;
86- let radiusY = ( node . height ( ) * node . scaleY ( ) ) / 2.0 ;
87-
140+ const radiusX = ( node . width ( ) * node . scaleX ( ) ) / 2.0 ;
141+ const radiusY = ( node . height ( ) * node . scaleY ( ) ) / 2.0 ;
88142 onChange ( {
89143 ...spec ,
90144 x : node . x ( ) ,
91145 y : node . y ( ) ,
92146 rotation : node . attrs . rotation ,
93- radiusX : radiusX ,
94- radiusY : radiusY ,
147+ radiusX,
148+ radiusY,
95149 } ) ;
96150 node . scale ( { x : 1 , y : 1 } ) ;
97151 commonShapeEvent . handleTransformEnd ( e ) ;
@@ -122,7 +176,7 @@ const EllipseComp = function ({
122176 } ;
123177
124178 return (
125- < React . Fragment >
179+ < >
126180 < Ellipse
127181 ref = { shapeRef }
128182 dragBoundFunc = { groupDragBound }
@@ -133,7 +187,6 @@ const EllipseComp = function ({
133187 onMouseOut = { onMouseOut }
134188 draggable = { commonShapeEvent . isLock !== true }
135189 onDragEnd = { handleDragEnd }
136- onDragStart = { handleDragStart }
137190 onTransformEnd = { handleTransformEnd }
138191 strokeScaleEnabled = { false }
139192 { ...spec }
@@ -149,14 +202,14 @@ const EllipseComp = function ({
149202 anchorStrokeWidth = { CIRCLE_STYLE . strokeWidth }
150203 anchorStroke = { CIRCLE_STYLE . stroke }
151204 anchorSize = { CORNER_RADIUS * 1.8 }
152- ignoreStroke = { true }
205+ ignoreStroke
153206 boundBoxFunc = { boundBoxFunc }
154207 />
155208 ) }
156- </ React . Fragment >
209+ </ >
157210 ) ;
158- } ;
159- const EllipseShape = function ( {
211+ }
212+ function EllipseShape ( {
160213 id,
161214 onMouseOverHandler,
162215 onMouseOutHandler,
@@ -171,6 +224,6 @@ const EllipseShape = function ({
171224 />
172225 ) ;
173226 }
174- return < > </ > ;
175- } ;
227+ return null ;
228+ }
176229export default EllipseShape ;
0 commit comments