2015-08-25 19:20:56 -04:00
|
|
|
/**
|
|
|
|
* The Matter.js demo page controller and example runner.
|
|
|
|
*
|
|
|
|
* NOTE: For the actual example code, refer to the source files in `/examples/`.
|
|
|
|
*
|
|
|
|
* @class Demo
|
|
|
|
*/
|
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
(function() {
|
|
|
|
|
2015-08-25 14:12:52 -04:00
|
|
|
var _isBrowser = typeof window !== 'undefined' && window.location,
|
2015-08-25 15:10:13 -04:00
|
|
|
_useInspector = _isBrowser && window.location.hash.indexOf('-inspect') !== -1,
|
|
|
|
_isMobile = _isBrowser && /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent),
|
|
|
|
_isAutomatedTest = !_isBrowser || window._phantom;
|
|
|
|
|
|
|
|
var Matter = _isBrowser ? window.Matter : require('../../build/matter-dev.js');
|
2015-08-17 15:55:13 -04:00
|
|
|
|
2015-08-15 15:39:13 -04:00
|
|
|
var Demo = {};
|
|
|
|
Matter.Demo = Demo;
|
|
|
|
|
|
|
|
if (!_isBrowser) {
|
|
|
|
module.exports = Demo;
|
2015-08-16 19:48:52 -04:00
|
|
|
window = {};
|
2015-08-15 15:39:13 -04:00
|
|
|
}
|
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
// Matter aliases
|
2015-12-25 10:57:27 -05:00
|
|
|
var Body = Matter.Body,
|
|
|
|
Example = Matter.Example,
|
2015-08-25 14:12:52 -04:00
|
|
|
Engine = Matter.Engine,
|
2015-08-25 14:31:44 -04:00
|
|
|
World = Matter.World,
|
2014-02-19 09:15:05 -05:00
|
|
|
Common = Matter.Common,
|
2015-08-25 14:12:52 -04:00
|
|
|
Bodies = Matter.Bodies,
|
2015-08-25 14:31:44 -04:00
|
|
|
Events = Matter.Events,
|
|
|
|
Mouse = Matter.Mouse,
|
2015-12-02 17:33:31 -05:00
|
|
|
MouseConstraint = Matter.MouseConstraint,
|
|
|
|
Runner = Matter.Runner;
|
2014-05-01 18:13:42 -04:00
|
|
|
|
|
|
|
// MatterTools aliases
|
2014-05-05 13:18:15 -04:00
|
|
|
if (window.MatterTools) {
|
|
|
|
var Gui = MatterTools.Gui,
|
|
|
|
Inspector = MatterTools.Inspector;
|
|
|
|
}
|
2014-02-19 09:15:05 -05:00
|
|
|
|
2015-08-25 18:31:10 -04:00
|
|
|
Demo.create = function(options) {
|
|
|
|
var defaults = {
|
2015-12-02 17:33:31 -05:00
|
|
|
isManual: false,
|
2015-08-25 18:31:10 -04:00
|
|
|
sceneName: 'mixed',
|
2015-08-25 15:10:13 -04:00
|
|
|
sceneEvents: []
|
2015-08-25 14:12:52 -04:00
|
|
|
};
|
2015-08-25 18:31:10 -04:00
|
|
|
|
|
|
|
return Common.extend(defaults, options);
|
2015-08-25 14:12:52 -04:00
|
|
|
};
|
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
Demo.init = function() {
|
2015-08-25 15:10:13 -04:00
|
|
|
var demo = Demo.create();
|
2016-01-14 18:25:16 -05:00
|
|
|
Matter.Demo._demo = demo;
|
2015-08-25 15:10:13 -04:00
|
|
|
|
2015-08-25 18:31:10 -04:00
|
|
|
// get container element for the canvas
|
|
|
|
demo.container = document.getElementById('canvas-container');
|
2015-08-04 16:20:20 -04:00
|
|
|
|
2015-08-25 18:31:10 -04:00
|
|
|
// create an example engine (see /examples/engine.js)
|
|
|
|
demo.engine = Example.engine(demo);
|
2015-08-04 16:20:20 -04:00
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
// run the engine
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.runner = Engine.run(demo.engine);
|
2014-02-19 09:15:05 -05:00
|
|
|
|
2015-08-25 18:31:10 -04:00
|
|
|
// add a mouse controlled constraint
|
|
|
|
demo.mouseConstraint = MouseConstraint.create(demo.engine);
|
|
|
|
World.add(demo.engine.world, demo.mouseConstraint);
|
|
|
|
|
2015-12-01 19:41:28 -05:00
|
|
|
// pass mouse to renderer to enable showMousePosition
|
|
|
|
demo.engine.render.mouse = demo.mouseConstraint.mouse;
|
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
// get the scene function name from hash
|
|
|
|
if (window.location.hash.length !== 0)
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.sceneName = window.location.hash.replace('#', '').replace('-inspect', '');
|
2014-02-19 09:15:05 -05:00
|
|
|
|
|
|
|
// set up a scene with bodies
|
2015-08-25 15:10:13 -04:00
|
|
|
Demo.reset(demo);
|
2015-12-02 17:33:31 -05:00
|
|
|
Demo.setScene(demo, demo.sceneName);
|
2014-02-19 09:15:05 -05:00
|
|
|
|
2014-03-25 11:31:05 -04:00
|
|
|
// set up demo interface (see end of this file)
|
2015-08-25 15:10:13 -04:00
|
|
|
Demo.initControls(demo);
|
2015-08-25 18:31:10 -04:00
|
|
|
|
2015-08-25 19:20:56 -04:00
|
|
|
// pass through runner as timing for debug rendering
|
|
|
|
demo.engine.metrics.timing = demo.runner;
|
|
|
|
|
2015-08-25 18:31:10 -04:00
|
|
|
return demo;
|
2014-02-19 09:15:05 -05:00
|
|
|
};
|
|
|
|
|
2014-03-25 11:31:05 -04:00
|
|
|
// call init when the page has loaded fully
|
2015-08-25 18:31:10 -04:00
|
|
|
if (!_isAutomatedTest) {
|
|
|
|
if (window.addEventListener) {
|
|
|
|
window.addEventListener('load', Demo.init);
|
|
|
|
} else if (window.attachEvent) {
|
|
|
|
window.attachEvent('load', Demo.init);
|
|
|
|
}
|
2014-02-19 09:15:05 -05:00
|
|
|
}
|
|
|
|
|
2015-12-02 17:33:31 -05:00
|
|
|
Demo.setScene = function(demo, sceneName) {
|
|
|
|
Example[sceneName](demo);
|
|
|
|
};
|
|
|
|
|
2014-03-25 11:31:05 -04:00
|
|
|
// the functions for the demo interface and controls below
|
2015-08-25 15:10:13 -04:00
|
|
|
Demo.initControls = function(demo) {
|
2014-03-25 11:31:05 -04:00
|
|
|
var demoSelect = document.getElementById('demo-select'),
|
|
|
|
demoReset = document.getElementById('demo-reset');
|
|
|
|
|
2014-04-29 08:07:53 -04:00
|
|
|
// create a Matter.Gui
|
|
|
|
if (!_isMobile && Gui) {
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.gui = Gui.create(demo.engine, demo.runner);
|
2014-03-25 11:31:05 -04:00
|
|
|
|
2014-03-26 07:53:41 -04:00
|
|
|
// need to add mouse constraint back in after gui clear or load is pressed
|
2015-08-25 15:10:13 -04:00
|
|
|
Events.on(demo.gui, 'clear load', function() {
|
|
|
|
demo.mouseConstraint = MouseConstraint.create(demo.engine);
|
|
|
|
World.add(demo.engine.world, demo.mouseConstraint);
|
2014-03-26 07:53:41 -04:00
|
|
|
});
|
2015-05-24 09:21:24 -04:00
|
|
|
|
|
|
|
// need to rebind mouse on render change
|
2015-08-25 15:10:13 -04:00
|
|
|
Events.on(demo.gui, 'setRenderer', function() {
|
|
|
|
Mouse.setElement(demo.mouseConstraint.mouse, demo.engine.render.canvas);
|
2015-05-24 09:21:24 -04:00
|
|
|
});
|
2014-04-29 08:07:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a Matter.Inspector
|
2014-05-01 18:13:42 -04:00
|
|
|
if (!_isMobile && Inspector && _useInspector) {
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.inspector = Inspector.create(demo.engine, demo.runner);
|
2014-04-28 07:50:56 -04:00
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
Events.on(demo.inspector, 'import', function() {
|
|
|
|
demo.mouseConstraint = MouseConstraint.create(demo.engine);
|
|
|
|
World.add(demo.engine.world, demo.mouseConstraint);
|
2014-04-28 07:50:56 -04:00
|
|
|
});
|
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
Events.on(demo.inspector, 'play', function() {
|
|
|
|
demo.mouseConstraint = MouseConstraint.create(demo.engine);
|
|
|
|
World.add(demo.engine.world, demo.mouseConstraint);
|
2014-05-03 11:54:22 -04:00
|
|
|
});
|
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
Events.on(demo.inspector, 'selectStart', function() {
|
|
|
|
demo.mouseConstraint.constraint.render.visible = false;
|
2014-04-28 07:50:56 -04:00
|
|
|
});
|
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
Events.on(demo.inspector, 'selectEnd', function() {
|
|
|
|
demo.mouseConstraint.constraint.render.visible = true;
|
2014-04-28 07:50:56 -04:00
|
|
|
});
|
2014-03-26 07:53:41 -04:00
|
|
|
}
|
|
|
|
|
2014-03-25 11:31:05 -04:00
|
|
|
// go fullscreen when using a mobile device
|
|
|
|
if (_isMobile) {
|
|
|
|
var body = document.body;
|
|
|
|
|
|
|
|
body.className += ' is-mobile';
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.engine.render.canvas.addEventListener('touchstart', Demo.fullscreen);
|
2014-03-25 11:31:05 -04:00
|
|
|
|
|
|
|
var fullscreenChange = function() {
|
|
|
|
var fullscreenEnabled = document.fullscreenEnabled || document.mozFullScreenEnabled || document.webkitFullscreenEnabled;
|
|
|
|
|
|
|
|
// delay fullscreen styles until fullscreen has finished changing
|
|
|
|
setTimeout(function() {
|
|
|
|
if (fullscreenEnabled) {
|
|
|
|
body.className += ' is-fullscreen';
|
|
|
|
} else {
|
|
|
|
body.className = body.className.replace('is-fullscreen', '');
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('webkitfullscreenchange', fullscreenChange);
|
|
|
|
document.addEventListener('mozfullscreenchange', fullscreenChange);
|
|
|
|
document.addEventListener('fullscreenchange', fullscreenChange);
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:33:31 -05:00
|
|
|
// keyboard controls
|
|
|
|
document.onkeypress = function(keys) {
|
|
|
|
// shift + a = toggle manual
|
|
|
|
if (keys.shiftKey && keys.keyCode === 65) {
|
|
|
|
Demo.setManualControl(demo, !demo.isManual);
|
|
|
|
}
|
|
|
|
|
|
|
|
// shift + q = step
|
|
|
|
if (keys.shiftKey && keys.keyCode === 81) {
|
|
|
|
if (!demo.isManual) {
|
|
|
|
Demo.setManualControl(demo, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Runner.tick(demo.runner, demo.engine);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-25 11:31:05 -04:00
|
|
|
// initialise demo selector
|
2015-08-25 15:10:13 -04:00
|
|
|
demoSelect.value = demo.sceneName;
|
2015-08-25 18:52:25 -04:00
|
|
|
Demo.setUpdateSourceLink(demo.sceneName);
|
2014-03-25 11:31:05 -04:00
|
|
|
|
|
|
|
demoSelect.addEventListener('change', function(e) {
|
2015-08-25 15:10:13 -04:00
|
|
|
Demo.reset(demo);
|
2015-12-02 17:33:31 -05:00
|
|
|
Demo.setScene(demo,demo.sceneName = e.target.value);
|
2016-01-14 18:25:16 -05:00
|
|
|
|
|
|
|
if (demo.gui) {
|
|
|
|
Gui.update(demo.gui);
|
|
|
|
}
|
2014-03-25 11:31:05 -04:00
|
|
|
|
|
|
|
var scrollY = window.scrollY;
|
2015-08-25 15:10:13 -04:00
|
|
|
window.location.hash = demo.sceneName;
|
2014-03-25 11:31:05 -04:00
|
|
|
window.scrollY = scrollY;
|
2015-08-25 18:52:25 -04:00
|
|
|
Demo.setUpdateSourceLink(demo.sceneName);
|
2014-03-25 11:31:05 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
demoReset.addEventListener('click', function(e) {
|
2015-08-25 15:10:13 -04:00
|
|
|
Demo.reset(demo);
|
2015-12-02 17:33:31 -05:00
|
|
|
Demo.setScene(demo, demo.sceneName);
|
2016-01-14 18:25:16 -05:00
|
|
|
|
|
|
|
if (demo.gui) {
|
|
|
|
Gui.update(demo.gui);
|
|
|
|
}
|
|
|
|
|
2015-08-25 18:52:25 -04:00
|
|
|
Demo.setUpdateSourceLink(demo.sceneName);
|
2014-03-25 11:31:05 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-25 18:52:25 -04:00
|
|
|
Demo.setUpdateSourceLink = function(sceneName) {
|
|
|
|
var demoViewSource = document.getElementById('demo-view-source'),
|
|
|
|
sourceUrl = 'https://github.com/liabru/matter-js/blob/master/examples';
|
|
|
|
demoViewSource.setAttribute('href', sourceUrl + '/' + sceneName + '.js');
|
|
|
|
};
|
|
|
|
|
2015-12-02 17:33:31 -05:00
|
|
|
Demo.setManualControl = function(demo, isManual) {
|
|
|
|
var engine = demo.engine,
|
|
|
|
world = engine.world,
|
|
|
|
runner = demo.runner;
|
|
|
|
|
|
|
|
demo.isManual = isManual;
|
|
|
|
|
|
|
|
if (demo.isManual) {
|
|
|
|
Runner.stop(runner);
|
|
|
|
|
|
|
|
// continue rendering but not updating
|
|
|
|
(function render(time){
|
|
|
|
runner.frameRequestId = window.requestAnimationFrame(render);
|
|
|
|
Events.trigger(engine, 'beforeUpdate');
|
|
|
|
Events.trigger(engine, 'tick');
|
|
|
|
engine.render.controller.world(engine);
|
|
|
|
Events.trigger(engine, 'afterUpdate');
|
|
|
|
})();
|
|
|
|
} else {
|
|
|
|
Runner.stop(runner);
|
|
|
|
Runner.start(runner, engine);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
Demo.fullscreen = function(demo) {
|
|
|
|
var _fullscreenElement = demo.engine.render.canvas;
|
2014-03-25 11:31:05 -04:00
|
|
|
|
|
|
|
if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement) {
|
|
|
|
if (_fullscreenElement.requestFullscreen) {
|
|
|
|
_fullscreenElement.requestFullscreen();
|
|
|
|
} else if (_fullscreenElement.mozRequestFullScreen) {
|
|
|
|
_fullscreenElement.mozRequestFullScreen();
|
|
|
|
} else if (_fullscreenElement.webkitRequestFullscreen) {
|
|
|
|
_fullscreenElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
Demo.reset = function(demo) {
|
|
|
|
var world = demo.engine.world,
|
2015-08-16 19:48:52 -04:00
|
|
|
i;
|
2014-03-25 11:31:05 -04:00
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
World.clear(world);
|
|
|
|
Engine.clear(demo.engine);
|
2014-03-25 11:31:05 -04:00
|
|
|
|
|
|
|
// clear scene graph (if defined in controller)
|
2015-08-25 15:10:13 -04:00
|
|
|
if (demo.engine.render) {
|
|
|
|
var renderController = demo.engine.render.controller;
|
2015-08-15 15:39:13 -04:00
|
|
|
if (renderController && renderController.clear)
|
2015-08-25 15:10:13 -04:00
|
|
|
renderController.clear(demo.engine.render);
|
2015-08-15 15:39:13 -04:00
|
|
|
}
|
2014-03-25 11:31:05 -04:00
|
|
|
|
2014-04-30 05:33:37 -04:00
|
|
|
// clear all scene events
|
2015-08-25 15:10:13 -04:00
|
|
|
if (demo.engine.events) {
|
|
|
|
for (i = 0; i < demo.sceneEvents.length; i++)
|
|
|
|
Events.off(demo.engine, demo.sceneEvents[i]);
|
2015-08-16 19:48:52 -04:00
|
|
|
}
|
2014-06-21 12:44:25 -04:00
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
if (demo.mouseConstraint && demo.mouseConstraint.events) {
|
|
|
|
for (i = 0; i < demo.sceneEvents.length; i++)
|
|
|
|
Events.off(demo.mouseConstraint, demo.sceneEvents[i]);
|
2014-06-21 12:44:25 -04:00
|
|
|
}
|
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
if (world.events) {
|
|
|
|
for (i = 0; i < demo.sceneEvents.length; i++)
|
|
|
|
Events.off(world, demo.sceneEvents[i]);
|
2014-07-12 12:35:28 -04:00
|
|
|
}
|
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
if (demo.runner && demo.runner.events) {
|
|
|
|
for (i = 0; i < demo.sceneEvents.length; i++)
|
|
|
|
Events.off(demo.runner, demo.sceneEvents[i]);
|
2015-07-29 15:27:45 -04:00
|
|
|
}
|
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
if (demo.engine.render && demo.engine.render.events) {
|
|
|
|
for (i = 0; i < demo.sceneEvents.length; i++)
|
|
|
|
Events.off(demo.engine.render, demo.sceneEvents[i]);
|
2015-07-29 15:27:45 -04:00
|
|
|
}
|
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.sceneEvents = [];
|
2014-03-25 11:31:05 -04:00
|
|
|
|
2014-04-29 12:35:27 -04:00
|
|
|
// reset id pool
|
2015-12-25 10:57:27 -05:00
|
|
|
Body._nextCollidingGroupId = 1;
|
|
|
|
Body._nextNonCollidingGroupId = -1;
|
|
|
|
Body._nextCategory = 0x0001;
|
2014-04-29 12:35:27 -04:00
|
|
|
Common._nextId = 0;
|
|
|
|
|
2014-05-10 10:01:35 -04:00
|
|
|
// reset random seed
|
|
|
|
Common._seed = 0;
|
|
|
|
|
2014-05-04 07:43:29 -04:00
|
|
|
// reset mouse offset and scale (only required for Demo.views)
|
2015-08-25 15:10:13 -04:00
|
|
|
if (demo.mouseConstraint) {
|
|
|
|
Mouse.setScale(demo.mouseConstraint.mouse, { x: 1, y: 1 });
|
|
|
|
Mouse.setOffset(demo.mouseConstraint.mouse, { x: 0, y: 0 });
|
2015-08-15 15:39:13 -04:00
|
|
|
}
|
2014-05-01 08:44:44 -04:00
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.engine.enableSleeping = false;
|
|
|
|
demo.engine.world.gravity.y = 1;
|
|
|
|
demo.engine.world.gravity.x = 0;
|
|
|
|
demo.engine.timing.timeScale = 1;
|
2014-03-25 11:31:05 -04:00
|
|
|
|
|
|
|
var offset = 5;
|
2015-08-25 15:10:13 -04:00
|
|
|
World.add(world, [
|
2014-03-25 11:31:05 -04:00
|
|
|
Bodies.rectangle(400, -offset, 800.5 + 2 * offset, 50.5, { isStatic: true }),
|
|
|
|
Bodies.rectangle(400, 600 + offset, 800.5 + 2 * offset, 50.5, { isStatic: true }),
|
|
|
|
Bodies.rectangle(800 + offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true }),
|
|
|
|
Bodies.rectangle(-offset, 300, 50.5, 600.5 + 2 * offset, { isStatic: true })
|
|
|
|
]);
|
2014-03-26 07:53:41 -04:00
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
if (demo.mouseConstraint) {
|
|
|
|
World.add(world, demo.mouseConstraint);
|
2015-08-15 15:39:13 -04:00
|
|
|
}
|
2014-03-25 11:31:05 -04:00
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
if (demo.engine.render) {
|
|
|
|
var renderOptions = demo.engine.render.options;
|
2015-08-15 15:39:13 -04:00
|
|
|
renderOptions.wireframes = true;
|
|
|
|
renderOptions.hasBounds = false;
|
|
|
|
renderOptions.showDebug = false;
|
|
|
|
renderOptions.showBroadphase = false;
|
|
|
|
renderOptions.showBounds = false;
|
|
|
|
renderOptions.showVelocity = false;
|
|
|
|
renderOptions.showCollisions = false;
|
|
|
|
renderOptions.showAxes = false;
|
|
|
|
renderOptions.showPositions = false;
|
|
|
|
renderOptions.showAngleIndicator = true;
|
|
|
|
renderOptions.showIds = false;
|
|
|
|
renderOptions.showShadows = false;
|
|
|
|
renderOptions.showVertexNumbers = false;
|
|
|
|
renderOptions.showConvexHulls = false;
|
|
|
|
renderOptions.showInternalEdges = false;
|
|
|
|
renderOptions.showSeparations = false;
|
|
|
|
renderOptions.background = '#fff';
|
2014-03-25 11:31:05 -04:00
|
|
|
|
2015-08-16 19:48:52 -04:00
|
|
|
if (_isMobile) {
|
|
|
|
renderOptions.showDebug = true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 11:31:05 -04:00
|
|
|
};
|
|
|
|
|
2014-07-22 08:44:09 -04:00
|
|
|
})();
|