API Reference

Class List

pc.Application

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

// Create application
var app = new pc.Application(canvas, options);
// Start game loop
app.start()

Summary

Properties

assetsThe assets available to the application.
autoRenderWhen true (the default) the application's render function is called every frame.
gamepadsUsed to access GamePad input.
graphicsDeviceThe graphics device used by the application.
keyboardThe keyboard device.
loaderThe resource loader.
mouseThe mouse device.
renderNextFrameIf pc.Application#autoRender is false, set `app.
rootThe root pc.Entity of the application.
sceneThe current pc.Scene
scriptsThe Script Registry of the Application
systemsThe component systems.
timeScaleScales the global time delta.
touchUsed to get touch events input.

Methods

configureLoad the application configuration file and apply application properties and fill the asset registry
destroyDestroys application and removes all event listeners.
disableFullscreenIf application is currently displaying an element as fullscreen, then stop and return to normal.
enableFullscreenRequest that the browser enters fullscreen mode.
isFullscreenReturns true if the application is currently running fullscreen
isHiddenQueries the visibility of the window or tab in which the application is running.
loadSceneHierarchyLoad a scene file, create and initialize the Entity hierarchy and add the hierarchy to the application root Entity.
loadSceneSettingsLoad a scene file and apply the scene settings to the current scene
preloadLoad all assets in the asset registry that are marked as 'preload'
renderApplication specific render method.
renderLineDraw a line in one color
renderLine^2Draw a line which blends between two colors
renderLine^3Draw a line of one color with specified line type
renderLine^4Draw a line which blends between two colors with specified line type
renderLinesDraw an array of lines.
resizeCanvasResize 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
setCanvasFillModeControls how the canvas fills the window and resizes when the window changes.
setCanvasResolutionChange the resolution of the canvas, and set the way it behaves when the window is resized
startStart the Application updating
updateApplication specific update method.

Details

Constructor

Application(canvas, options)

Create a new Application.

// Create application
var app = new pc.Application(canvas, options);
// Start game loop
app.start()

Parameters

canvasElementThe canvas element
optionsObject
options.keyboardpc.KeyboardKeyboard handler for input
options.mousepc.MouseMouse handler for input
options.touchpc.TouchDeviceTouchDevice handler for input
options.gamepadspc.GamePadsGamepad handler for input
options.scriptPrefixStringPrefix to apply to script urls before loading
options.assetPrefixStringPrefix to apply to asset urls before loading
options.graphicsDeviceOptionsObjectOptions object that is passed into the pc.GraphicsDevice constructor

Properties

pc.AssetRegistryassets

The assets available to the application.

BooleanautoRender

When true (the default) the application's render function is called every frame.

pc.GamePadsgamepads

Used to access GamePad input.

pc.GraphicsDevicegraphicsDevice

The graphics device used by the application.

pc.Keyboardkeyboard

The keyboard device.

pc.ResourceLoaderloader

The resource loader.

pc.Mousemouse

The mouse device.

BooleanrenderNextFrame

If pc.Application#autoRender is false, set `app.renderNextFrame` true to force application to render the scene once next frame.

// render the scene only while space key is pressed
if (this.app.keyboard.isPressed(pc.KEY_SPACE)) {
   this.app.renderNextFrame = true;
}
pc.Entityroot

The root pc.Entity of the application.

pc.Scenescene

The current pc.Scene

pc.ScriptRegistryscripts

The Script Registry of the Application

pc.ComponentSystem[]systems

The component systems.

NumbertimeScale

Scales the global time delta.

pc.TouchDevicetouch

Used to get touch events input.

Methods

configure(url, callback)

Load the application configuration file and apply application properties and fill the asset registry

Parameters

urlStringThe URL of the configuration file to load
callbackfunctionThe Function called when the configuration file is loaded and parsed

destroy()

Destroys application and removes all event listeners.

disableFullscreen([success])

If application is currently displaying an element as fullscreen, then stop and return to normal.

Parameters

successfunctionFunction called when transition to normal mode is finished

enableFullscreen([element], [success], [error])

Request that the browser enters fullscreen mode. This is not available on all browsers. Note: Switching to fullscreen can only be initiated by a user action, e.g. in the event hander for a mouse or keyboard input

var button = document.getElementById('my-button');
button.addEventListener('click', function () {
    app.enableFullscreen(canvas, function () {
        console.log('Now fullscreen');
    }, function () {
        console.log('Something went wrong!');
    });
}, false);

