Skip to content

Commit b0affa0

Browse files
committed
fix: Different matrix scale for bitmap and view cgcontext
1 parent 31ad4a8 commit b0affa0

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/ui-canvas/canvas.ios.ts

Lines changed: 21 additions & 14 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,14 +1472,13 @@ 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;
1475+
const density = this.mIsBitmap ? 1 : Screen.mainScreen.scale;
14571476
const currentMatrix = this.getMatrix();
14581477
const invertedTransform = CGAffineTransformInvert(currentMatrix.mTransform);
1459-
// Screen scale is excluded because it causes problems for other cases (e.g. bitmaps)
1460-
// Android canvas scale has to be re-applied after setMatrix() call too so this is going to make iOS behavior similar
1461-
const flipTransform = CGAffineTransformMake(1, 0, 0, -1, 0, this.mHeight);
1478+
const scaleTransform = CGAffineTransformMake(density, 0, 0, -density, 0, density * this.mHeight);
14621479

14631480
CGContextConcatCTM(ctx, invertedTransform);
1464-
CGContextConcatCTM(ctx, flipTransform);
1481+
CGContextConcatCTM(ctx, scaleTransform);
14651482
CGContextConcatCTM(ctx, matrix.mTransform);
14661483
}
14671484
getMatrix(): Matrix {
@@ -1766,16 +1783,6 @@ export class Canvas implements ICanvas {
17661783
getHeight() {
17671784
return this.mHeight;
17681785
}
1769-
constructor(imageOrWidth: ImageSource | UIImage | number, height?: number) {
1770-
if (imageOrWidth instanceof ImageSource) {
1771-
this.mCgContext = this._createContextFromImage(imageOrWidth.ios);
1772-
} else if (imageOrWidth instanceof UIImage) {
1773-
this.mCgContext = this._createContextFromImage(imageOrWidth);
1774-
} else if (imageOrWidth > 0 && height > 0) {
1775-
this.mCgContext = this._createContext(imageOrWidth, height);
1776-
}
1777-
// CGContextFillRect(this._cgContext);
1778-
}
17791786

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

0 commit comments

Comments
 (0)