Impact

Canvas

CanvasContext2D


isPointInPath(x, y) and shadows are not yet implemented.

font is quite picky at what it accepts. The format must be XXpt Name, e.g.:

ctx.font = '10pt Zapfino';

To load custom, non-system fonts use ejecta.loadFont()

ejecta.loadFont('my-font.tff');

WebGL Context

Ejecta fully implements the WebGL 1.0 Spec.

Audio

Video

Example:

var video = document.createElement('video');
console.log( 'can play mp4', video.canPlayType('video/mp4') );
video.src = 'test.mov';
 
// Also loads from the internet:
//video.src = 'http://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT1211/sample_iTunes.mov';
 
// Show playback controls:
//video.controls = true;
 
// For local video files, you don't really have to wait for the 
// 'canplaythrough' event you can just call .play() after setting the .src
 
video.addEventListener('canplaythrough', function(){
	console.log('duration', video.duration);
	video.play();
	
	// Jump to 100 seconds:
	//video.currentTime = 100;
	
	video.addEventListener('click', function(){
		video.pause();
	}, false);
	
	video.addEventListener('ended', function(){
		console.log('ended!');
	}, false);
	
}, false);

Image

Ejecta supports loading of .jpg, .png and .pvr (PVRTC compressed) Images.

localStorage

Note that getItem() and setItem() only accept string values and keys, but you can easily save objects with JSON.stringify(obj) and retrieve them with JSON.parse(string) again.

document

The whole document object is a thin, "fake" layer, meant to provide some common behavior for loading additional script files and handling touch and devicemotion events. E.g. the following examples will work in Ejecta as well as in a normal Browser:

// Get the main screen canvas
var canvas = document.getElementById('canvas');

// Create a second, off-screen canvas
var offscreen = document.createElement('canvas');

// Load an additional script file asynchronously
var script = document.createElement('script');
script.src = 'some-source-file.js';
document.body.appendChild(script);

// Log coordinates for touchmove event
document.addEventListener( 'touchmove', function( ev ) {
	console.log( ev.touches[0].pageX, ev.touches[0].pageY );
}, false );

// Log accelerometer data
document.addEventListener( 'devicemotion', function( ev ) {
	var accel = ev.accelerationIncludingGravity;
	console.log( accel.x, accel.y, accel.z );
}, false );

See Other Sources/ejecta.js if you want to implement additional default behavior.

console

window

Gamepad API

Supports Gamepads on iOS and tvOS. It closely follows the W3C Gamepad API Spec – including the "standard" mapping.

On tvOS, the TV Remote is mapped to the appropriate buttons, the touchpad is mapped to an analog stick and(!) a D-Pad. The one addition to the W3C spec is the gamepad.exitOnMenuPress flag. See here for the fascinating details about why this flag exists.

For a full example see this comment.

Geolocation

Example:

var success = function(position) {
    console.log('position', JSON.stringify(position.coords), position.timestamp);
};
var error = function(error) {
    if( error.code === navigator.geolocation.PERMISSION_DENIED ) {
        // Remember that we don't have permissions and don't try again
    }
    console.log('error', error.code, error.message);
}

// One shot location - aims to return fast and may be imprecise.
// Set enableHighAccuracy to true for better results
navigator.geolocation.getCurrentPosition(success, error, {enableHighAccuracy:false});

More over at github.

WebSocket

Example:

var socket = new WebSocket('ws://example.com:8787')

socket.addEventListener('open', function() {
	console.log('open');
	socket.send('hello');
});

socket.addEventListener('message', function(event) {
	console.log(event.data)
});

socket.addEventListener('close', function() {
	console.log('close');
});

socket.addEventListener('error', function(error) {
	console.log(error)
});

XMLHttpRequest

Example:

var request = new XMLHttpRequest();

request.onreadystatechange = function() {
    if( request.readyState == request.DONE && request.status == 200 ) {
        console.log( 'server', request.getResponseHeader('server') );
        console.log( request.responseText );
    }
};

request.open('GET', 'http://www.google.com/');
request.send();

As of Ejecta 1.2, XMLHttpRequest can also be used to load local files. The file path is relative to the App/ directory.

Use req.overrideMimeType('text/plain; charset=x-user-defined'); to load binary data as text. The newer, better way of loading binary data is to set the responseType to 'arraybuffer'

ejecta

// This loads an additional script file synchronously (blocking)
ejecta.include('some-source-file.js');

// Open a URL in Mobile Safari
ejecta.openURL( 'http://impactjs.com/', 'Open the Impact website?' );

// Ask the user for some text:
ejecta.getText( 'Highscore', 'Please enter your name', function(text) {
	console.log(text);
});

// CommonJS style require
var inc = require('math/basics').increment;

// Load a custom TTF font, similar to a CSS @font-face declaration
ejecta.loadFont("path/to/my/font.ttf");