0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00
liabru-matter-js/examples/sprites.js

114 lines
3 KiB
JavaScript
Raw Normal View History

2016-11-20 19:41:57 -05:00
var Example = Example || {};
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
Example.sprites = function() {
var Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
2015-08-25 14:31:44 -04:00
Composites = Matter.Composites,
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: {
width: 800,
height: 600,
2016-11-20 19:41:57 -05:00
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 = {
2017-01-19 18:36:34 -05:00
isStatic: true
2016-11-20 19:41:57 -05:00
};
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, {
2015-08-25 14:12:52 -04:00
render: {
2016-11-20 19:41:57 -05:00
strokeStyle: '#ffffff',
sprite: {
texture: './img/box.png'
2015-08-25 14:12:52 -04:00
}
2016-11-20 19:41:57 -05:00
}
});
} 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'
2015-08-25 14:12:52 -04:00
}
2016-11-20 19:41:57 -05:00
}
});
}
});
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
}
2015-08-25 14:12:52 -04:00
}
});
2016-11-20 19:41:57 -05:00
World.add(world, mouseConstraint);
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// keep the mouse in sync with rendering
render.mouse = mouse;
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// 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);
}
};
};