@@ -174,15 +174,29 @@ export default abstract class Shape extends Observable {
174
174
@stringProperty ( { nonPaintProp : true } ) visibility : Visibility = 'visible' ;
175
175
@stringProperty ( { nonPaintProp : true } ) horizontalAlignment : HorizontalAlignment & 'middle' ;
176
176
@stringProperty ( { nonPaintProp : true } ) verticalAlignment : VerticalAlignment & 'center' ;
177
+
178
+ @percentLengthProperty width : PercentLength ;
179
+ @percentLengthProperty height : PercentLength ;
180
+
177
181
protected handleAlignment = false ;
178
182
179
183
abstract drawOnCanvas ( canvas : Canvas , parent : CanvasView ) : void ;
180
184
185
+ getWidth ( availableWidth : number , availableHeight : number ) {
186
+ return this . width ? Utils . layout . toDeviceIndependentPixels ( PercentLength . toDevicePixels ( this . width , 0 , availableWidth ) ) : 0 ;
187
+ }
188
+ getHeight ( availableWidth : number , availableHeight : number ) {
189
+ return this . height ? Utils . layout . toDeviceIndependentPixels ( PercentLength . toDevicePixels ( this . height , 0 , availableHeight ) ) : 0 ;
190
+
191
+ }
192
+
181
193
drawMyShapeOnCanvas ( canvas : Canvas , parent : CanvasView , width : number , height : number ) {
182
194
if ( this . visibility !== 'visible' ) {
183
195
return ;
184
196
}
185
197
if ( ! this . handleAlignment ) {
198
+ const availableWidth = Utils . layout . toDevicePixels ( canvas . getWidth ( ) ) ;
199
+ const availableHeight = Utils . layout . toDevicePixels ( canvas . getHeight ( ) ) ;
186
200
const paddingLeft = parent . effectivePaddingLeft + Utils . layout . toDeviceIndependentPixels ( parent . effectiveBorderLeftWidth ) ;
187
201
const paddingRight = parent . effectivePaddingRight + Utils . layout . toDeviceIndependentPixels ( parent . effectiveBorderRightWidth ) ;
188
202
@@ -202,14 +216,18 @@ export default abstract class Shape extends Observable {
202
216
canvas . translate ( 0 , - paddingBottom ) ;
203
217
}
204
218
if ( this . horizontalAlignment === 'right' ) {
205
- canvas . translate ( width , 0 ) ;
219
+ const sWidth = this . getWidth ( availableWidth , availableHeight ) ;
220
+ canvas . translate ( width - sWidth , 0 ) ;
206
221
} else if ( this . horizontalAlignment === 'center' || this . horizontalAlignment === 'middle' ) {
207
- canvas . translate ( width / 2 , 0 ) ;
222
+ const sWidth = this . getWidth ( availableWidth , availableHeight ) ;
223
+ canvas . translate ( width / 2 - sWidth / 2 , 0 ) ;
208
224
}
209
225
if ( this . verticalAlignment === 'bottom' ) {
210
- canvas . translate ( 0 , height ) ;
226
+ const sHeight = this . getHeight ( availableWidth , availableHeight ) ;
227
+ canvas . translate ( 0 , height - sHeight ) ;
211
228
} else if ( this . verticalAlignment === 'center' || this . verticalAlignment === 'middle' ) {
212
- canvas . translate ( 0 , height / 2 ) ;
229
+ const sHeight = this . getHeight ( availableWidth , availableHeight ) ;
230
+ canvas . translate ( 0 , height / 2 - sHeight / 2 ) ;
213
231
}
214
232
}
215
233
0 commit comments