mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-27 09:50:52 -05:00
114 lines
No EOL
3 KiB
JavaScript
114 lines
No EOL
3 KiB
JavaScript
var Example = Example || {};
|
|
|
|
Example.sprites = function() {
|
|
var Engine = Matter.Engine,
|
|
Render = Matter.Render,
|
|
Runner = Matter.Runner,
|
|
Composites = Matter.Composites,
|
|
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: {
|
|
width: 800,
|
|
height: 600,
|
|
background: '#0f0f13',
|
|
showAngleIndicator: false,
|
|
wireframes: false
|
|
}
|
|
});
|
|
|
|
Render.run(render);
|
|
|
|
// create runner
|
|
var runner = Runner.create();
|
|
Runner.run(runner, engine);
|
|
|
|
// add bodies
|
|
var offset = 10,
|
|
options = {
|
|
isStatic: true
|
|
};
|
|
|
|
world.bodies = [];
|
|
|
|
// these static walls will not be rendered in this sprites example, see options
|
|
World.add(world, [
|
|
Bodies.rectangle(400, -offset, 800.5 + 2 * offset, 50.5, options),
|
|
Bodies.rectangle(400, 600 + offset, 800.5 + 2 * offset, 50.5, options),
|
|
Bodies.rectangle(800 + offset, 300, 50.5, 600.5 + 2 * offset, options),
|
|
Bodies.rectangle(-offset, 300, 50.5, 600.5 + 2 * offset, options)
|
|
]);
|
|
|
|
var stack = Composites.stack(20, 20, 10, 4, 0, 0, function(x, y) {
|
|
if (Common.random() > 0.35) {
|
|
return Bodies.rectangle(x, y, 64, 64, {
|
|
render: {
|
|
strokeStyle: '#ffffff',
|
|
sprite: {
|
|
texture: './img/box.png'
|
|
}
|
|
}
|
|
});
|
|
} else {
|
|
return Bodies.circle(x, y, 46, {
|
|
density: 0.0005,
|
|
frictionAir: 0.06,
|
|
restitution: 0.3,
|
|
friction: 0.01,
|
|
render: {
|
|
sprite: {
|
|
texture: './img/ball.png'
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
World.add(world, stack);
|
|
|
|
// add mouse control
|
|
var mouse = Mouse.create(render.canvas),
|
|
mouseConstraint = MouseConstraint.create(engine, {
|
|
mouse: mouse,
|
|
constraint: {
|
|
stiffness: 0.2,
|
|
render: {
|
|
visible: false
|
|
}
|
|
}
|
|
});
|
|
|
|
World.add(world, mouseConstraint);
|
|
|
|
// keep the mouse in sync with rendering
|
|
render.mouse = mouse;
|
|
|
|
// fit the render viewport to the scene
|
|
Render.lookAt(render, {
|
|
min: { x: 0, y: 0 },
|
|
max: { x: 800, y: 600 }
|
|
});
|
|
|
|
// context for MatterTools.Demo
|
|
return {
|
|
engine: engine,
|
|
runner: runner,
|
|
render: render,
|
|
canvas: render.canvas,
|
|
stop: function() {
|
|
Matter.Render.stop(render);
|
|
Matter.Runner.stop(runner);
|
|
}
|
|
};
|
|
}; |