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-08-25 14:12:52 -04:00
|
|
|
var Example = Matter.Example,
|
|
|
|
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,
|
|
|
|
MouseConstraint = Matter.MouseConstraint;
|
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
|
|
|
|
2014-03-25 11:31:05 -04:00
|
|
|
// initialise the demo
|
|
|
|
|
2015-08-25 14:12:52 -04:00
|
|
|
Demo.create = function() {
|
|
|
|
return {
|
2015-08-25 15:10:13 -04:00
|
|
|
isMobile: _isMobile,
|
|
|
|
sceneEvents: []
|
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();
|
|
|
|
|
2014-03-26 07:53:41 -04:00
|
|
|
// some example engine options
|
2014-02-19 09:15:05 -05:00
|
|
|
var options = {
|
|
|
|
positionIterations: 6,
|
|
|
|
velocityIterations: 4,
|
2015-04-12 19:27:14 -04:00
|
|
|
enableSleeping: false,
|
|
|
|
metrics: { extended: true }
|
2014-02-19 09:15:05 -05:00
|
|
|
};
|
|
|
|
|
2014-03-20 11:05:42 -04:00
|
|
|
// create a Matter engine
|
2014-02-19 09:15:05 -05:00
|
|
|
// NOTE: this is actually Matter.Engine.create(), see the aliases at top of this file
|
2015-08-15 15:39:13 -04:00
|
|
|
if (_isBrowser) {
|
|
|
|
var container = document.getElementById('canvas-container');
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.engine = Engine.create(container, options);
|
2015-08-15 15:39:13 -04:00
|
|
|
|
|
|
|
// add a mouse controlled constraint
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.mouseConstraint = MouseConstraint.create(demo.engine);
|
|
|
|
World.add(demo.engine.world, demo.mouseConstraint);
|
2015-08-15 15:39:13 -04:00
|
|
|
} else {
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.engine = Engine.create(options);
|
|
|
|
demo.engine.render = {};
|
|
|
|
demo.engine.render.options = {};
|
2015-08-15 15:39:13 -04:00
|
|
|
}
|
2014-03-26 07:53:41 -04:00
|
|
|
|
2015-08-25 15:10:13 -04:00
|
|
|
// demo instance reference for external use
|
|
|
|
Matter.Demo._demo = demo;
|
2015-08-04 16:20:20 -04:00
|
|
|
|
|
|
|
// skip runner when performing automated tests
|
|
|
|
if (_isAutomatedTest) return;
|
|
|
|
|
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
|
|
|
|
|
|
|
// default scene function name
|
2015-08-25 15:10:13 -04:00
|
|
|
demo.sceneName = 'mixed';
|
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);
|
|
|
|
Example[demo.sceneName](demo);
|
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);
|
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-16 19:48:52 -04:00
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
if (window.addEventListener) {
|
|
|
|
window.addEventListener('load', Demo.init);
|
|
|
|
} else if (window.attachEvent) {
|
|
|
|
window.attachEvent('load', Demo.init);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialise demo selector
|
2015-08-25 15:10:13 -04:00
|
|
|
demoSelect.value = 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);
|
|
|
|
Example[demo.sceneName = e.target.value](demo);
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
demoReset.addEventListener('click', function(e) {
|
2015-08-25 15:10:13 -04:00
|
|
|
Demo.reset(demo);
|
|
|
|
Example[demo.sceneName](demo);
|
|
|
|
Gui.update(demo.gui);
|
2014-03-25 11:31:05 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
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
|
|
|
})();
|