NativeScript Bitmap Factory
A NativeScript module for creating and manipulating bitmap images.
NativeScript 8
This will work only on NativeScript 8.
NativeScript Toolbox
The original module is part of nativescript-toolbox.
Platforms
Installation
ns plugin add @oliverphaser/nativescript-bitmap-factory
Usage
import BitmapFactory = require("@oliverphaser/nativescript-bitmap-factory");
import KnownColors = require("@nativescript/core/color/known-colors");
var bmp = BitmapFactory.create(640, 480);
bmp.dispose(() => {
bmp.drawOval("300x150", "0,75",
KnownColors.Red, KnownColors.Black);
bmp.drawCircle(80, null,
KnownColors.DarkGreen);
bmp.drawArc("100x200", "10,20",
0, 90,
KnownColors.Black, KnownColors.Yellow);
bmp.setPoint("160,150", "ff0");
bmp.drawLine("0,150", "300,75", '#0000ff');
bmp.writeText("This is a test!", "100,100", {
color: KnownColors.Yellow,
size: 10,
name: "Roboto"
});
var data = bmp.toDataUrl(BitmapFactory.OutputFormat.JPEG, 75);
var base64JPEG = bmp.toBase64(BitmapFactory.OutputFormat.JPEG, 75);
var imgSrc = bmp.toImageSource();
});
Functions
Name | Description |
---|
asBitmap | Returns a value as wrapped bitmap. |
create | Creates a new bitmap instance. |
getDefaultOptions | Returns the default options for creating a new bitmap. |
makeMutable | A helper function that keeps sure to return a native image object that is able to be used as wrapped bitmap object. |
setDefaultOptions | Sets the default options for creating a new bitmap. |
Platform specific stuff
You can access the nativeObject
property to access the platform specific object.
For Android this is a Bitmap object and for iOS this is an UIImage object.
To check the platform you can use the android
and ios
properties which have the same values as the corresponding properties from application
core module.
Android
You also can access the underlying Canvas object by __canvas
property.
iOS
You also can access the underlying CGImage object by__CGImage
property.
Data types
IArgb
Stores data of an RGB value with alpha value.
These values can also be submitted as strings (like #ff0
or ffffff
) or numbers.
interface IArgb {
a: number;
r: number;
g: number;
b: number;
}
IBitmapData
Used by toObject()
method.
interface IBitmapData {
base64: string;
mime: string;
}
IFont
Font settings.
interface IFont {
antiAlias?: boolean;
color?: string | number | IArgb;
name?: string;
size?: number;
}
IPoint2D
Coordinates, can also be a string like 0,0
or 150|300
.
interface IPoint2D {
x: number;
y: number;
}
IPoint2D
Size, can also be a string like 0,0
or 150x300
.
interface ISize {
height: number;
width: number;
}
OutputFormat
Supported output formats.
enum OutputFormat {
PNG = 1,
JPEG = 2,
}
Bitmap
interface IBitmap {
android: AndroidApplication;
clone(): IBitmap;
crop(leftTop?: IPoint2D | string,
size?: ISize | string): IBitmap;
defaultColor: IPoint2D | string | number;
dispose<T, TResult>(action?: (bmp: IBitmap, tag?: T) => TResult,
tag?: T): TResult;
drawCircle(radius?: number,
center?: IPoint2D | string,
color?: string | number | IArgb, fillColor?: string | number | IArgb): IBitmap;
drawArc(size?: ISize | string,
leftTop?: IPoint2D | string,
startAngle?: number,
sweepAngle?: number,
color?: string | number | IArgb, fillColor?: string | number | IArgb): IBitmap;
drawLine(start: IPoint2D | string, end: IPoint2D | string,
color?: string | number | IArgb): IBitmap;
drawOval(size?: ISize | string,
leftTop?: IPoint2D | string,
color?: string | number | IArgb, fillColor?: string | number | IArgb): IBitmap;
drawRect(size?: ISize | string,
leftTop?: IPoint2D | string,
color?: string | number | IArgb, fillColor?: string | number | IArgb): IBitmap;
getPoint(coordinates?: IPoint2D | string): IArgb;
height: number;
ios: iOSApplication;
insert(other: any,
leftTop?: IPoint2D | string): IBitmap;
isDisposed: boolean;
nativeObject: any;
normalizeColor(value: string | number | IArgb): IArgb;
resize(newSize: ISize | string): IBitmap;
resizeHeight(newHeight: number): IBitmap;
resizeMax(maxSize: number): IBitmap;
resizeWidth(newWidth: number): IBitmap;
rotate(degrees?: number): IBitmap;
setPoint(coordinates?: IPoint2D | string,
color?: string | number | IArgb): IBitmap;
size: ISize;
toBase64(format?: OutputFormat, quality?: number): string;
toDataUrl(format?: OutputFormat, quality?: number): string;
toImageSource(): ImageSource;
toObject(format?: OutputFormat, quality?: number): IBitmapData;
writeText(txt: any,
leftTop?: IPoint2D | string, font?: IFont | string): IBitmap;
width: number;
}
Contributors