diff --git a/demo/js/Demo.js b/demo/js/Demo.js index 6c936d5..73337ad 100644 --- a/demo/js/Demo.js +++ b/demo/js/Demo.js @@ -31,53 +31,31 @@ Inspector = MatterTools.Inspector; } - // initialise the demo - - Demo.create = function() { - return { - isMobile: _isMobile, + Demo.create = function(options) { + var defaults = { + sceneName: 'mixed', sceneEvents: [] }; + + return Common.extend(defaults, options); }; Demo.init = function() { var demo = Demo.create(); - // some example engine options - var options = { - positionIterations: 6, - velocityIterations: 4, - enableSleeping: false, - metrics: { extended: true } - }; + // get container element for the canvas + demo.container = document.getElementById('canvas-container'); - // create a Matter engine - // NOTE: this is actually Matter.Engine.create(), see the aliases at top of this file - if (_isBrowser) { - var container = document.getElementById('canvas-container'); - demo.engine = Engine.create(container, options); - - // add a mouse controlled constraint - demo.mouseConstraint = MouseConstraint.create(demo.engine); - World.add(demo.engine.world, demo.mouseConstraint); - } else { - demo.engine = Engine.create(options); - demo.engine.render = {}; - demo.engine.render.options = {}; - } - - // demo instance reference for external use - Matter.Demo._demo = demo; - - // skip runner when performing automated tests - if (_isAutomatedTest) return; + // create an example engine (see /examples/engine.js) + demo.engine = Example.engine(demo); // run the engine demo.runner = Engine.run(demo.engine); - // default scene function name - demo.sceneName = 'mixed'; - + // add a mouse controlled constraint + demo.mouseConstraint = MouseConstraint.create(demo.engine); + World.add(demo.engine.world, demo.mouseConstraint); + // get the scene function name from hash if (window.location.hash.length !== 0) demo.sceneName = window.location.hash.replace('#', '').replace('-inspect', ''); @@ -88,18 +66,20 @@ // set up demo interface (see end of this file) Demo.initControls(demo); + + return demo; }; // call init when the page has loaded fully - - if (window.addEventListener) { - window.addEventListener('load', Demo.init); - } else if (window.attachEvent) { - window.attachEvent('load', Demo.init); + if (!_isAutomatedTest) { + if (window.addEventListener) { + window.addEventListener('load', Demo.init); + } else if (window.attachEvent) { + window.attachEvent('load', Demo.init); + } } // the functions for the demo interface and controls below - Demo.initControls = function(demo) { var demoSelect = document.getElementById('demo-select'), demoReset = document.getElementById('demo-reset'); diff --git a/examples/engine.js b/examples/engine.js new file mode 100644 index 0000000..3b3419d --- /dev/null +++ b/examples/engine.js @@ -0,0 +1,17 @@ +(function() { + + var Engine = Matter.Engine; + + Example.engine = function(demo) { + // some example engine options + var options = { + positionIterations: 6, + velocityIterations: 4, + enableSleeping: false, + metrics: { extended: true } + }; + + return Engine.create(demo.container, options); + }; + +})(); \ No newline at end of file diff --git a/examples/wreckingBall.js b/examples/wreckingBall.js index b6b3a3b..3221745 100644 --- a/examples/wreckingBall.js +++ b/examples/wreckingBall.js @@ -25,12 +25,6 @@ pointA: { x: 300, y: 100 }, bodyB: ball })); - - if (!demo.isMobile) { - var renderOptions = engine.render.options; - renderOptions.showCollisions = true; - renderOptions.showVelocity = true; - } }; })(); \ No newline at end of file diff --git a/test/browser/TestDemo.js b/test/browser/TestDemo.js index 4a95388..336e1cd 100644 --- a/test/browser/TestDemo.js +++ b/test/browser/TestDemo.js @@ -51,18 +51,30 @@ var test = function(status) { worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json'; var worldStart = page.evaluate(function(demo) { - var engine = Matter.Demo._demo.engine; if (!(demo in Matter.Example)) { throw '\'' + demo + '\' is not defined in Matter.Demo'; } - Matter.Demo.reset(Matter.Demo._demo); - Matter.Example[demo](Matter.Demo._demo); + + var _demo = Matter.Demo.create(), + engine = Matter.Example.engine(_demo), + runner = Matter.Runner.create(); + + Matter.Demo._demo = _demo; + _demo.engine = engine; + _demo.engine.render = {}; + _demo.engine.render.options = {}; + _demo.runner = runner; + _demo.mouseConstraint = Matter.MouseConstraint.create(engine); + + Matter.Demo.reset(_demo); + Matter.Example[demo](_demo); + return engine.world; }, demo); var worldEnd = page.evaluate(function(demo, frames) { var engine = Matter.Demo._demo.engine, - runner = Matter.Runner.create(); + runner = Matter.Demo._demo.runner; for (var j = 0; j <= frames; j += 1) { Matter.Runner.tick(runner, engine, j * runner.delta); diff --git a/test/node/TestDemo.js b/test/node/TestDemo.js index 30b6237..31089e9 100644 --- a/test/node/TestDemo.js +++ b/test/node/TestDemo.js @@ -6,8 +6,8 @@ var compare = require('fast-json-patch').compare; var path = require('path'); var $ = require('cheerio'); var Matter = require('../../build/matter-dev.js'); -Matter.Demo = require('../../demo/js/Demo.js'); Matter.Example = require('../../demo/js/Examples.js'); +Matter.Demo = require('../../demo/js/Demo.js'); var demo, frames = 10, @@ -42,17 +42,21 @@ var test = function() { worldStartDiffPath = diffsPath + '/' + demo + '/' + demo + '-0.json', worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json'; - Matter.Demo.init(); - - var engine = Matter.Demo._demo.engine, + var _demo = Matter.Demo.create(), + engine = Matter.Example.engine(_demo), runner = Matter.Runner.create(); + _demo.engine = engine; + _demo.engine.render = {}; + _demo.engine.render.options = {}; + _demo.runner = runner; + if (!(demo in Matter.Example)) { throw '\'' + demo + '\' is not defined in Matter.Example'; } - Matter.Demo.reset(Matter.Demo._demo); - Matter.Example[demo](Matter.Demo._demo); + Matter.Demo.reset(_demo); + Matter.Example[demo](_demo); var worldStart = JSON.parse(resurrect.stringify(engine.world, precisionLimiter));