0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00

fix Matter.Demo for node

This commit is contained in:
liabru 2015-08-15 20:39:13 +01:00
parent ad55b85912
commit 2de1161db2

View file

@ -1,5 +1,15 @@
(function() { (function() {
var _isBrowser = typeof window !== 'undefined',
Matter = _isBrowser ? window.Matter : require('../../build/matter-dev.js');
var Demo = {};
Matter.Demo = Demo;
if (!_isBrowser) {
module.exports = Demo;
}
// Matter aliases // Matter aliases
var Engine = Matter.Engine, var Engine = Matter.Engine,
World = Matter.World, World = Matter.World,
@ -19,14 +29,11 @@
Svg = Matter.Svg; Svg = Matter.Svg;
// MatterTools aliases // MatterTools aliases
if (window.MatterTools) { if (_isBrowser && window.MatterTools) {
var Gui = MatterTools.Gui, var Gui = MatterTools.Gui,
Inspector = MatterTools.Inspector; Inspector = MatterTools.Inspector;
} }
var Demo = {};
Matter.Demo = Demo;
var _engine, var _engine,
_runner, _runner,
_gui, _gui,
@ -34,15 +41,13 @@
_sceneName, _sceneName,
_mouseConstraint, _mouseConstraint,
_sceneEvents = [], _sceneEvents = [],
_useInspector = window.location.hash.indexOf('-inspect') !== -1, _useInspector = _isBrowser && window.location.hash.indexOf('-inspect') !== -1,
_isMobile = /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent), _isMobile = _isBrowser && /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent),
_isAutomatedTest = window._phantom ? true : false; _isAutomatedTest = _isBrowser ? false : true;
// initialise the demo // initialise the demo
Demo.init = function() { Demo.init = function() {
var container = document.getElementById('canvas-container');
// some example engine options // some example engine options
var options = { var options = {
positionIterations: 6, positionIterations: 6,
@ -53,11 +58,18 @@
// create a Matter engine // create a Matter engine
// NOTE: this is actually Matter.Engine.create(), see the aliases at top of this file // NOTE: this is actually Matter.Engine.create(), see the aliases at top of this file
if (_isBrowser) {
var container = document.getElementById('canvas-container');
_engine = Engine.create(container, options); _engine = Engine.create(container, options);
// add a mouse controlled constraint // add a mouse controlled constraint
_mouseConstraint = MouseConstraint.create(_engine); _mouseConstraint = MouseConstraint.create(_engine);
World.add(_engine.world, _mouseConstraint); World.add(_engine.world, _mouseConstraint);
} else {
_engine = Engine.create(options);
_engine.render = {};
_engine.render.options = {};
}
// engine reference for external use // engine reference for external use
Matter.Demo._engine = _engine; Matter.Demo._engine = _engine;
@ -84,11 +96,13 @@
// call init when the page has loaded fully // call init when the page has loaded fully
if (_isBrowser) {
if (window.addEventListener) { if (window.addEventListener) {
window.addEventListener('load', Demo.init); window.addEventListener('load', Demo.init);
} else if (window.attachEvent) { } else if (window.attachEvent) {
window.attachEvent('load', Demo.init); window.attachEvent('load', Demo.init);
} }
}
// each demo scene is set up in its own function, see below // each demo scene is set up in its own function, see below
@ -1720,15 +1734,17 @@
Engine.clear(_engine); Engine.clear(_engine);
// clear scene graph (if defined in controller) // clear scene graph (if defined in controller)
if (_engine.render) {
var renderController = _engine.render.controller; var renderController = _engine.render.controller;
if (renderController.clear) if (renderController && renderController.clear)
renderController.clear(_engine.render); renderController.clear(_engine.render);
}
// clear all scene events // clear all scene events
for (var i = 0; i < _sceneEvents.length; i++) for (var i = 0; i < _sceneEvents.length; i++)
Events.off(_engine, _sceneEvents[i]); Events.off(_engine, _sceneEvents[i]);
if (_mouseConstraint.events) { if (_mouseConstraint && _mouseConstraint.events) {
for (i = 0; i < _sceneEvents.length; i++) for (i = 0; i < _sceneEvents.length; i++)
Events.off(_mouseConstraint, _sceneEvents[i]); Events.off(_mouseConstraint, _sceneEvents[i]);
} }
@ -1743,7 +1759,7 @@
Events.off(_runner, _sceneEvents[i]); Events.off(_runner, _sceneEvents[i]);
} }
if (_engine.render.events) { if (_engine.render && _engine.render.events) {
for (i = 0; i < _sceneEvents.length; i++) for (i = 0; i < _sceneEvents.length; i++)
Events.off(_engine.render, _sceneEvents[i]); Events.off(_engine.render, _sceneEvents[i]);
} }
@ -1757,8 +1773,10 @@
Common._seed = 0; Common._seed = 0;
// reset mouse offset and scale (only required for Demo.views) // reset mouse offset and scale (only required for Demo.views)
if (_mouseConstraint) {
Mouse.setScale(_mouseConstraint.mouse, { x: 1, y: 1 }); Mouse.setScale(_mouseConstraint.mouse, { x: 1, y: 1 });
Mouse.setOffset(_mouseConstraint.mouse, { x: 0, y: 0 }); Mouse.setOffset(_mouseConstraint.mouse, { x: 0, y: 0 });
}
_engine.enableSleeping = false; _engine.enableSleeping = false;
_engine.world.gravity.y = 1; _engine.world.gravity.y = 1;
@ -1773,8 +1791,11 @@
Bodies.rectangle(-offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true }) Bodies.rectangle(-offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true })
]); ]);
if (_mouseConstraint) {
World.add(_world, _mouseConstraint); World.add(_world, _mouseConstraint);
}
if (_engine.render) {
var renderOptions = _engine.render.options; var renderOptions = _engine.render.options;
renderOptions.wireframes = true; renderOptions.wireframes = true;
renderOptions.hasBounds = false; renderOptions.hasBounds = false;
@ -1793,6 +1814,7 @@
renderOptions.showInternalEdges = false; renderOptions.showInternalEdges = false;
renderOptions.showSeparations = false; renderOptions.showSeparations = false;
renderOptions.background = '#fff'; renderOptions.background = '#fff';
}
if (_isMobile) if (_isMobile)
renderOptions.showDebug = true; renderOptions.showDebug = true;