Skip to content

Instantly share code, notes, and snippets.

@crisu83
Created June 25, 2014 07:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save crisu83/5857c4a638e57308be4f to your computer and use it in GitHub Desktop.
Save crisu83/5857c4a638e57308be4f to your computer and use it in GitHub Desktop.
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;
@javierfernandes
Copy link

Nice ! thanks man !

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