Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Wrapper module for running Phaser.js on Node.js.
// this is an ingenius hack that allows us to run Phaser without a browser
// ... and yes, it took some time to figure out how to do this
var Canvas = require('canvas')
, jsdom = require('jsdom')
, document = jsdom.jsdom(null)
, window = document.parentWindow
, Phaser;
// expose a few things to all the modules
global.document = document;
global.window = window;
global.Canvas = Canvas;
global.Image = Canvas.Image;
global.window.CanvasRenderingContext2D = 'foo'; // let Phaser think that we have a canvas
global.window.Element = undefined;
global.navigator = {userAgent: 'Custom'}; // could be anything
// fake the xml http request object because Phaser.Loader uses it
global.XMLHttpRequest = function() {};
// load an expose PIXI in order to finally load Phaser
global.PIXI = require('Phaser/build/custom/pixi');
global.Phaser = Phaser = require('Phaser/build/custom/phaser-arcade-physics');
module.exports = Phaser;

This comment has been minimized.

Copy link

@javierfernandes javierfernandes commented May 25, 2018

Nice ! thanks man !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment