Class: Application

pc.Application

Default application which performs general setup code and initiates the main game loop.

Constructor

new Application(canvas, options)

Create a new Application.
Parameters:
Name Type Description
canvas Element The canvas element
options Object
Properties
Name Type Attributes Description
keyboard pc.Keyboard <optional>
Keyboard handler for input
mouse pc.Mouse <optional>
Mouse handler for input
touch pc.TouchDevice <optional>
TouchDevice handler for input
gamepads pc.GamePads <optional>
Gamepad handler for input
scriptPrefix String <optional>
Prefix to apply to script urls before loading
assetPrefix String <optional>
Prefix to apply to asset urls before loading
graphicsDeviceOptions Object <optional>
Options object that is passed into the pc.GraphicsDevice constructor
Source:
Example
// Create application
var app = new pc.Application(canvas, options);
// Start game loop
app.start()

Members

assets :pc.AssetRegistry

The assets available to the application.
Type:
Source:

autoRender :Boolean

When true (the default) the application's render function is called every frame.
Type:
  • Boolean
Source:

batcher :pc.BatchManager

The Batch Manager of the Application
Type:
Source:

elementInput :pc.ElementInput

Used to handle input for pc.ElementComponents.
Type:
Source:

gamepads :pc.GamePads

Used to access GamePad input.
Type:
Source:

graphicsDevice :pc.GraphicsDevice

The graphics device used by the application.
Type:
Source:

(private) i18n :pc.I18n

Handles localization
Type:
Source:

keyboard :pc.Keyboard

The keyboard device.
Type:
Source:

loader :pc.ResourceLoader

The resource loader.
Type:
Source:

maxDeltaTime :Number

Clamps per-frame delta time to an upper bound. Useful since returning from a tab deactivation can generate huge values for dt, which can adversely affect game state. Defaults to 0.1 (seconds).
Type:
  • Number
Source:

mouse :pc.Mouse

The mouse device.
Type:
Source:

(private) onLibrariesLoaded

Event handler called when all code libraries have been loaded Code libraries are passed into the constructor of the Application and the application won't start running or load packs until all libraries have been loaded
Source:

renderNextFrame :Boolean

If pc.Application#autoRender is false, set `app.renderNextFrame` true to force application to render the scene once next frame.
Type:
  • Boolean
Source:
Example
// render the scene only while space key is pressed
if (this.app.keyboard.isPressed(pc.KEY_SPACE)) {
   this.app.renderNextFrame = true;
}

root :pc.Entity

The root pc.Entity of the application.
Type:
Source:

scene :pc.Scene

The current pc.Scene
Type:
Source:

scripts :pc.ScriptRegistry

The Script Registry of the Application
Type:
Source:

systems :pc.ComponentSystemRegistry

The component systems.
Type:
Source:

timeScale :Number

Scales the global time delta.
Type:
  • Number
Source:

touch :pc.TouchDevice

Used to get touch events input.
Type:
Source:

Methods

configure(url, callback)

Load the application configuration file and apply application properties and fill the asset registry
Parameters:
Name Type Description
url String The URL of the configuration file to load
callback function The Function called when the configuration file is loaded and parsed
Source:

destroy()

Destroys application and removes all event listeners.
Source:

getSceneUrl(name) → {String}

Look up the URL of the scene hierarchy file via the name given to the scene in the editor. Use this to in pc.Application#loadSceneHierarchy.
Parameters:
Name Type Description
name String The name of the scene file given in the Editor
Source:
Returns:
The URL of the scene file
Type
String

isHidden() → {Boolean}

Queries the visibility of the window or tab in which the application is running.
Source:
Returns:
True if the application is not visible and false otherwise.
Type
Boolean

loadSceneHierarchy(url, callback)

Load a scene file, create and initialize the Entity hierarchy and add the hierarchy to the application root Entity.
Parameters:
Name Type Description
url String The URL of the scene file. Usually this will be "scene_id.json"
callback function The function to call after loading, passed (err, entity) where err is null if no errors occurred.
Source:
Example
app.loadSceneHierarchy("1000.json", function (err, entity) {
    if (!err) {
      var e = app.root.find("My New Entity");
    } else {
      // error
    }
  }
});

loadSceneSettings(url, callback)

Load a scene file and apply the scene settings to the current scene
Parameters:
Name Type Description
url String The URL of the scene file. Usually this will be "scene_id.json"
callback function The function called after the settings are applied. Passed (err) where err is null if no error occurred.
Source:
Example
app.loadSceneSettings("1000.json", function (err) {
    if (!err) {
      // success
    } else {
      // error
    }
  }
});

(private) onVisibilityChange()

Called when the visibility state of the current tab/window changes
Source:

preload(callback)

Load all assets in the asset registry that are marked as 'preload'
Parameters:
Name Type Description
callback function Function called when all assets are loaded
Source:

render()

Application specific render method. Override this if you have a custom Application
Source:

renderLine(start, end, color, endColoropt, optionsopt)

