Skip to content

Commit b18fa4a

Browse files
committed
feat: BitmapShader
1 parent 2cad4a8 commit b18fa4a

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/canvas.android.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,31 @@ export class LinearGradient {
445445
}
446446
}
447447

448+
export class BitmapShader {
449+
_native: android.graphics.BitmapShader;
450+
getNative() {
451+
return this._native;
452+
}
453+
constructor(bitmap: any, tileX: any, tileY: any) {
454+
if (bitmap instanceof ImageSource) {
455+
bitmap = bitmap.android;
456+
}
457+
this._native = new android.graphics.BitmapShader(bitmap, tileX, tileY);
458+
return new Proxy(this, this);
459+
}
460+
get(target, name, receiver) {
461+
const native = this._native;
462+
if (native[name]) {
463+
return function (...args) {
464+
const methodName = name;
465+
return native[methodName](...args);
466+
};
467+
} else {
468+
return Reflect.get(target, name, receiver);
469+
}
470+
}
471+
}
472+
448473
export class StaticLayout {
449474
_native: android.text.StaticLayout;
450475
getNative() {

src/canvas.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class Paint {
8888
// }
8989
export class StaticLayout extends android.text.StaticLayout {
9090
constructor(text: any, paint: Paint, width: number, align, spacingmult, spacingadd, includepad);
91-
public draw(canvas: Canvas): void;
91+
public draw(canvas: any, param1?: Path, param2?: Paint, param3: number): void;
9292
}
9393

9494
export class FontMetrics {
@@ -235,6 +235,7 @@ export class RectF {
235235
}
236236
export class RadialGradient extends android.graphics.RadialGradient {}
237237
export class LinearGradient extends android.graphics.LinearGradient {}
238+
export class BitmapShader extends android.graphics.BitmapShader {}
238239
export class TileMode extends android.graphics.Shader.TileMode {}
239240
export class Path {
240241
computeBounds(rect: RectF, exact: boolean);

src/canvas.ios.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,25 @@ export class Paint implements IPaint {
11851185
CGContextDrawRadialGradient(ctx, g.gradient, CGPointMake(g.centerX, g.centerY), 0, CGPointMake(g.centerX, g.centerY), g.radius, options);
11861186
CGContextRestoreGState(ctx);
11871187
// CGContextAddPath(ctx, path);
1188+
} else if (this.shader instanceof BitmapShader) {
1189+
const color = UIColor.clearColor;
1190+
CGContextSetFillColorWithColor(ctx, color.CGColor);
1191+
CGContextSetStrokeColorWithColor(ctx, color.CGColor);
1192+
const g = this.shader;
1193+
const source: UIImage = g.image;
1194+
1195+
const w = source.size.width;
1196+
const h = source.size.height;
1197+
const rect = CGRectMake(0, 0, w, h);
1198+
1199+
// draw original image
1200+
CGContextSaveGState(ctx);
1201+
CGContextClip(ctx);
1202+
CGContextTranslateCTM(ctx, 0, source.size.height);
1203+
CGContextScaleCTM(ctx, 1.0, -1.0);
1204+
CGContextDrawImage(ctx, rect, source.CGImage);
1205+
CGContextRestoreGState(ctx);
1206+
// CGContextAddPath(ctx, path);
11881207
}
11891208
}
11901209
_textAttribs: NSMutableDictionary<any, any>;
@@ -1780,7 +1799,14 @@ export class Canvas implements ICanvas {
17801799
}
17811800

17821801
if (path && paint.shader) {
1783-
CGContextAddPath(ctx, path);
1802+
if (paint.style === Style.STROKE) {
1803+
const cgStrokedPath = CGPathCreateCopyByStrokingPath(path, null,
1804+
paint.strokeWidth, paint.strokeCap as any, paint.strokeJoin as any, 0);
1805+
CGContextAddPath(ctx, cgStrokedPath);
1806+
} else {
1807+
CGContextAddPath(ctx, path);
1808+
1809+
}
17841810
paint.drawShader(ctx);
17851811
} else {
17861812
if (bPath) {
@@ -2134,6 +2160,17 @@ export class RadialGradient {
21342160
}
21352161
}
21362162
}
2163+
export class BitmapShader {
2164+
constructor(public bitmap: any, public tileX: any, public tileY: any) {}
2165+
get image() {
2166+
if (this.bitmap instanceof ImageSource) {
2167+
return this.bitmap.ios;
2168+
}
2169+
return this.bitmap;
2170+
}
2171+
release() {
2172+
}
2173+
}
21372174
export class PorterDuffXfermode {
21382175
constructor(public mode?: number) {}
21392176
}

0 commit comments

Comments
 (0)