Skip to content

Commit d8c7e98

Browse files
authored
Merge pull request #62 from nativescript-community/fix-ios-set-matrix
fix(ui-canvas): Proper iOS scale for bitmap cases
2 parents ad59928 + b0affa0 commit d8c7e98

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/ui-canvas/canvas.ios.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,8 +1361,26 @@ export class Canvas implements ICanvas {
13611361
mWidth: number;
13621362
mHeight: number;
13631363
mScale = 1;
1364+
mIsBitmap = false;
13641365
view: WeakRef<CanvasView>;
13651366

1367+
constructor(imageOrWidth: ImageSource | UIImage | number, height?: number) {
1368+
let isBitmap = true;
1369+
1370+
if (imageOrWidth instanceof ImageSource) {
1371+
this.mCgContext = this._createContextFromImage(imageOrWidth.ios);
1372+
} else if (imageOrWidth instanceof UIImage) {
1373+
this.mCgContext = this._createContextFromImage(imageOrWidth);
1374+
} else if (imageOrWidth > 0 && height > 0) {
1375+
this.mCgContext = this._createContext(imageOrWidth, height);
1376+
} else {
1377+
isBitmap = false;
1378+
}
1379+
1380+
this.mIsBitmap = isBitmap;
1381+
// CGContextFillRect(this._cgContext);
1382+
}
1383+
13661384
setBitmap(image) {
13671385
// if (image instanceof ImageSource) {
13681386
// this._bitmap = image.android;
@@ -1454,7 +1472,7 @@ export class Canvas implements ICanvas {
14541472
setMatrix(matrix: Matrix): void {
14551473
// TODO: Find a better way to implement matrix set
14561474
const ctx = this.ctx;
1457-
const density = Screen.mainScreen.scale;
1475+
const density = this.mIsBitmap ? 1 : Screen.mainScreen.scale;
14581476
const currentMatrix = this.getMatrix();
14591477
const invertedTransform = CGAffineTransformInvert(currentMatrix.mTransform);
14601478
const scaleTransform = CGAffineTransformMake(density, 0, 0, -density, 0, density * this.mHeight);
@@ -1765,16 +1783,6 @@ export class Canvas implements ICanvas {
17651783
getHeight() {
17661784
return this.mHeight;
17671785
}
1768-
constructor(imageOrWidth: ImageSource | UIImage | number, height?: number) {
1769-
if (imageOrWidth instanceof ImageSource) {
1770-
this.mCgContext = this._createContextFromImage(imageOrWidth.ios);
1771-
} else if (imageOrWidth instanceof UIImage) {
1772-
this.mCgContext = this._createContextFromImage(imageOrWidth);
1773-
} else if (imageOrWidth > 0 && height > 0) {
1774-
this.mCgContext = this._createContext(imageOrWidth, height);
1775-
}
1776-
// CGContextFillRect(this._cgContext);
1777-
}
17781786

17791787
startApplyPaint(paint?: Paint, withFont = false) {
17801788
this.save();

0 commit comments

Comments
 (0)