Matter.Render

    1. const fglayer = scene.layer('fglayer');
    2. const {Engine, World, Render, Runner, Common, Composites, Mouse, MouseConstraint, Bodies} = Matter;
    3. // create engine
    4. const engine = Engine.create(),
    5. world = engine.world;
    6. // create renderer
    7. const render = Render.create({
    8. layer: fglayer,
    9. engine,
    10. options: {
    11. showAngleIndicator: true,
    12. background: '#fff',
    13. wireframes: false,
    14. },
    15. });
    16. Render.run(render);
    17. // create runner
    18. const runner = Runner.create();
    19. Runner.run(runner, engine);
    20. // add bodies
    21. const stack = Composites.stack(20, 20, 10, 5, 0, 0, (x, y) => {
    22. let sides = Math.round(Common.random(1, 8));
    23. // triangles can be a little unstable, so avoid until fixed
    24. // round the edges of some bodies
    25. let chamfer = null;
    26. if(sides > 2 && Common.random() > 0.7) {
    27. chamfer = {
    28. radius: 10,
    29. };
    30. }
    31. const width = 64;
    32. switch (Math.round(Common.random(0, 1))) {
    33. case 0:
    34. if(Common.random() < 0.6) {
    35. return Bodies.rectangle(x, y, Common.random(25, 50), Common.random(25, 50), {chamfer});
    36. } if(Common.random() < 0.8) {
    37. return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(25, 30), {chamfer});
    38. }
    39. return Bodies.rectangle(x, y, width, width, {
    40. chamfer,
    41. render: {
    42. sprite: {
    43. attrs: {
    44. textures: {
    45. src: 'https://p5.ssl.qhimg.com/t01bd0523f7bc9241c2.png',
    46. srcRect: [32, 32, 64, 64],
    47. },
    48. size: [width, width],
    49. },
    50. },
    51. },
    52. });
    53. return Bodies.polygon(x, y, sides, Common.random(25, 50), {chamfer});
    54. default:
    55. break;
    56. }
    57. });
    58. World.add(world, stack);
    59. World.add(world, [
    60. // walls
    61. Bodies.rectangle(400, 0, 800, 50, {isStatic: true}),
    62. Bodies.rectangle(400, 600, 800, 50, {isStatic: true}),
    63. Bodies.rectangle(800, 300, 50, 600, {isStatic: true}),
    64. Bodies.rectangle(0, 300, 50, 600, {isStatic: true}),
    65. ]);
    66. // add mouse control
    67. const mouse = Mouse.create(render.canvas),
    68. mouseConstraint = MouseConstraint.create(engine, {
    69. mouse,
    70. constraint: {
    71. stiffness: 0.2,
    72. render: {
    73. visible: false,
    74. },
    75. },
    76. });
    77. World.add(world, mouseConstraint);
    78. // keep the mouse in sync with rendering