Skip to content

Commit 6352a88

Browse files
committed
feat: shapes support getWidth and getHeight for alignment
and potential auto size!
1 parent dc6317e commit 6352a88

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/shapes/rectangle.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export default class Rectangle extends Shape {
1313
canvas.drawRect(this.getRect(canvas), this.paint);
1414
}
1515
}
16-
@percentLengthProperty width: PercentLength;
17-
@percentLengthProperty height: PercentLength;
16+
1817
@lengthProperty left: Length = zeroLength;
1918
@lengthProperty top: Length = zeroLength;
2019
@numberProperty borderRadius: number;

src/shapes/shape.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,29 @@ export default abstract class Shape extends Observable {
174174
@stringProperty({ nonPaintProp: true }) visibility: Visibility = 'visible';
175175
@stringProperty({ nonPaintProp: true }) horizontalAlignment: HorizontalAlignment & 'middle';
176176
@stringProperty({ nonPaintProp: true }) verticalAlignment: VerticalAlignment & 'center';
177+
178+
@percentLengthProperty width: PercentLength;
179+
@percentLengthProperty height: PercentLength;
180+
177181
protected handleAlignment = false;
178182

179183
abstract drawOnCanvas(canvas: Canvas, parent: CanvasView): void;
180184

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+
181193
drawMyShapeOnCanvas(canvas: Canvas, parent: CanvasView, width: number, height: number) {
182194
if (this.visibility !== 'visible') {
183195
return;
184196
}
185197
if (!this.handleAlignment) {
198+
const availableWidth = Utils.layout.toDevicePixels(canvas.getWidth());
199+
const availableHeight = Utils.layout.toDevicePixels(canvas.getHeight());
186200
const paddingLeft = parent.effectivePaddingLeft + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderLeftWidth);
187201
const paddingRight = parent.effectivePaddingRight + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderRightWidth);
188202

@@ -202,14 +216,18 @@ export default abstract class Shape extends Observable {
202216
canvas.translate(0, -paddingBottom);
203217
}
204218
if (this.horizontalAlignment === 'right') {
205-
canvas.translate(width, 0);
219+
const sWidth = this.getWidth(availableWidth, availableHeight);
220+
canvas.translate(width - sWidth, 0);
206221
} 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);
208224
}
209225
if (this.verticalAlignment === 'bottom') {
210-
canvas.translate(0, height);
226+
const sHeight = this.getHeight(availableWidth, availableHeight);
227+
canvas.translate(0, height - sHeight);
211228
} 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);
213231
}
214232
}
215233

0 commit comments

Comments
 (0)