0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-30 10:20:52 -05:00
liabru-matter-js/demo/js/Demo.js

303 lines
11 KiB
JavaScript
Raw Normal View History

/**
* 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-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
2015-08-25 18:31:10 -04:00
Demo.create = function(options) {
var defaults = {
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();
2015-08-25 18:31:10 -04:00
// get container element for the canvas
demo.container = document.getElementById('canvas-container');
2015-08-25 18:31:10 -04:00
// create an example engine (see /examples/engine.js)
demo.engine = Example.engine(demo);
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);
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);
2015-08-25 18:31:10 -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
}
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
// 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);
});
// 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);
});
2014-04-29 08:07:53 -04:00
}
// create a Matter.Inspector
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-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;
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);
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;
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);
Example[demo.sceneName](demo);
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-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
// 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
}
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]);
}
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]);
}
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-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-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;
// 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 })
]);
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
};
})();