Image
An image used for icons and images across the application.
import { Image } from '@mobrowser/api';
Example
import { Image, Notification } from '@mobrowser/api';
const icon = Image.fromLocalImage(app.getPath('appResources') + '/image.png')
const notification = new Notification({
title: 'Hello',
body: 'This is a notification.',
icon: icon
})
Properties
isEmpty
readonly isEmpty: boolean;
Whether the image has no pixel data.
scaleFactors
readonly scaleFactors: number[];
Device scale factors for which this image has representations (for example [1, 2]).
size
readonly size: Size;
Logical size at scale factor 1.0 (equivalent to getSize(1.0)).
isTemplateImage
readonly isTemplateImage: boolean;
macOS only. When true, the image is drawn as a template (monochrome, tinted by the system).
On other platforms this property is always false.
See Apple’s documentation.
Methods
getSize()
getSize(scaleFactor: number): Size;
Returns the pixel width and height of the image at scaleFactor.
| Parameter | Type | Description |
|---|---|---|
scaleFactor | number | The device scale factor. |
Return value
the pixel width and height of the image at scaleFactor.
Example
import { Image } from '@mobrowser/api';
const image = Image.fromLocalImage(app.getPath('appResources') + '/image.png')
const size = image.getSize(2.0)
asBitmap()
asBitmap(scaleFactor: number): Uint8Array;
Returns premultiplied RGBA pixel data as a Uint8Array at scaleFactor (four bytes per pixel).
| Parameter | Type | Description |
|---|---|---|
scaleFactor | number | The device scale factor. |
Return value
a Uint8Array of premultiplied RGBA pixel data.
Example
import { Image } from '@mobrowser/api';
const image = Image.fromLocalImage(app.getPath('appResources') + '/image.png')
const bitmap = image.asBitmap(1.0)
asDataURL()
asDataURL(scaleFactor: number): string;
Returns a PNG data: URL for the bitmap at scaleFactor.
| Parameter | Type | Description |
|---|---|---|
scaleFactor | number | The device scale factor. |
Return value
a PNG data: URL for the bitmap at scaleFactor.
Example
import { Image } from '@mobrowser/api';
const image = Image.fromLocalImage(app.getPath('appResources') + '/image.png')
const dataURL = image.asDataURL(1.0)
resize()
resize(options: ResizeOptions): Image;
Returns a new image resized according to options. The argument must be a plain object.
| Parameter | Type | Description |
|---|---|---|
options | ResizeOptions | Target dimensions and optional quality. |
Return value
a new image resized according to options.
Example
import { Image } from '@mobrowser/api';
const image = Image.fromLocalImage(app.getPath('appResources') + '/image.png')
const resizedImage = image.resize({ width: 100, height: 100 })
addScaleVariant()
addScaleVariant(options: ImageScaleVariantOptions): void;
Adds another scale representation from buffer or dataURL. At least one of buffer or
dataURL is required. Can be called on an empty image to build a multi-scale image from scratch.
| Parameter | Type | Description |
|---|---|---|
options | ImageScaleVariantOptions | scaleFactor, pixel size when using buffer (see Image.fromBuffer), and image bytes. |
Example
import { Image } from '@mobrowser/api';
const image = Image.empty()
image.addScaleVariant({
scaleFactor: 2.0,
size: { width: 200, height: 200 },
dataURL: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
})
crop()
crop(rectangle: Rect): Image;
Returns a new image containing the axis-aligned region rectangle (in pixel coordinates).
| Parameter | Type | Description |
|---|---|---|
rectangle | Rect | The crop region. |
Return value
a new image containing the axis-aligned region rectangle (in pixel coordinates).
Example
import { Image } from '@mobrowser/api';
const image = Image.fromLocalImage(app.getPath('appResources') + '/image.png')
const croppedImage = image.crop({
origin: { x: 0, y: 0 },
size: { width: 100, height: 100 },
})
setTemplateImage()
setTemplateImage(isTemplateImage: boolean): void;
Sets whether the image is drawn as a template (monochrome, tinted by the system).
On other platforms this property is always false and assigning it has no effect.
See Apple’s documentation.
| Parameter | Type | Description |
|---|---|---|
isTemplateImage | boolean | Whether the image is drawn as a template. |
Example
import { Image } from '@mobrowser/api';
const image = Image.fromLocalImage(app.getPath('appResources') + '/image.png')
image.setTemplateImage(true)