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:
parent
c0b24daeef
commit
fb76605190
5 changed files with 64 additions and 57 deletions
|
@ -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');
|
||||
|
|
17
examples/engine.js
Normal file
17
examples/engine.js
Normal 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);
|
||||
};
|
||||
|
||||
})();
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
|
@ -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);
|
||||
|
|
|
@ -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));
|
||||
|
||||
|
|
Loading…
Reference in a new issue