Namespace: utils

utils

A collection of utility functions used by Pixi, but also handy for your games and applications.

import { isWebGLSupported } from 'pixi.js';

if (isWebGLSupported()) {
   // WebGL is supported, proceed!
}

Classes

EventEmitter
Pool
PoolGroupClass
Transform
ViewableBuffer

Interface Definitions

Path

Path utilities for working with URLs and file paths in a cross-platform way. All paths that are passed in will become normalized to have posix separators.

import { path } from 'pixi.js';

path.normalize('http://www.example.com/foo/bar/../baz'); // http://www.example.com/foo/baz
Properties:
Name Type Description
basename (path: string, ext: string) => string

Returns the last portion of a path

dirname (path: string) => string

Returns the directory name of a path

extname (path: string) => string

Returns the extension of the path, from the last occurrence of the . (period) character to end of string in the last portion of the path. If there is no . in the last portion of the path, or if there are no . characters other than the first character of the basename of path, an empty string is returned.

getProtocol (path: string) => string

Returns the protocol of the path e.g. http://, https://, file:///, data:, blob:, C:/

hasProtocol (path: string) => boolean

Checks if the path has a protocol e.g. http://, https://, file:///, data:, blob:, C:/ This will return true for windows file paths

isAbsolute (path: string) => boolean

Determines if path is an absolute path. Absolute paths can be urls, data urls, or paths on disk

isBlobUrl (path: string) => boolean

Checks if the path is a blob URL

isDataUrl (path: string) => boolean

Checks if the path is a data URL

isUrl (path: string) => boolean

Checks if the path is a URL e.g. http://, https://

join (...paths: string[]) => string

Joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path

normalize (path: string) => string

Normalizes the given path, resolving '..' and '.' segments

parse (path: string) => { root? : string, dir? : string, base? : string, ext? : string, name? : string }

Parses a path into an object containing the 'root', dir, base, ext, and name properties.

rootname (path: string) => string

Returns the root of the path e.g. /, C:/, file:///, http://domain.com/

toAbsolute (url: string, baseUrl: string, rootUrl: string) => string

Converts URL to an absolute path. When loading from a Web Worker, we must use absolute paths. If the URL is already absolute we return it as is If it's not, we convert it

toPosix (path: string) => string

Converts a path to posix format.

Members

DATA_URI RegExp staticreadonly

Regexp for data URI. Based on: https://github.com/ragingwind/data-uri-regex

Default Value:
  • /(?:^data:image\/([\w+]+);(?:[\w=]+|charset=[\w-]+)?(?:;base64)?,)/i
Example
 import { DATA_URI } from 'pixi.js';

 DATA_URI.test('data:image/png;base64,foobar'); // => true

isMobile readonly

Detects whether the device is mobile and what type of mobile device it is.

import { isMobile } from 'pixi.js';

if (isMobile.apple.tablet) {
   // The device is an Apple tablet device.
}
Properties:
Name Type Description
amazon {
   phone: boolean,
   tablet: boolean,
   device: boolean
}

Whether the device is an Amazon device.

android {
   phone: boolean,
   tablet: boolean,
   device: boolean
}

Whether the device is an Android device.

any boolean

Whether the device is any kind of device.

apple {
   phone: boolean,
   ipod: boolean,
   tablet: boolean,
   universal: boolean,
   device: boolean
}

Whether the device is an Apple device.

other {
   blackberry: boolean,
   blackberry10: boolean,
   opera: boolean,
   firefox: boolean,
   chrome: boolean,
   device: boolean
}

Whether the device is a specific device.

phone boolean

Whether the device is a phone device.

tablet boolean

Whether the device is a tablet device.

windows {
   phone: boolean,
   tablet: boolean,
   device: boolean
}

Whether the device is a Windows device.

path readonly

Path utilities for working with URLs and file paths in a cross-platform way. All paths that are passed in will become normalized to have posix separators.

import { path } from 'pixi.js';

path.normalize('http://www.example.com/foo/bar/../baz'); // http://www.example.com/foo/baz
See:

Type Definitions

PoolConstructor

A type alias for a constructor of a Pool.

PoolItem

An object that can be stored in a Pool.

PoolItemConstructor

The constructor of an object that can be stored in a Pool.

Methods

earcut (vertices, holes, dimensions) Array<number>

A polygon triangulation library

Name Type Attributes Default Description
vertices Array<number>

A flat array of vertex coordinates

holes Array<number> <optional>

An array of hole indices

dimensions number <optional>
2

The number of coordinates per vertex in the input array

See:
Returns:
Type Description
Array<number> Triangulated polygon

detectVideoAlphaMode () Promise<ALPHA_MODES>

Helper for detecting the correct alpha mode for video textures. For some reason, some browsers/devices/WebGL implementations premultiply the alpha of a video before and then a second time if UNPACK_PREMULTIPLY_ALPHA_WEBGL is true. So the video is premultiplied twice if the alpha mode is UNPACK. In this case we need the alpha mode to be PMA. This function detects the upload behavior by uploading a white 2x2 webm with 50% alpha without UNPACK_PREMULTIPLY_ALPHA_WEBGL and then checking whether the uploaded pixels are premultiplied.

Returns:
Type Description
Promise<ALPHA_MODES> The correct alpha mode for video textures.

getCanvasBoundingBox (canvas, resolution) Rectangle

Measuring the bounds of a canvas' visible (non-transparent) pixels.

Name Type Default Description
canvas ICanvas

The canvas to measure.

resolution number 1

The resolution of the canvas.

Since:
  • 7.1.0
Returns:
Type Description
Rectangle The bounding box of the canvas' visible pixels.

getResolutionOfUrl (url, defaultValue) number

get the resolution / device pixel ratio of an asset by looking for the prefix used by spritesheets and image urls

Name Type Attributes Default Description
url string

the image path

defaultValue number <optional>
1

the defaultValue if no filename prefix is set.

Returns:
Type Description
number resolution / device pixel ratio of an asset

isWebGLSupported (failIfMajorPerformanceCaveat) boolean

Helper for checking for WebGL support.

Name Type Attributes Description
failIfMajorPerformanceCaveat boolean <optional>

whether to fail if there is a major performance caveat, defaults to false

Returns:
Type Description
boolean Is WebGL supported.

isWebGPUSupported (options) Promise<boolean>

Helper for checking for WebGPU support.

Name Type Description
options GPURequestAdapterOptions

The options for requesting a GPU adapter.

Returns:
Type Description
Promise<boolean> Is WebGPU supported.

removeItems (arr, startIdx, removeCount) void

Remove items from a javascript array without generating garbage

Name Type Description
arr Array<any>

Array to remove elements from

startIdx number

starting index

removeCount number

how many to remove

uid (name) number

Gets the next unique identifier

Name Type Default Description
name UIDNames 'default'

The name of the identifier.

Returns:
Type Description
number The next unique identifier to use.