Skip to content

Instantly share code, notes, and snippets.

@ryaninvents
Forked from crisu83/phaser.js
Last active August 15, 2016 15:55
Show Gist options
  • Save ryaninvents/df918aee4421bc1a7f85747cabb2ae7e to your computer and use it in GitHub Desktop.
Save ryaninvents/df918aee4421bc1a7f85747cabb2ae7e 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 = require('jsdom').jsdom(null);
global.window = document.defaultView;
global.Canvas = require('canvas');
global.Image = require('canvas').Image;
global.window.CanvasRenderingContext2D = 'foo'; // let Phaser think that we have a canvas
global.window.Element = undefined;
global.window.document = global.document;
global.navigator = global.window.navigator = { userAgent: 'node.js' }; // could be anything
global.window.DOMParser = require('xmldom').DOMParser;
// 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('./pixi/bin/pixi.js');
global.Phaser = Phaser = require('Phaser/build/custom/phaser-arcade-physics');
module.exports = Phaser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment