0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-24 09:36:48 -05:00
liabru-matter-js/demo/js/Demo.js

311 lines
10 KiB
JavaScript
Raw Normal View History

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-15 15:39:13 -04:00
Matter = _isBrowser ? window.Matter : require('../../build/matter-dev.js');
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;
// 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
var _engine,
_runner,
2014-02-19 09:15:05 -05:00
_gui,
2014-04-25 12:00:42 -04:00
_inspector,
2014-03-20 11:05:42 -04:00
_sceneName,
_mouseConstraint,
_sceneEvents = [],
2015-08-15 15:39:13 -04:00
_useInspector = _isBrowser && window.location.hash.indexOf('-inspect') !== -1,
_isMobile = _isBrowser && /(ipad|iphone|ipod|android)/gi.test(navigator.userAgent),
2015-08-16 19:48:52 -04:00
_isAutomatedTest = !_isBrowser || window._phantom;
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 {
engine: _engine,
runner: _runner,
mouseConstraint: _mouseConstraint,
sceneEvents: _sceneEvents,
isMobile: _isMobile
};
};
2014-02-19 09:15:05 -05:00
Demo.init = function() {
// some example engine options
2014-02-19 09:15:05 -05:00
var options = {
positionIterations: 6,
velocityIterations: 4,
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');
_engine = Engine.create(container, options);
// add a mouse controlled constraint
_mouseConstraint = MouseConstraint.create(_engine);
World.add(_engine.world, _mouseConstraint);
} else {
_engine = Engine.create(options);
_engine.render = {};
_engine.render.options = {};
}
// engine reference for external use
Matter.Demo._engine = _engine;
// skip runner when performing automated tests
if (_isAutomatedTest) return;
2014-02-19 09:15:05 -05:00
// run the engine
_runner = Engine.run(_engine);
2014-02-19 09:15:05 -05:00
// default scene function name
_sceneName = 'mixed';
// get the scene function name from hash
if (window.location.hash.length !== 0)
_sceneName = window.location.hash.replace('#', '').replace('-inspect', '');
2014-02-19 09:15:05 -05:00
// set up a scene with bodies
2015-08-25 14:12:52 -04:00
Demo.reset();
Example[_sceneName](Demo.create());
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)
2014-02-19 09:15:05 -05:00
Demo.initControls();
};
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
Demo.initControls = function() {
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) {
_gui = Gui.create(_engine, _runner);
2014-03-25 11:31:05 -04:00
// need to add mouse constraint back in after gui clear or load is pressed
Events.on(_gui, 'clear load', function() {
_mouseConstraint = MouseConstraint.create(_engine);
World.add(_engine.world, _mouseConstraint);
});
// need to rebind mouse on render change
Events.on(_gui, 'setRenderer', function() {
Mouse.setElement(_mouseConstraint.mouse, _engine.render.canvas);
});
2014-04-29 08:07:53 -04:00
}
// create a Matter.Inspector
if (!_isMobile && Inspector && _useInspector) {
_inspector = Inspector.create(_engine, _runner);
2014-04-28 07:50:56 -04:00
Events.on(_inspector, 'import', function() {
_mouseConstraint = MouseConstraint.create(_engine);
World.add(_engine.world, _mouseConstraint);
});
2014-05-03 11:54:22 -04:00
Events.on(_inspector, 'play', function() {
_mouseConstraint = MouseConstraint.create(_engine);
World.add(_engine.world, _mouseConstraint);
});
2014-04-28 07:50:56 -04:00
Events.on(_inspector, 'selectStart', function() {
_mouseConstraint.constraint.render.visible = false;
});
Events.on(_inspector, 'selectEnd', function() {
_mouseConstraint.constraint.render.visible = true;
});
}
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';
_engine.render.canvas.addEventListener('touchstart', Demo.fullscreen);
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
demoSelect.value = _sceneName;
demoSelect.addEventListener('change', function(e) {
2015-08-25 14:12:52 -04:00
Demo.reset();
Example[_sceneName = e.target.value](Demo.create());
2014-03-25 11:31:05 -04:00
Gui.update(_gui);
var scrollY = window.scrollY;
window.location.hash = _sceneName;
window.scrollY = scrollY;
});
demoReset.addEventListener('click', function(e) {
2015-08-25 14:12:52 -04:00
Demo.reset();
Example[_sceneName](Demo.create());
2014-03-25 11:31:05 -04:00
Gui.update(_gui);
});
};
Demo.fullscreen = function(){
var _fullscreenElement = _engine.render.canvas;
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);
}
}
};
Demo.reset = function() {
2015-08-16 19:48:52 -04:00
var _world = _engine.world,
i;
2014-03-25 11:31:05 -04:00
World.clear(_world);
Engine.clear(_engine);
// clear scene graph (if defined in controller)
2015-08-15 15:39:13 -04:00
if (_engine.render) {
var renderController = _engine.render.controller;
if (renderController && renderController.clear)
renderController.clear(_engine.render);
}
2014-03-25 11:31:05 -04:00
// clear all scene events
2015-08-16 19:48:52 -04:00
if (_engine.events) {
for (i = 0; i < _sceneEvents.length; i++)
Events.off(_engine, _sceneEvents[i]);
}
2015-08-15 15:39:13 -04:00
if (_mouseConstraint && _mouseConstraint.events) {
2014-06-21 18:23:41 -04:00
for (i = 0; i < _sceneEvents.length; i++)
Events.off(_mouseConstraint, _sceneEvents[i]);
}
if (_world.events) {
for (i = 0; i < _sceneEvents.length; i++)
Events.off(_world, _sceneEvents[i]);
}
2015-08-12 18:27:39 -04:00
if (_runner && _runner.events) {
for (i = 0; i < _sceneEvents.length; i++)
Events.off(_runner, _sceneEvents[i]);
}
2015-08-15 15:39:13 -04:00
if (_engine.render && _engine.render.events) {
for (i = 0; i < _sceneEvents.length; i++)
Events.off(_engine.render, _sceneEvents[i]);
}
_sceneEvents = [];
2014-03-25 11:31:05 -04:00
2014-04-29 12:35:27 -04:00
// reset id pool
Common._nextId = 0;
// 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-15 15:39:13 -04:00
if (_mouseConstraint) {
Mouse.setScale(_mouseConstraint.mouse, { x: 1, y: 1 });
Mouse.setOffset(_mouseConstraint.mouse, { x: 0, y: 0 });
}
2014-05-01 08:44:44 -04:00
2014-03-25 11:31:05 -04:00
_engine.enableSleeping = false;
_engine.world.gravity.y = 1;
_engine.world.gravity.x = 0;
_engine.timing.timeScale = 1;
2014-03-25 11:31:05 -04:00
var offset = 5;
World.add(_world, [
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 })
]);
2015-08-15 15:39:13 -04:00
if (_mouseConstraint) {
World.add(_world, _mouseConstraint);
}
2014-03-25 11:31:05 -04:00
2015-08-15 15:39:13 -04:00
if (_engine.render) {
var renderOptions = _engine.render.options;
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
};
})();