- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
import { Container } from '@pixi/display';
import { autoDetectRenderer, extensions, ExtensionType } from '@pixi/core';
import type { Rectangle } from '@pixi/math';
import type { Renderer, IRendererOptionsAuto, AbstractRenderer } from '@pixi/core';
import type { IDestroyOptions } from '@pixi/display';
import { deprecation } from '@pixi/utils';
/**
* Any plugin that's usable for Application should contain these methods.
* @memberof PIXI
*/
export interface IApplicationPlugin
{
/**
* Called when Application is constructed, scoped to Application instance.
* Passes in `options` as the only argument, which are Application constructor options.
* @param {object} options - Application options.
*/
init(options: IApplicationOptions): void;
/** Called when destroying Application, scoped to Application instance. */
destroy(): void;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IApplicationOptions extends IRendererOptionsAuto, GlobalMixins.IApplicationOptions {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Application extends GlobalMixins.Application {}
/**
* Convenience class to create a new PIXI application.
*
* This class automatically creates the renderer, ticker and root container.
* @example
* // Create the application
* const app = new PIXI.Application();
*
* // Add the view to the DOM
* document.body.appendChild(app.view);
*
* // ex, add display objects
* app.stage.addChild(PIXI.Sprite.from('something.png'));
* @class
* @memberof PIXI
*/
export class Application
{
/** Collection of installed plugins. */
static _plugins: IApplicationPlugin[] = [];
/**
* The root display container that's rendered.
* @member {PIXI.Container}
*/
public stage: Container = new Container();
/**
* WebGL renderer if available, otherwise CanvasRenderer.
* @member {PIXI.Renderer|PIXI.CanvasRenderer}
*/
public renderer: Renderer | AbstractRenderer;
/**
* @param {object} [options] - The optional renderer parameters.
* @param {boolean} [options.autoStart=true] - Automatically starts the rendering after the construction.
* **Note**: Setting this parameter to false does NOT stop the shared ticker even if you set
* options.sharedTicker to true in case that it is already started. Stop it by your own.
* @param {number} [options.width=800] - The width of the renderers view.
* @param {number} [options.height=600] - The height of the renderers view.
* @param {HTMLCanvasElement} [options.view] - The canvas to use as a view, optional.
* @param {boolean} [options.useContextAlpha=true] - Pass-through value for canvas' context `alpha` property.
* If you want to set transparency, please use `backgroundAlpha`. This option is for cases where the
* canvas needs to be opaque, possibly for performance reasons on some older devices.
* @param {boolean} [options.autoDensity=false] - Resizes renderer view in CSS pixels to allow for
* resolutions other than 1.
* @param {boolean} [options.antialias=false] - Sets antialias
* @param {boolean} [options.preserveDrawingBuffer=false] - Enables drawing buffer preservation, enable this if you
* need to call toDataUrl on the WebGL context.
* @param {number} [options.resolution=PIXI.settings.RESOLUTION] - The resolution / device pixel ratio of the renderer.
* @param {boolean} [options.forceCanvas=false] - prevents selection of WebGL renderer, even if such is present, this
* option only is available when using **pixi.js-legacy** or **@pixi/canvas-renderer** modules, otherwise
* it is ignored.
* @param {number} [options.backgroundColor=0x000000] - The background color of the rendered area
* (shown if not transparent).
* @param {number} [options.backgroundAlpha=1] - Value from 0 (fully transparent) to 1 (fully opaque).
* @param {boolean} [options.clearBeforeRender=true] - This sets if the renderer will clear the canvas or
* not before the new render pass.
* @param {string} [options.powerPreference] - Parameter passed to webgl context, set to "high-performance"
* for devices with dual graphics card. **(WebGL only)**.
* @param {boolean} [options.sharedTicker=false] - `true` to use PIXI.Ticker.shared, `false` to create new ticker.
* If set to false, you cannot register a handler to occur before anything that runs on the shared ticker.
* The system ticker will always run before both the shared ticker and the app ticker.
* @param {boolean} [options.sharedLoader=false] - `true` to use PIXI.Loader.shared, `false` to create new Loader.
* @param {Window|HTMLElement} [options.resizeTo] - Element to automatically resize stage to.
*/
constructor(options?: IApplicationOptions)
{
// The default options
options = Object.assign({
forceCanvas: false,
}, options);
this.renderer = autoDetectRenderer(options);
// install plugins here
Application._plugins.forEach((plugin) =>
{
plugin.init.call(this, options);
});
}
/**
* Use the {@link PIXI.extensions.add} API to register plugins.
* @deprecated since 6.5.0
* @static
* @param {PIXI.IApplicationPlugin} plugin - Plugin being installed
*/
static registerPlugin(plugin: IApplicationPlugin): void
{
// #if _DEBUG
deprecation('6.5.0', 'Application.registerPlugin() is deprecated, use extensions.add()');
// #endif
extensions.add({
type: ExtensionType.Application,
ref: plugin,
});
}
/** Render the current stage. */
public render(): void
{
this.renderer.render(this.stage);
}
/**
* Reference to the renderer's canvas element.
* @member {HTMLCanvasElement}
* @readonly
*/
get view(): HTMLCanvasElement
{
return this.renderer.view;
}
/**
* Reference to the renderer's screen rectangle. Its safe to use as `filterArea` or `hitArea` for the whole screen.
* @member {PIXI.Rectangle}
* @readonly
*/
get screen(): Rectangle
{
return this.renderer.screen;
}
/**
* Destroy and don't use after this.
* @param {boolean} [removeView=false] - Automatically remove canvas from DOM.
* @param {object|boolean} [stageOptions] - Options parameter. A boolean will act as if all options
* have been set to that value
* @param {boolean} [stageOptions.children=false] - if set to true, all the children will have their destroy
* method called as well. 'stageOptions' will be passed on to those calls.
* @param {boolean} [stageOptions.texture=false] - Only used for child Sprites if stageOptions.children is set
* to true. Should it destroy the texture of the child sprite
* @param {boolean} [stageOptions.baseTexture=false] - Only used for child Sprites if stageOptions.children is set
* to true. Should it destroy the base texture of the child sprite
*/
public destroy(removeView?: boolean, stageOptions?: IDestroyOptions | boolean): void
{
// Destroy plugins in the opposite order
// which they were constructed
const plugins = Application._plugins.slice(0);
plugins.reverse();
plugins.forEach((plugin) =>
{
plugin.destroy.call(this);
});
this.stage.destroy(stageOptions);
this.stage = null;
this.renderer.destroy(removeView);
this.renderer = null;
}
}
extensions.handleByList(ExtensionType.Application, Application._plugins);