Skip to content

Commit 308f3e6

Browse files
committed
feat: horizontal and verticalAlignment basic support for shapes
1 parent a98a700 commit 308f3e6

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/canvas.android.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,10 @@ class CanvasView extends CanvasBase {
536536
canvas.drawBitmap(shapeCanvas.getImage() as android.graphics.Bitmap, 0, 0, this.shapePaint);
537537
} else if (!this.cached) {
538538
const shapes = this._shapes;
539+
const width = canvas.getWidth();
540+
const height = canvas.getHeight();
539541
if (shapes && shapes.length > 0) {
540-
shapes.forEach((s) => s.drawMyShapeOnCanvas(this.augmentedCanvas as any, this as any));
542+
shapes.forEach((s) => s.drawMyShapeOnCanvas(this.augmentedCanvas as any, this as any, width, height));
541543
}
542544
}
543545
this.notify({ eventName: 'draw', object: this, canvas: this.augmentedCanvas });

src/canvas.common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export abstract class CanvasBase extends View {
169169
if (this._shapes && this._shapes.length > 0 && width > 0 && height > 0) {
170170
const canvas = (this.shapesCanvas = new Canvas(width, height));
171171
canvas.setDensity(this.density);
172-
this._shapes.forEach((s) => s.drawMyShapeOnCanvas(canvas, this as any));
172+
this._shapes.forEach((s) => s.drawMyShapeOnCanvas(canvas, this as any, width, height));
173173
this.redraw();
174174
}
175175
}

src/shapes/shape.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-redeclare */
2-
import { Color, Length, Observable, PercentLength, Visibility } from '@nativescript/core';
2+
import { Color, HorizontalAlignment, Length, Observable, PercentLength, Utils, VerticalAlignment, Visibility } from '@nativescript/core';
33
import { booleanConverter } from '@nativescript/core/ui/core/view-base';
44
import { Canvas, CanvasView, Cap, Join, Paint, Style } from '../canvas';
55
import { parseCap, parseDashEffect, parseJoin, parseShadow, parseType } from '../utils';
@@ -172,13 +172,47 @@ export default abstract class Shape extends Observable {
172172
})
173173
shadow: Shadow;
174174
@stringProperty({ nonPaintProp: true }) visibility: Visibility = 'visible';
175+
@stringProperty({ nonPaintProp: true }) horizontalAlignment: HorizontalAlignment & 'middle';
176+
@stringProperty({ nonPaintProp: true }) verticalAlignment: VerticalAlignment & 'center';
177+
protected handleAlignment = false;
175178

176179
abstract drawOnCanvas(canvas: Canvas, parent: CanvasView): void;
177180

178-
drawMyShapeOnCanvas(canvas: Canvas, parent: CanvasView) {
181+
drawMyShapeOnCanvas(canvas: Canvas, parent: CanvasView, width: number, height: number) {
179182
if (this.visibility !== 'visible') {
180183
return;
181184
}
185+
if (!this.handleAlignment) {
186+
const paddingLeft = parent.effectivePaddingLeft + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderLeftWidth);
187+
const paddingRight = parent.effectivePaddingRight + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderRightWidth);
188+
189+
const paddingTop = parent.effectivePaddingTop + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderTopWidth);
190+
const paddingBottom = parent.effectivePaddingBottom + Utils.layout.toDeviceIndependentPixels(parent.effectiveBorderBottomWidth);
191+
canvas.save();
192+
if (paddingLeft > 0) {
193+
canvas.translate(paddingLeft, 0);
194+
}
195+
if (paddingRight > 0) {
196+
canvas.translate(-paddingRight, 0);
197+
}
198+
if (paddingTop > 0) {
199+
canvas.translate(0, paddingTop);
200+
}
201+
if (paddingBottom > 0) {
202+
canvas.translate(0, -paddingBottom);
203+
}
204+
if (this.horizontalAlignment === 'right') {
205+
canvas.translate(width, 0);
206+
} else if (this.horizontalAlignment === 'center' || this.horizontalAlignment === 'middle') {
207+
canvas.translate(width / 2, 0);
208+
}
209+
if (this.verticalAlignment === 'bottom') {
210+
canvas.translate(0, height);
211+
} else if (this.verticalAlignment === 'center' || this.verticalAlignment === 'middle') {
212+
canvas.translate(0, height / 2);
213+
}
214+
}
215+
182216
const paint = this.paint;
183217
// console.log('drawMyShapeOnCanvas', paint.getColor(), this.strokeColor, this.fillColor);
184218
if (this.strokeColor || this.fillColor) {
@@ -209,5 +243,8 @@ export default abstract class Shape extends Observable {
209243
} else {
210244
this.drawOnCanvas(canvas, parent);
211245
}
246+
if (!this.handleAlignment) {
247+
canvas.restore();
248+
}
212249
}
213250
}

0 commit comments

Comments
 (0)