【问题标题】:Matter.js Mouse Controls SetupMatter.js 鼠标控制设置
【发布时间】:2022-10-15 08:26:04
【问题描述】:

我正在尝试将鼠标控件添加到两个框的启动器 Matter.js 示例中。我似乎错过了一些东西,因为它不起作用。我只想能够用鼠标移动身体。

我正在尝试将鼠标控件添加到两个框的启动器 Matter.js 示例中。我似乎错过了一些东西,因为它不起作用。我只想能够用鼠标移动身体。

'''

<canvas id="canvasM" data-pixel-ratio="2" style="position:relative; z-index:0;"></canvas>
<script>
      // module aliases
  var Engine = Matter.Engine,
      Render = Matter.Render,
      Runner = Matter.Runner,
      Bodies = Matter.Bodies,
      Composite = Matter.Composite;
      World = Matter.World;
  
  var mouse;
  
  // create an engine
  var engine = Engine.create();
    world = engine.world;
  
  var w = window.innerWidth;
  var h = window.innerHeight;
  
  // create two boxes and a ground
  var boxA = Bodies.rectangle(.5*w+30, .7*h, 80, 80);
  var boxB = Bodies.rectangle(.5*w+60, 50, 80, 80);
  var ground = Bodies.rectangle(.5*w-1, .888*h+.05*h-30+1.5, w, .1*h, { isStatic: true });

  // add all of the bodies to the world
  Composite.add(engine.world, 
                [boxA, boxB, ground]);
  

  // create runner
  var runner = Runner.create();

  // run the engine
  Runner.run(runner, engine);
  
  var canvas = document.getElementById('canvasM');
  context = canvas.getContext('2d');
  canvas.width = window.innerWidth-130;
  canvas.height = 0.888*window.innerHeight;


  
  (function render() {
      var bodies = Composite.allBodies(engine.world);

      window.requestAnimationFrame(render);

      context.beginPath();

      for (var i = 0; i < bodies.length; i += 1) {
          var vertices = bodies[i].vertices;

          context.moveTo(vertices[0].x, vertices[0].y);

          for (var j = 1; j < vertices.length; j += 1) {
              context.lineTo(vertices[j].x, vertices[j].y);
          }

          context.lineTo(vertices[0].x, vertices[0].y);
      }

      context.lineWidth = 3;
      context.fill = '#fff';
      context.strokeStyle = '#000';
      context.stroke();
    

  var mouseC = Matter.MouseConstraint;
  mouseC.pixelRatio = 2;
  var canvmouse = Matter.Mouse.create(document.getElementById('canvasM'));
  mouseC = mouseC.create(engine,{
        mouse: canvmouse});

  Composite.add(world, mouseC);

  render.mouse = mouse;
  
  })();
  
  
  
</script>

'''

【问题讨论】:

  • 在渲染循环中每秒添加 60 次鼠标约束是没有意义的。如Using Matter.js to render to the DOM or React 所示,预先执行一次。
  • 我这样做是因为没有其他工作,尝试从您发布的示例中复制鼠标仍然没有
  • 是的,您在此处的设置存在多个基本问题。例如,当您还使用 MJS 的内部运行器时,调用 requestAnimationFrame 是不常见的,它为您做完全相同的事情。来自the docs:“如果您使用自己的游戏循环,则不需要Matter.Runner 模块。”目前尚不清楚您是要使用自己的循环还是 MJS。你要哪个?我建议使用链接的示例作为构建应用程序的框架,而不是相反。它有效,因此您可以对其进行调整以适合您的用例。
  • 发现错误与我的变量命名有关。 (你的回复确实帮助我集中注意力)现在坐标系似乎关闭了,我已经考虑了像素比,但在那之后,我明白了。
  • 哦,抱歉——您使用的是画布,而不是 DOM,但概念几乎相同。 codepen.io/ggorlen/full/LOwrxX 是一个带有画布、鼠标和正确坐标的示例。我似乎在 SO 上找不到我的任何画布示例,所以如果我有时间,我会添加一个答案。

标签: javascript html css matter.js


【解决方案1】:

正如 cmets 中提到的,创建一个 Matter 运行器和使用你自己的更新循环是相互排斥的。如果您确实希望使用 MJS 运行器,请连接到更新事件并根据需要重新绘制您的 UI。如果您更喜欢使用自己的更新循环,请跳过渲染器并在循环中调用 Matter.update()

这是使用您自己的循环的示例:

const w = window.innerWidth;
const h = window.innerHeight;
const engine = Matter.Engine.create();

const boxA = Matter.Bodies.rectangle(
  0.5 * w + 30, // x
  0.7 * h, // y
  80, // w
  80, // h
);
const boxB = Matter.Bodies.rectangle(
  0.5 * w + 60,
  50,
  80,
  80
);
const ground = Matter.Bodies.rectangle(
  0.5 * w - 1,
  0.888 * h + 0.05 * h - 30 + 1.5,
  w,
  0.1 * h,
  {
    isStatic: true,
  }
);

const canvas = document.querySelector("canvas");
context = canvas.getContext("2d");
canvas.width = w - 130;
canvas.height = 0.888 * h;

const mouseConstraint = Matter.MouseConstraint.create(
  engine,
  {element: canvas}
);
Matter.Composite.add(engine.world, [
  boxA,
  boxB,
  ground,
  mouseConstraint,
]);
const bodies = Matter.Composite.allBodies(engine.world);

(function render() {
  window.requestAnimationFrame(render);

  context.clearRect(0, 0, canvas.width, canvas.height);
  context.beginPath();

  for (const {vertices} of bodies) {
    context.moveTo(vertices[0].x, vertices[0].y);
    vertices.forEach(({x, y}) => context.lineTo(x, y));
    context.lineTo(vertices[0].x, vertices[0].y);
  }

  context.lineWidth = 3;
  context.fill = "#fff";
  context.strokeStyle = "#000";
  context.stroke();

  Matter.Engine.update(engine);
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.18.0/matter.min.js"></script>
<canvas></canvas>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多