Skip to content

Commit e17f9f9

Browse files
authored
Merge pull request #57 from nativescript-community/feat-ios-set-matrix
feat(ui-canvas): Added support for iOS Canvas setMatrix
2 parents e7f0f20 + 05a4d49 commit e17f9f9

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/ui-canvas/canvas.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export class Canvas extends ProxyClass {
191191
clipPath(path: Path, op: Op): boolean;
192192
setBitmap(image);
193193
// getSaveCount(): number;
194-
// setMatrix(param0: android.graphics.Matrix): void;
194+
setMatrix(matrix: Matrix): void;
195195
// getClipBounds(param0: android.graphics.Rect): boolean;
196196
// saveLayerAlpha(param0: number, param1: number, param2: number, param3: number, param4: number): number;
197197
// restoreToCount(param0: number): void;

src/ui-canvas/canvas.ios.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-redeclare */
2-
import { Color, Font, ImageSource, View } from '@nativescript/core';
2+
import { Color, Font, ImageSource, Screen, View } from '@nativescript/core';
33
import { FontStyleType, FontWeightType } from '@nativescript/core/ui/styling/font';
44
import {
55
Canvas as ICanvas,
@@ -1450,9 +1450,17 @@ export class Canvas implements ICanvas {
14501450
console.error('Method not implemented:', 'skew');
14511451
}
14521452

1453-
// setMatrix(matrix: Matrix) {
1454-
// CGContextConcatCTM(this.ctx, matrix.mTransform);
1455-
// }
1453+
setMatrix(matrix: Matrix): void {
1454+
// TODO: Find a better way to implement matrix set
1455+
const density = Screen.mainScreen.scale;
1456+
const currentMatrix = this.getMatrix();
1457+
const invertedTransform = CGAffineTransformInvert(currentMatrix.mTransform);
1458+
const scaleTransform = CGAffineTransformMake(density, 0, 0, -density, 0, density * this.getHeight());
1459+
1460+
CGContextConcatCTM(this.ctx, invertedTransform);
1461+
CGContextConcatCTM(this.ctx, scaleTransform);
1462+
CGContextConcatCTM(this.ctx, matrix.mTransform);
1463+
}
14561464
getMatrix(): Matrix {
14571465
return new Matrix(CGContextGetCTM(this.ctx));
14581466
}

0 commit comments

Comments
 (0)