javascript - matterJS 渲染到 Canvas

标签 javascript html canvas game-physics matter.js

我尝试使用 matterJS 并将其渲染到我布局中的特定 Canvas ,但我无法管理它。

这是 JS:

window.addEventListener("load",init);
    function init(){

    var canvas = document.getElementById('matterJS');
    var width = 100,
        height = 100;

    canvas.width = width;
    canvas.height = height; 
        // Matter.js - http://brm.io/matter-js/

        // Matter module aliases
        var Engine = Matter.Engine,
            World = Matter.World,
            Body = Matter.Body,
            Composites = Matter.Composites,
            MouseConstraint = Matter.MouseConstraint;

        // create a Matter.js engine
        var engine = Engine.create({
          render: {
            element: document.body,
            canvas: canvas,
            options: {
              width: 100,
              height: 100,                  
              showAngleIndicator: true,
              showVelocity: true,
              wireframes: false
            }
          }
        });

        // add a mouse controlled constraint
        var mouseConstraint = MouseConstraint.create(engine);
        World.add(engine.world, mouseConstraint);

        // add a Newton's Cradle (using the Composites factory method!)
        var cradle = Composites.newtonsCradle(280, 150, 5, 30, 200);
        World.add(engine.world, cradle);

        // offset the first body in the cradle so it will swing
        Body.translate(cradle.bodies[0], { x: -180, y: -100 });

        // run the engine
        Engine.run(engine);
   }

在我的 html 中,我这样做:

<canvas class="matterJS"></canvas>

我没有收到任何错误,但也没有任何渲染。我认为这应该可行。但为什么没有呢?

我只能在我这样创建引擎时渲染结果:

var engine = Engine.create(document.getElementById('matterJS'),{
                options: {
                  width: 100,
                  height: 100,                  
                  showAngleIndicator: true,
                  showVelocity: true,
                  wireframes: false
              }
            });

最佳答案

现在是这样解决的:

var canvas = document.getElementById('matterJS');
var width = 800,
    height = 800;

canvas.width = width;
canvas.height = height; 
    // Matter.js - http://brm.io/matter-js/

    // Matter module aliases
    var Engine = Matter.Engine,
        World = Matter.World,
        Body = Matter.Body,
        Composites = Matter.Composites,
        MouseConstraint = Matter.MouseConstraint;

    // create a Matter.js engine
    var engine = Engine.create(canvas, {
        options: {
          width: 1000,
          height: 1000,                 
          showAngleIndicator: true,
          showVelocity: true,
          wireframes: false
      }
    });

关于javascript - matterJS 渲染到 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981126/

相关文章:

javascript - 为什么当我取消静音后,自动播放会停止工作?

javascript - 在 Canvas 中填充更多类型的相似对象

javascript - Bootstrap Grid 将内容调整到页面上的新位置

jquery - simplebar 滚动删除溢出隐藏选项

html - Canvas LineTo 中的多色

javascript - 著名的 : can't draw image to CanvasSurface with own size property

javascript - nodejs 中模块 require 的奇怪行为

javascript - Bootstrap 自动折叠

html - 是什么打破了这个表格布局?

javascript - Fabric.js 中的灰度很慢