Parameters

elementElementThe element to display in fullscreen, if element is not provided the application canvas is used
successfunctionFunction called if the request for fullscreen was successful
errorfunctionFunction called if the request for fullscreen was unsuccessful

isFullscreen()

Returns true if the application is currently running fullscreen

Returns

Boolean True if the application is running fullscreen

isHidden()

Queries the visibility of the window or tab in which the application is running.

Returns

Boolean True if the application is not visible and false otherwise.

loadSceneHierarchy(url, callback)

Load a scene file, create and initialize the Entity hierarchy and add the hierarchy to the application root Entity.

app.loadSceneHierarchy("1000.json", function (err, entity) {
    if (!err) {
      var e = app.root.find("My New Entity");
    } else {
      // error
    }
  }
});

Parameters

urlStringThe URL of the scene file. Usually this will be "scene_id.json"
callbackfunctionThe function to call after loading, passed (err, entity) where err is null if no errors occurred.

loadSceneSettings(url, callback)

Load a scene file and apply the scene settings to the current scene

app.loadSceneSettings("1000.json", function (err) {
    if (!err) {
      // success
    } else {
      // error
    }
  }
});

Parameters

urlStringThe URL of the scene file. Usually this will be "scene_id.json"
callbackfunctionThe function called after the settings are applied. Passed (err) where err is null if no error occurred.

preload(callback)

Load all assets in the asset registry that are marked as 'preload'

Parameters

callbackfunctionFunction called when all assets are loaded

render()

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

renderLine(start, end, color)

Draw a line in one color

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);

Parameters

startpc.Vec3The start of the line
endpc.Vec3The end of the line
colorpc.ColorThe color of the line

renderLine^2(start, end, startColor, endColor)

Draw a line which blends between two colors

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);

Parameters

startpc.Vec3The start of the line
endpc.Vec3The end of the line
startColorpc.ColorThe start color of the line
endColorpc.ColorThe end color of the line

renderLine^3(start, end, color, lineType)

Draw a line of one color with specified line type

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, pc.LINEBATCH_OVERLAY);

Parameters

startpc.Vec3The start of the line
endpc.Vec3The end of the line
colorpc.ColorThe color of the line
lineTypeNumberThe type of rendering to use: pc.LINEBATCH_WORLD, pc.LINEBATCH_OVERLAY, pc.LINEBATCH_GIZMO. Default is pc.LINEBATCH_WORLD

renderLine^4(start, end, startColor, endColor, lineType)

Draw a line which blends between two colors with specified line type

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, pc.LINEBATCH_OVERLAY);

Parameters

startpc.Vec3The start of the line
endpc.Vec3The end of the line
startColorpc.ColorThe start color of the line
endColorpc.ColorThe end color of the line
lineTypeNumberThe type of rendering to use: pc.LINEBATCH_WORLD, pc.LINEBATCH_OVERLAY, pc.LINEBATCH_GIZMO. Default is pc.LINEBATCH_WORLD

renderLines(position, color, [lineType])

Draw an array of lines.

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);

Parameters

positionpc.Vec3[]An array of points to draw lines between
colorpc.Color[]An array of colors to color the lines. This must be the same size as the position array
lineTypeNumberThe type of rendering to use: pc.LINEBATCH_WORLD, pc.LINEBATCH_OVERLAY, pc.LINEBATCH_GIZMO. Default is pc.LINEBATCH_WORLD

resizeCanvas([width], [height])

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

widthNumberThe width of the canvas, only used in NONE mode
heightNumberThe height of the canvas, only used in NONE mode

Returns

Object A object containing the values calculated to use as width and height

setCanvasFillMode(mode, [width], [height])

Controls how the canvas fills the window and resizes when the window changes.

Parameters

modeStringThe 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.
widthNumberThe width of the canvas (only used when mode is pc.FILLMODE_NONE).
heightNumberThe height of the canvas (only used when mode is pc.FILLMODE_NONE).

setCanvasResolution(mode, [width], [height])

Change the resolution of the canvas, and set the way it behaves when the window is resized

Parameters

modestringThe 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.
widthNumberThe horizontal resolution, optional in AUTO mode, if not provided canvas clientWidth is used
heightNumberThe vertical resolution, optional in AUTO mode, if not provided canvas clientHeight is used

start()

Start the Application updating

update(dt)

Application specific update method. Override this if you have a custom Application

Parameters

dtNumberThe time delta since the last frame.