2014-02-19 09:15:05 -05:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
// Matter aliases
|
|
|
|
var Engine = Matter.Engine,
|
|
|
|
World = Matter.World,
|
|
|
|
Bodies = Matter.Bodies,
|
|
|
|
Composites = Matter.Composites,
|
|
|
|
Common = Matter.Common,
|
2014-03-31 10:35:00 -04:00
|
|
|
MouseConstraint = Matter.MouseConstraint;
|
2014-02-19 09:15:05 -05:00
|
|
|
|
|
|
|
var Demo = {};
|
|
|
|
|
|
|
|
var _engine,
|
|
|
|
_sceneName = 'mixed',
|
|
|
|
_sceneWidth,
|
2015-07-02 15:15:06 -04:00
|
|
|
_sceneHeight,
|
|
|
|
_deviceOrientationEvent;
|
2014-02-19 09:15:05 -05:00
|
|
|
|
|
|
|
Demo.init = function() {
|
|
|
|
var canvasContainer = document.getElementById('canvas-container'),
|
|
|
|
demoStart = document.getElementById('demo-start');
|
|
|
|
|
|
|
|
demoStart.addEventListener('click', function() {
|
|
|
|
demoStart.style.display = 'none';
|
|
|
|
|
|
|
|
_engine = Engine.create(canvasContainer, {
|
|
|
|
render: {
|
|
|
|
options: {
|
|
|
|
wireframes: true,
|
2014-03-31 10:35:00 -04:00
|
|
|
showAngleIndicator: true,
|
|
|
|
showDebug: true
|
2014-02-19 09:15:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-03-31 10:35:00 -04:00
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
Demo.fullscreen();
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
Engine.run(_engine);
|
|
|
|
Demo.updateScene();
|
|
|
|
}, 800);
|
|
|
|
});
|
|
|
|
|
2015-07-02 15:15:06 -04:00
|
|
|
window.addEventListener('deviceorientation', function(event) {
|
|
|
|
_deviceOrientationEvent = event;
|
|
|
|
Demo.updateGravity(event);
|
|
|
|
}, true);
|
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
window.addEventListener('touchstart', Demo.fullscreen);
|
2015-07-02 15:15:06 -04:00
|
|
|
|
2014-02-19 09:15:05 -05:00
|
|
|
window.addEventListener('orientationchange', function() {
|
2015-07-02 15:15:06 -04:00
|
|
|
Demo.updateGravity(_deviceOrientationEvent);
|
2014-02-19 09:15:05 -05:00
|
|
|
Demo.updateScene();
|
|
|
|
Demo.fullscreen();
|
|
|
|
}, false);
|
|
|
|
};
|
|
|
|
|
2014-03-31 10:35:00 -04:00
|
|
|
window.addEventListener('load', Demo.init);
|
|
|
|
|
|
|
|
Demo.mixed = function() {
|
|
|
|
var _world = _engine.world;
|
|
|
|
|
|
|
|
Demo.reset();
|
|
|
|
|
|
|
|
World.add(_world, MouseConstraint.create(_engine));
|
|
|
|
|
|
|
|
var stack = Composites.stack(20, 20, 10, 5, 0, 0, function(x, y, column, row) {
|
|
|
|
switch (Math.round(Common.random(0, 1))) {
|
|
|
|
|
|
|
|
case 0:
|
2014-05-10 10:01:35 -04:00
|
|
|
if (Common.random() < 0.8) {
|
2014-03-31 10:35:00 -04:00
|
|
|
return Bodies.rectangle(x, y, Common.random(20, 40), Common.random(20, 40), { friction: 0.01, restitution: 0.4 });
|
|
|
|
} else {
|
|
|
|
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30), { friction: 0.01, restitution: 0.4 });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
return Bodies.polygon(x, y, Math.round(Common.random(4, 6)), Common.random(20, 40), { friction: 0.01, restitution: 0.4 });
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
World.add(_world, stack);
|
|
|
|
};
|
2014-02-19 09:15:05 -05:00
|
|
|
|
|
|
|
Demo.updateScene = function() {
|
|
|
|
if (!_engine)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_sceneWidth = document.documentElement.clientWidth;
|
|
|
|
_sceneHeight = document.documentElement.clientHeight;
|
|
|
|
|
|
|
|
var boundsMax = _engine.world.bounds.max,
|
|
|
|
renderOptions = _engine.render.options,
|
|
|
|
canvas = _engine.render.canvas;
|
|
|
|
|
|
|
|
boundsMax.x = _sceneWidth;
|
|
|
|
boundsMax.y = _sceneHeight;
|
|
|
|
|
|
|
|
canvas.width = renderOptions.width = _sceneWidth;
|
|
|
|
canvas.height = renderOptions.height = _sceneHeight;
|
|
|
|
|
|
|
|
Demo[_sceneName]();
|
|
|
|
};
|
|
|
|
|
2015-07-02 15:15:06 -04:00
|
|
|
Demo.updateGravity = function(event) {
|
2014-02-19 09:15:05 -05:00
|
|
|
if (!_engine)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var orientation = window.orientation,
|
|
|
|
gravity = _engine.world.gravity;
|
|
|
|
|
|
|
|
if (orientation === 0) {
|
|
|
|
gravity.x = Common.clamp(event.gamma, -90, 90) / 90;
|
|
|
|
gravity.y = Common.clamp(event.beta, -90, 90) / 90;
|
|
|
|
} else if (orientation === 180) {
|
|
|
|
gravity.x = Common.clamp(event.gamma, -90, 90) / 90;
|
|
|
|
gravity.y = Common.clamp(-event.beta, -90, 90) / 90;
|
|
|
|
} else if (orientation === 90) {
|
|
|
|
gravity.x = Common.clamp(event.beta, -90, 90) / 90;
|
|
|
|
gravity.y = Common.clamp(-event.gamma, -90, 90) / 90;
|
|
|
|
} else if (orientation === -90) {
|
|
|
|
gravity.x = Common.clamp(-event.beta, -90, 90) / 90;
|
|
|
|
gravity.y = Common.clamp(event.gamma, -90, 90) / 90;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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() {
|
|
|
|
var _world = _engine.world;
|
2014-05-10 10:01:35 -04:00
|
|
|
|
|
|
|
Common._seed = 2;
|
2014-02-19 09:15:05 -05:00
|
|
|
|
|
|
|
World.clear(_world);
|
|
|
|
Engine.clear(_engine);
|
|
|
|
|
|
|
|
var offset = 5;
|
|
|
|
World.addBody(_world, Bodies.rectangle(_sceneWidth * 0.5, -offset, _sceneWidth + 0.5, 50.5, { isStatic: true }));
|
|
|
|
World.addBody(_world, Bodies.rectangle(_sceneWidth * 0.5, _sceneHeight + offset, _sceneWidth + 0.5, 50.5, { isStatic: true }));
|
|
|
|
World.addBody(_world, Bodies.rectangle(_sceneWidth + offset, _sceneHeight * 0.5, 50.5, _sceneHeight + 0.5, { isStatic: true }));
|
|
|
|
World.addBody(_world, Bodies.rectangle(-offset, _sceneHeight * 0.5, 50.5, _sceneHeight + 0.5, { isStatic: true }));
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|