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

73 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-10-15 18:23:35 -04:00
(function() {
var Body = Matter.Body,
Common = Matter.Common,
Composite = Matter.Composite;
var MatterWrap = {
name: 'matter-wrap',
version: '0.1.0',
for: 'matter-js@^0.10.0',
install: function(base) {
2016-11-02 20:49:22 -04:00
base.after('Engine.update', function() {
MatterWrap.Engine.update(this);
});
2016-10-15 18:23:35 -04:00
},
Engine: {
update: function(engine) {
var world = engine.world,
bodies = Composite.allBodies(world);
for (var i = 0; i < bodies.length; i += 1) {
var body = bodies[i];
if (body.wrap) {
MatterWrap.Body.wrap(body, body.wrap);
}
}
}
},
Body: {
wrap: function(body, bounds) {
var x = null,
y = null;
if (typeof bounds.min.x !== 'undefined' && typeof bounds.max.x !== 'undefined') {
if (body.bounds.min.x > bounds.max.x) {
x = bounds.min.x - (body.bounds.max.x - body.position.x);
} else if (body.bounds.max.x < bounds.min.x) {
x = bounds.max.x - (body.bounds.min.x - body.position.x);
}
}
if (typeof bounds.min.y !== 'undefined' && typeof bounds.max.y !== 'undefined') {
if (body.bounds.min.y > bounds.max.y) {
y = bounds.min.y - (body.bounds.max.y - body.position.y);
} else if (body.bounds.max.y < bounds.min.y) {
y = bounds.max.y - (body.bounds.min.y - body.position.y);
}
}
if (x !== null || y !== null) {
Body.setPosition(body, {
x: x || body.position.x,
y: y || body.position.y
});
}
}
}
};
Matter.Plugin.register(MatterWrap);
2016-11-03 20:04:56 -04:00
if (typeof window !== 'undefined') {
window.MatterWrap = MatterWrap;
}
2016-10-15 18:23:35 -04:00
})();