0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-27 13:59:01 -05:00

added module export to examples

This commit is contained in:
liabru 2019-09-14 18:54:50 +01:00
parent 6b698ea2f6
commit f9ea7995bf
44 changed files with 347 additions and 122 deletions

View file

@ -78,4 +78,8 @@ Example.airFriction = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -89,4 +89,8 @@ Example.avalanche = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -97,4 +97,8 @@ Example.ballPool = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -122,4 +122,8 @@ Example.bridge = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -93,4 +93,8 @@ Example.broadphase = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -87,4 +87,8 @@ Example.car = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -94,4 +94,8 @@ Example.catapult = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -120,4 +120,8 @@ Example.chains = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -79,4 +79,8 @@ Example.circleStack = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -82,4 +82,8 @@ Example.cloth = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -153,4 +153,8 @@ Example.collisionFiltering = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -105,4 +105,8 @@ Example.compositeManipulation = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -104,4 +104,8 @@ Example.compound = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -87,4 +87,8 @@ Example.compoundStack = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -93,4 +93,8 @@ Example.concave = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -177,4 +177,8 @@ Example.constraints = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -150,4 +150,8 @@ Example.doublePendulum = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -176,4 +176,8 @@ Example.events = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -88,4 +88,8 @@ Example.friction = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -94,4 +94,8 @@ Example.gravity = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -68,26 +68,28 @@ Example.gyro = function() {
]);
// add gyro control
var updateGravity = function(event) {
var orientation = typeof window.orientation !== 'undefined' ? window.orientation : 0,
gravity = engine.world.gravity;
if (typeof window !== 'undefined') {
var updateGravity = function(event) {
var orientation = typeof window.orientation !== 'undefined' ? window.orientation : 0,
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;
}
};
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;
}
};
window.addEventListener('deviceorientation', updateGravity);
window.addEventListener('deviceorientation', updateGravity);
}
// add mouse control
var mouse = Mouse.create(render.canvas),
@ -121,7 +123,13 @@ Example.gyro = function() {
stop: function() {
Matter.Render.stop(render);
Matter.Runner.stop(runner);
window.removeEventListener('deviceorientation', updateGravity);
if (typeof window !== 'undefined') {
window.removeEventListener('deviceorientation', updateGravity);
}
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

45
examples/index.js Normal file
View file

@ -0,0 +1,45 @@
module.exports = {
airFriction: require('./airFriction.js'),
avalanche: require('./avalanche.js'),
ballPool: require('./ballPool.js'),
bridge: require('./bridge.js'),
broadphase: require('./broadphase.js'),
car: require('./car.js'),
catapult: require('./catapult.js'),
chains: require('./chains.js'),
circleStack: require('./circleStack.js'),
cloth: require('./cloth.js'),
collisionFiltering: require('./collisionFiltering.js'),
compositeManipulation: require('./compositeManipulation.js'),
compound: require('./compound.js'),
compoundStack: require('./compoundStack.js'),
concave: require('./concave.js'),
constraints: require('./constraints.js'),
doublePendulum: require('./doublePendulum.js'),
events: require('./events.js'),
friction: require('./friction.js'),
gravity: require('./gravity.js'),
gyro: require('./gyro.js'),
manipulation: require('./manipulation.js'),
mixed: require('./mixed.js'),
newtonsCradle: require('./newtonsCradle.js'),
ragdoll: require('./ragdoll.js'),
pyramid: require('./pyramid.js'),
raycasting: require('./raycasting.js'),
restitution: require('./restitution.js'),
rounded: require('./rounded.js'),
sensors: require('./sensors.js'),
sleeping: require('./sleeping.js'),
slingshot: require('./slingshot.js'),
softBody: require('./softBody.js'),
sprites: require('./sprites.js'),
stack: require('./stack.js'),
staticFriction: require('./staticFriction.js'),
stress: require('./stress.js'),
stress2: require('./stress2.js'),
svg: require('./svg.js'),
terrain: require('./terrain.js'),
timescale: require('./timescale.js'),
views: require('./views.js'),
wreckingBall: require('./wreckingBall.js')
};

View file

@ -138,4 +138,8 @@ Example.manipulation = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -103,4 +103,8 @@ Example.mixed = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -74,4 +74,8 @@ Example.newtonsCradle = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -79,4 +79,8 @@ Example.pyramid = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -495,4 +495,8 @@ Example.ragdoll.ragdoll = function(x, y, scale, options) {
});
return person;
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -133,4 +133,8 @@ Example.raycasting = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -83,4 +83,8 @@ Example.restitution = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -107,4 +107,8 @@ Example.rounded = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -129,4 +129,8 @@ Example.sensors = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -102,4 +102,8 @@ Example.sleeping = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -98,4 +98,8 @@ Example.slingshot = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -83,4 +83,8 @@ Example.softBody = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -111,4 +111,8 @@ Example.sprites = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -79,4 +79,8 @@ Example.stack = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -104,4 +104,8 @@ Example.staticFriction = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -77,4 +77,8 @@ Example.stress = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -77,4 +77,8 @@ Example.stress2 = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -40,44 +40,46 @@ Example.svg = function() {
'iconmonstr-user-icon'
];
for (var i = 0; i < svgs.length; i += 1) {
(function(i) {
$.get('./svg/' + svgs[i] + '.svg').done(function(data) {
var vertexSets = [],
color = Common.choose(['#556270', '#4ECDC4', '#C7F464', '#FF6B6B', '#C44D58']);
if (typeof $ !== 'undefined') {
for (var i = 0; i < svgs.length; i += 1) {
(function(i) {
$.get('./svg/' + svgs[i] + '.svg').done(function(data) {
var vertexSets = [],
color = Common.choose(['#556270', '#4ECDC4', '#C7F464', '#FF6B6B', '#C44D58']);
$(data).find('path').each(function(i, path) {
var points = Svg.pathToVertices(path, 30);
vertexSets.push(Vertices.scale(points, 0.4, 0.4));
$(data).find('path').each(function(i, path) {
var points = Svg.pathToVertices(path, 30);
vertexSets.push(Vertices.scale(points, 0.4, 0.4));
});
World.add(world, Bodies.fromVertices(100 + i * 150, 200 + i * 50, vertexSets, {
render: {
fillStyle: color,
strokeStyle: color,
lineWidth: 1
}
}, true));
});
})(i);
}
World.add(world, Bodies.fromVertices(100 + i * 150, 200 + i * 50, vertexSets, {
render: {
fillStyle: color,
strokeStyle: color,
lineWidth: 1
}
}, true));
$.get('./svg/svg.svg').done(function(data) {
var vertexSets = [],
color = Common.choose(['#556270', '#4ECDC4', '#C7F464', '#FF6B6B', '#C44D58']);
$(data).find('path').each(function(i, path) {
vertexSets.push(Svg.pathToVertices(path, 30));
});
})(i);
}
$.get('./svg/svg.svg').done(function(data) {
var vertexSets = [],
color = Common.choose(['#556270', '#4ECDC4', '#C7F464', '#FF6B6B', '#C44D58']);
$(data).find('path').each(function(i, path) {
vertexSets.push(Svg.pathToVertices(path, 30));
World.add(world, Bodies.fromVertices(400, 80, vertexSets, {
render: {
fillStyle: color,
strokeStyle: color,
lineWidth: 1
}
}, true));
});
World.add(world, Bodies.fromVertices(400, 80, vertexSets, {
render: {
fillStyle: color,
strokeStyle: color,
lineWidth: 1
}
}, true));
});
}
World.add(world, [
Bodies.rectangle(400, 0, 800, 50, { isStatic: true }),
@ -120,4 +122,8 @@ Example.svg = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -36,36 +36,38 @@ Example.terrain = function() {
// add bodies
var terrain;
$.get('./svg/terrain.svg').done(function(data) {
var vertexSets = [];
if (typeof $ !== 'undefined') {
$.get('./svg/terrain.svg').done(function(data) {
var vertexSets = [];
$(data).find('path').each(function(i, path) {
vertexSets.push(Svg.pathToVertices(path, 30));
$(data).find('path').each(function(i, path) {
vertexSets.push(Svg.pathToVertices(path, 30));
});
terrain = Bodies.fromVertices(400, 350, vertexSets, {
isStatic: true,
render: {
fillStyle: '#2e2b44',
strokeStyle: '#2e2b44',
lineWidth: 1
}
}, true);
World.add(world, terrain);
var bodyOptions = {
frictionAir: 0,
friction: 0.0001,
restitution: 0.6
};
World.add(world, Composites.stack(80, 100, 20, 20, 10, 10, function(x, y) {
if (Query.point([terrain], { x: x, y: y }).length === 0) {
return Bodies.polygon(x, y, 5, 12, bodyOptions);
}
}));
});
terrain = Bodies.fromVertices(400, 350, vertexSets, {
isStatic: true,
render: {
fillStyle: '#2e2b44',
strokeStyle: '#2e2b44',
lineWidth: 1
}
}, true);
World.add(world, terrain);
var bodyOptions = {
frictionAir: 0,
friction: 0.0001,
restitution: 0.6
};
World.add(world, Composites.stack(80, 100, 20, 20, 10, 10, function(x, y) {
if (Query.point([terrain], { x: x, y: y }).length === 0) {
return Bodies.polygon(x, y, 5, 12, bodyOptions);
}
}));
});
}
// add mouse control
var mouse = Mouse.create(render.canvas),
@ -101,4 +103,8 @@ Example.terrain = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example;
}

View file

@ -149,4 +149,8 @@ Example.timescale = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -180,4 +180,8 @@ Example.views = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -91,4 +91,8 @@ Example.wreckingBall = function() {
Matter.Runner.stop(runner);
}
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}