@@ -107,59 +107,68 @@ function syncCtrlElPos(
107107 rectEl : HTMLElement ,
108108 ctrlsEl : Record < TCtrlKey , HTMLElement > ,
109109) : void {
110+ if ( s . interactable === 'disabled' ) {
111+ rectEl . style . display = 'none' ;
112+ return ;
113+ }
114+ rectEl . style . display = '' ;
115+
110116 const cvsRatio = getCvsRatio ( cvsEl ) ;
111117 const { x, y, w, h, angle } = s . rect ;
118+
112119 Object . assign ( rectEl . style , {
113120 left : `${ x * cvsRatio . w } px` ,
114121 top : `${ y * cvsRatio . h } px` ,
115122 width : `${ w * cvsRatio . w } px` ,
116123 height : `${ h * cvsRatio . h } px` ,
117- rotate : `${ angle } rad` ,
124+ transform : `rotate( ${ angle } rad) ` ,
118125 } ) ;
119- Object . entries ( getRectCtrls ( cvsEl , s . rect ) ) . forEach ( ( [ k , { x, y, w, h } ] ) => {
120- // ctrl 是相对中心点定位的
121- const baseStyle = {
126+
127+ const ctrlPosMap = getRectCtrls ( cvsEl , s . rect ) ;
128+
129+ for ( const k in ctrlsEl ) {
130+ const key = k as TCtrlKey ;
131+ const el = ctrlsEl [ key ] ;
132+ const pos = ctrlPosMap [ key ] ;
133+
134+ if ( pos == null ) {
135+ el . style . display = 'none' ;
136+ continue ;
137+ }
138+
139+ const baseStyle : Record < string , string > = {
140+ width : `${ pos . w * cvsRatio . w } px` ,
141+ height : `${ pos . h * cvsRatio . h } px` ,
142+ transform : `translate(${ pos . x * cvsRatio . w } px, ${ pos . y * cvsRatio . h } px)` ,
122143 left : '50%' ,
123144 top : '50%' ,
124- width : `${ w * cvsRatio . w } px` ,
125- height : `${ h * cvsRatio . h } px` ,
126- transform : `translate(${ x * cvsRatio . w } px, ${ y * cvsRatio . h } px)` ,
127145 } ;
128- ctrlsEl [ k as TCtrlKey ] . innerHTML = '' ;
129- if ( k === 'rotate' ) {
130- Object . assign ( ctrlsEl [ k as TCtrlKey ] . style , {
131- display : s . interactable === 'interactive' ? 'block' : 'none' ,
132- ...baseStyle ,
133- } ) ;
134- } else {
135- if ( s . interactable === 'interactive' ) {
136- Object . assign ( ctrlsEl [ k as TCtrlKey ] . style , {
146+
147+ let customStyle : Record < string , string > = { display : 'none' } ;
148+ el . innerHTML = '' ;
149+
150+ switch ( s . interactable ) {
151+ case 'interactive' :
152+ customStyle = {
137153 display : 'block' ,
138154 backgroundColor : '#fff' ,
139155 border : '1px solid #3ee' ,
140- ...baseStyle ,
141- } ) ;
142- } else if ( s . interactable === 'selectable' ) {
143- Object . assign ( ctrlsEl [ k as TCtrlKey ] . style , {
144- display : 'flex' ,
145- justifyContent : 'center' ,
146- alignItems : 'center' ,
147- backgroundColor : 'transparent' ,
148- border : 'none' ,
149- ...baseStyle ,
150- } ) ;
151- ctrlsEl [ k as TCtrlKey ] . innerHTML = CloseSvg ;
152- } else {
153- Object . assign ( ctrlsEl [ k as TCtrlKey ] . style , {
154- display : 'none' ,
155- ...baseStyle ,
156- } ) ;
157- }
156+ } ;
157+ break ;
158+ case 'selectable' :
159+ if ( key !== 'rotate' ) {
160+ customStyle = {
161+ display : 'flex' ,
162+ justifyContent : 'center' ,
163+ alignItems : 'center' ,
164+ backgroundColor : 'transparent' ,
165+ border : 'none' ,
166+ } ;
167+ el . innerHTML = CloseSvg ;
168+ }
169+ break ;
158170 }
159- } ) ;
160- if ( s . interactable === 'disabled' ) {
161- rectEl . style . display = 'none' ;
162- } else {
163- rectEl . style . display = '' ;
171+
172+ Object . assign ( el . style , baseStyle , customStyle ) ;
164173 }
165174}
0 commit comments