0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-01-13 16:18:50 -05:00
liabru-matter-js/examples/attractors.js

111 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-11-20 19:41:57 -05:00
var Example = Example || {};
2016-09-03 18:27:44 -04:00
2017-01-19 18:36:34 -05:00
Matter.use(
'matter-gravity',
'matter-wrap'
);
2016-11-20 19:41:57 -05:00
2017-01-19 18:36:34 -05:00
Example.attractors = function() {
2016-11-20 19:41:57 -05:00
var Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
2016-09-03 18:27:44 -04:00
Body = Matter.Body,
2016-11-20 19:41:57 -05:00
Common = Matter.Common,
MouseConstraint = Matter.MouseConstraint,
Mouse = Matter.Mouse,
World = Matter.World,
Bodies = Matter.Bodies;
// create engine
var engine = Engine.create(),
world = engine.world;
// create renderer
var render = Render.create({
element: document.body,
engine: engine,
options: {
2016-11-27 20:13:22 -05:00
width: Math.min(document.documentElement.clientWidth, 800),
height: Math.min(document.documentElement.clientHeight, 600)
2016-11-20 19:41:57 -05:00
}
});
Render.run(render);
// create runner
var runner = Runner.create();
Runner.run(runner, engine);
2016-09-03 18:27:44 -04:00
2016-11-20 19:41:57 -05:00
// add bodies
world.bodies = [];
world.gravity.scale = 0;
2016-09-03 18:27:44 -04:00
2016-11-20 19:41:57 -05:00
var G = 0.001;
2017-01-19 18:36:34 -05:00
engine.timing.timeScale = 1.5;
for (var i = 0; i < 150; i += 1) {
var radius = Common.random(6, 10);
2016-11-20 19:41:57 -05:00
var body = Bodies.circle(
Common.random(10, render.options.width),
Common.random(10, render.options.height),
2017-01-19 18:36:34 -05:00
radius,
2016-11-20 19:41:57 -05:00
{
2017-01-19 18:36:34 -05:00
mass: Common.random(10, 15),
2016-11-20 19:41:57 -05:00
frictionAir: 0,
2017-01-24 15:34:26 -05:00
plugin: {
gravity: G,
wrap: {
min: { x: 0, y: 0 },
max: { x: render.options.width, y: render.options.height }
}
2016-11-20 19:41:57 -05:00
}
}
2016-09-03 18:27:44 -04:00
);
2017-01-19 18:36:34 -05:00
var speed = 5;
2016-11-20 19:41:57 -05:00
Body.setVelocity(body, {
2017-01-19 18:36:34 -05:00
x: Common.random(-speed, speed),
y: Common.random(-speed, speed)
2016-11-20 19:41:57 -05:00
});
World.add(world, body);
}
// add mouse control
var mouse = Mouse.create(render.canvas),
mouseConstraint = MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
2016-09-03 18:27:44 -04:00
}
2016-11-20 19:41:57 -05:00
}
});
World.add(world, mouseConstraint);
2016-09-03 18:27:44 -04:00
2016-11-20 19:41:57 -05:00
// keep the mouse in sync with rendering
render.mouse = mouse;
2016-09-03 18:27:44 -04:00
2016-11-27 20:13:22 -05:00
// fit the render viewport to the scene
Render.lookAt(render, {
min: { x: 0, y: 0 },
max: { x: 800, y: 600 }
});
2016-11-20 19:41:57 -05:00
// context for MatterTools.Demo
return {
engine: engine,
runner: runner,
render: render,
canvas: render.canvas,
stop: function() {
Matter.Render.stop(render);
Matter.Runner.stop(runner);
2016-09-03 18:27:44 -04:00
}
};
2016-11-20 19:41:57 -05:00
};