0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00

refactor Demo.init

This commit is contained in:
liabru 2015-08-25 23:31:10 +01:00
parent c0b24daeef
commit fb76605190
5 changed files with 64 additions and 57 deletions

View file

@ -31,52 +31,30 @@
Inspector = MatterTools.Inspector; Inspector = MatterTools.Inspector;
} }
// initialise the demo Demo.create = function(options) {
var defaults = {
Demo.create = function() { sceneName: 'mixed',
return {
isMobile: _isMobile,
sceneEvents: [] sceneEvents: []
}; };
return Common.extend(defaults, options);
}; };
Demo.init = function() { Demo.init = function() {
var demo = Demo.create(); var demo = Demo.create();
// some example engine options // get container element for the canvas
var options = { demo.container = document.getElementById('canvas-container');
positionIterations: 6,
velocityIterations: 4,
enableSleeping: false,
metrics: { extended: true }
};
// create a Matter engine // create an example engine (see /examples/engine.js)
// NOTE: this is actually Matter.Engine.create(), see the aliases at top of this file demo.engine = Example.engine(demo);
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;
// run the engine // run the engine
demo.runner = Engine.run(demo.engine); demo.runner = Engine.run(demo.engine);
// default scene function name // add a mouse controlled constraint
demo.sceneName = 'mixed'; demo.mouseConstraint = MouseConstraint.create(demo.engine);
World.add(demo.engine.world, demo.mouseConstraint);
// get the scene function name from hash // get the scene function name from hash
if (window.location.hash.length !== 0) if (window.location.hash.length !== 0)
@ -88,18 +66,20 @@
// set up demo interface (see end of this file) // set up demo interface (see end of this file)
Demo.initControls(demo); Demo.initControls(demo);
return demo;
}; };
// call init when the page has loaded fully // call init when the page has loaded fully
if (!_isAutomatedTest) {
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);
} }
}
// the functions for the demo interface and controls below // the functions for the demo interface and controls below
Demo.initControls = function(demo) { Demo.initControls = function(demo) {
var demoSelect = document.getElementById('demo-select'), var demoSelect = document.getElementById('demo-select'),
demoReset = document.getElementById('demo-reset'); demoReset = document.getElementById('demo-reset');

17
examples/engine.js Normal file
View file

@ -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);
};
})();

View file

@ -25,12 +25,6 @@
pointA: { x: 300, y: 100 }, pointA: { x: 300, y: 100 },
bodyB: ball bodyB: ball
})); }));
if (!demo.isMobile) {
var renderOptions = engine.render.options;
renderOptions.showCollisions = true;
renderOptions.showVelocity = true;
}
}; };
})(); })();

View file

@ -51,18 +51,30 @@ var test = function(status) {
worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json'; worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json';
var worldStart = page.evaluate(function(demo) { var worldStart = page.evaluate(function(demo) {
var engine = Matter.Demo._demo.engine;
if (!(demo in Matter.Example)) { if (!(demo in Matter.Example)) {
throw '\'' + demo + '\' is not defined in Matter.Demo'; 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; return engine.world;
}, demo); }, demo);
var worldEnd = page.evaluate(function(demo, frames) { var worldEnd = page.evaluate(function(demo, frames) {
var engine = Matter.Demo._demo.engine, var engine = Matter.Demo._demo.engine,
runner = Matter.Runner.create(); runner = Matter.Demo._demo.runner;
for (var j = 0; j <= frames; j += 1) { for (var j = 0; j <= frames; j += 1) {
Matter.Runner.tick(runner, engine, j * runner.delta); Matter.Runner.tick(runner, engine, j * runner.delta);

View file

@ -6,8 +6,8 @@ var compare = require('fast-json-patch').compare;
var path = require('path'); var path = require('path');
var $ = require('cheerio'); var $ = require('cheerio');
var Matter = require('../../build/matter-dev.js'); var Matter = require('../../build/matter-dev.js');
Matter.Demo = require('../../demo/js/Demo.js');
Matter.Example = require('../../demo/js/Examples.js'); Matter.Example = require('../../demo/js/Examples.js');
Matter.Demo = require('../../demo/js/Demo.js');
var demo, var demo,
frames = 10, frames = 10,
@ -42,17 +42,21 @@ var test = function() {
worldStartDiffPath = diffsPath + '/' + demo + '/' + demo + '-0.json', worldStartDiffPath = diffsPath + '/' + demo + '/' + demo + '-0.json',
worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json'; worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json';
Matter.Demo.init(); var _demo = Matter.Demo.create(),
engine = Matter.Example.engine(_demo),
var engine = Matter.Demo._demo.engine,
runner = Matter.Runner.create(); runner = Matter.Runner.create();
_demo.engine = engine;
_demo.engine.render = {};
_demo.engine.render.options = {};
_demo.runner = runner;
if (!(demo in Matter.Example)) { if (!(demo in Matter.Example)) {
throw '\'' + demo + '\' is not defined in Matter.Example'; throw '\'' + demo + '\' is not defined in Matter.Example';
} }
Matter.Demo.reset(Matter.Demo._demo); Matter.Demo.reset(_demo);
Matter.Example[demo](Matter.Demo._demo); Matter.Example[demo](_demo);
var worldStart = JSON.parse(resurrect.stringify(engine.world, precisionLimiter)); var worldStart = JSON.parse(resurrect.stringify(engine.world, precisionLimiter));