Renders a line. Line start and end coordinates are specified in world-space. If a single color is supplied, the line will be flat-shaded with that color. If two colors are supplied, the line will be smooth shaded between those colors. It is also possible to control which scene layer the line is rendered into. By default, lines are rendered into the immediate layer pc.LAYERID_IMMEDIATE.
Parameters:
Name Type Attributes Description
start pc.Vec3 The start world-space coordinate of the line.
end pc.Vec3 The end world-space coordinate of the line.
color pc.Color The start color of the line.
endColor pc.Color <optional>
The end color of the line.
options Object <optional>
Options to set rendering properties
Properties
Name Type Attributes Description
layer pc.Layer <optional>
The layer to render the line into. Defaults to pc.LAYERID_IMMEDIATE.
Source:
Examples
// Render a 1-unit long white line
var start = new pc.Vec3(0, 0, 0);
var end = new pc.Vec3(1, 0, 0);
var color = new pc.Color(1, 1, 1);
app.renderLine(start, end, color);
// Render a 1-unit long line that is smooth-shaded from white to red
var start = new pc.Vec3(0, 0, 0);
var end = new pc.Vec3(1, 0, 0);
var startColor = new pc.Color(1, 1, 1);
var endColor = new pc.Color(1, 0, 0);
app.renderLine(start, end, startColor, endColor);
// Render a 1-unit long white line into the world layer
var start = new pc.Vec3(0, 0, 0);
var end = new pc.Vec3(1, 0, 0);
var color = new pc.Color(1, 1, 1);
var worldLayer = app.scene.layers.getLayerById(pc.LAYERID_WORLD);
app.renderLine(start, end, color, {
    layer: worldLayer
});
// Render a 1-unit long line that is smooth-shaded from white to red into the world layer
var start = new pc.Vec3(0, 0, 0);
var end = new pc.Vec3(1, 0, 0);
var startColor = new pc.Color(1, 1, 1);
var endColor = new pc.Color(1, 0, 0);
var worldLayer = app.scene.layers.getLayerById(pc.LAYERID_WORLD);
app.renderLine(start, end, color, {
    layer: worldLayer
});

renderLines(position, color, optionsopt)

Draw an array of lines.
Parameters:
Name Type Attributes Description
position Array.<pc.Vec3> An array of points to draw lines between
color Array.<pc.Color> An array of colors to color the lines. This must be the same size as the position array
options Object <optional>
Options to set rendering properties
Properties
Name Type Attributes Description
layer pc.Layer <optional>
The layer to render the line into
Source:
Example
var points = [new pc.Vec3(0,0,0), new pc.Vec3(1,0,0), new pc.Vec3(1,1,0), new pc.Vec3(1,1,1)];
var colors = [new pc.Color(1,0,0), new pc.Color(1,1,0), new pc.Color(0,1,1), new pc.Color(0,0,1)];
app.renderLines(points, colors);

resizeCanvas(widthopt, heightopt) → {Object}

Resize the canvas in line with the current FillMode In KEEP_ASPECT mode, the canvas will grow to fill the window as best it can while maintaining the aspect ratio In FILL_WINDOW mode, the canvas will simply fill the window, changing aspect ratio In NONE mode, the canvas will always match the size provided
Parameters:
Name Type Attributes Description
width Number <optional>
The width of the canvas, only used in NONE mode
height Number <optional>
The height of the canvas, only used in NONE mode
Source:
Returns:
A object containing the values calculated to use as width and height
Type
Object

setCanvasFillMode(mode, widthopt, heightopt)

Controls how the canvas fills the window and resizes when the window changes.
Parameters:
Name Type Attributes Description
mode String The mode to use when setting the size of the canvas. Can be:
  • pc.FILLMODE_NONE: the canvas will always match the size provided.
  • pc.FILLMODE_FILL_WINDOW: the canvas will simply fill the window, changing aspect ratio.
  • pc.FILLMODE_KEEP_ASPECT: the canvas will grow to fill the window as best it can while maintaining the aspect ratio.
width Number <optional>
The width of the canvas (only used when mode is pc.FILLMODE_NONE).
height Number <optional>
The height of the canvas (only used when mode is pc.FILLMODE_NONE).
Source:

setCanvasResolution(mode, widthopt, heightopt)

Change the resolution of the canvas, and set the way it behaves when the window is resized
Parameters:
Name Type Attributes Description
mode String The mode to use when setting the resolution. Can be:
  • pc.RESOLUTION_AUTO: if width and height are not provided, canvas will be resized to match canvas client size.
  • pc.RESOLUTION_FIXED: resolution of canvas will be fixed.
width Number <optional>
The horizontal resolution, optional in AUTO mode, if not provided canvas clientWidth is used
height Number <optional>
The vertical resolution, optional in AUTO mode, if not provided canvas clientHeight is used
Source:

setSkybox(asset)

Sets the skybox asset to current scene, and subscribes to asset load/change events
Parameters:
Name Type Description
asset pc.Asset Asset of type `skybox` to be set to, or null to remove skybox
Source:

start()

Start the Application updating
Source:

update(dt)

Application specific update method. Override this if you have a custom Application
Parameters:
Name Type Description
dt Number The time delta since the last frame.
Source: