Skip to content

Commit

Permalink
Merge pull request #57 from nativescript-community/feat-ios-set-matrix
Browse files Browse the repository at this point in the history
feat(ui-canvas): Added support for iOS Canvas setMatrix
  • Loading branch information
farfromrefug authored Jul 26, 2024
2 parents e7f0f20 + 05a4d49 commit e17f9f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ui-canvas/canvas.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class Canvas extends ProxyClass {
clipPath(path: Path, op: Op): boolean;
setBitmap(image);
// getSaveCount(): number;
// setMatrix(param0: android.graphics.Matrix): void;
setMatrix(matrix: Matrix): void;
// getClipBounds(param0: android.graphics.Rect): boolean;
// saveLayerAlpha(param0: number, param1: number, param2: number, param3: number, param4: number): number;
// restoreToCount(param0: number): void;
Expand Down
16 changes: 12 additions & 4 deletions src/ui-canvas/canvas.ios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-redeclare */
import { Color, Font, ImageSource, View } from '@nativescript/core';
import { Color, Font, ImageSource, Screen, View } from '@nativescript/core';
import { FontStyleType, FontWeightType } from '@nativescript/core/ui/styling/font';
import {
Canvas as ICanvas,
Expand Down Expand Up @@ -1450,9 +1450,17 @@ export class Canvas implements ICanvas {
console.error('Method not implemented:', 'skew');
}

// setMatrix(matrix: Matrix) {
// CGContextConcatCTM(this.ctx, matrix.mTransform);
// }
setMatrix(matrix: Matrix): void {
// TODO: Find a better way to implement matrix set
const density = Screen.mainScreen.scale;
const currentMatrix = this.getMatrix();
const invertedTransform = CGAffineTransformInvert(currentMatrix.mTransform);
const scaleTransform = CGAffineTransformMake(density, 0, 0, -density, 0, density * this.getHeight());

CGContextConcatCTM(this.ctx, invertedTransform);
CGContextConcatCTM(this.ctx, scaleTransform);
CGContextConcatCTM(this.ctx, matrix.mTransform);
}
getMatrix(): Matrix {
return new Matrix(CGContextGetCTM(this.ctx));
}
Expand Down

0 comments on commit e17f9f9

Please sign in to comment.