0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-28 14:09: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

@ -79,3 +79,7 @@ Example.airFriction = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -90,3 +90,7 @@ Example.avalanche = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -98,3 +98,7 @@ Example.ballPool = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -123,3 +123,7 @@ Example.bridge = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -94,3 +94,7 @@ Example.broadphase = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -88,3 +88,7 @@ Example.car = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -95,3 +95,7 @@ Example.catapult = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -121,3 +121,7 @@ Example.chains = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -80,3 +80,7 @@ Example.circleStack = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -83,3 +83,7 @@ Example.cloth = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -154,3 +154,7 @@ Example.collisionFiltering = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -106,3 +106,7 @@ Example.compositeManipulation = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -105,3 +105,7 @@ Example.compound = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -88,3 +88,7 @@ Example.compoundStack = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -94,3 +94,7 @@ Example.concave = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -178,3 +178,7 @@ Example.constraints = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -151,3 +151,7 @@ Example.doublePendulum = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -177,3 +177,7 @@ Example.events = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -89,3 +89,7 @@ Example.friction = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -95,3 +95,7 @@ Example.gravity = function() {
}
};
};
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

@ -139,3 +139,7 @@ Example.manipulation = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -104,3 +104,7 @@ Example.mixed = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -75,3 +75,7 @@ Example.newtonsCradle = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -80,3 +80,7 @@ Example.pyramid = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -496,3 +496,7 @@ 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

@ -134,3 +134,7 @@ Example.raycasting = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -84,3 +84,7 @@ Example.restitution = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -108,3 +108,7 @@ Example.rounded = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -130,3 +130,7 @@ Example.sensors = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -103,3 +103,7 @@ Example.sleeping = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -99,3 +99,7 @@ Example.slingshot = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -84,3 +84,7 @@ Example.softBody = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -112,3 +112,7 @@ Example.sprites = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -80,3 +80,7 @@ Example.stack = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -105,3 +105,7 @@ Example.staticFriction = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -78,3 +78,7 @@ Example.stress = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -78,3 +78,7 @@ Example.stress2 = function() {
}
};
};
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 }),
@ -121,3 +123,7 @@ Example.svg = function() {
}
};
};
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),
@ -102,3 +104,7 @@ Example.terrain = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example;
}

View file

@ -150,3 +150,7 @@ Example.timescale = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -181,3 +181,7 @@ Example.views = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}

View file

@ -92,3 +92,7 @@ Example.wreckingBall = function() {
}
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = Example[Object.keys(Example)[0]];
}