matter.js -
我是其中一个演示从matter.js打来的。它返回一个空白屏幕。我已经尝试过几种方法,花了好几个小时在这上面,仍然找不出到底出了什么问题。
我检查了括号、语法和错误,但它仍然返回一个空白屏幕。
我已经检查了matter.js是否在后台正确加载。
有人能在这里指出错误吗?
<script>
window.addEventListener('load', function() {
//Fetch our canvas
var canvas = document.getElementById('world22');
var Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
Composites = Matter.Composites,
Events = Matter.Events,
Constraint = Matter.Constraint,
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,
canvas: canvas,
engine: engine,
options: {
width: 800,
height: 600,
showAngleIndicator: true,
}
});
Render.run(render);
// create runner
var runner = Runner.create();
Runner.run(runner, engine);
// add bodies
var ground = Bodies.rectangle(395, 600, 815, 50, { isStatic: true }),
rockOptions = { density: 0.004 },
rock = Bodies.polygon(170, 450, 8, 20, rockOptions),
anchor = { x: 170, y: 450 },
elastic = Constraint.create({
pointA: anchor,
bodyB: rock,
stiffness: 0.05
});
var pyramid = Composites.pyramid(500, 300, 9, 10, 0, 0, function(x, y) {
return Bodies.rectangle(x, y, 25, 40);
});
var ground2 = Bodies.rectangle(610, 250, 200, 20, { isStatic: true });
var pyramid2 = Composites.pyramid(550, 0, 5, 10, 0, 0, function(x, y) {
return Bodies.rectangle(x, y, 25, 40);
});
World.add(engine.world, [ground, pyramid, ground2, pyramid2, rock, elastic]);
Events.on(engine, 'afterUpdate', function() {
if (mouseConstraint.mouse.button === -1 && (rock.position.x > 190
rock.position.y < 430)) {
rock = Bodies.polygon(170, 450, 7, 20, rockOptions);
World.add(engine.world, rock);
elastic.bodyB = rock;
}
});
// 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 }
});
});
</script>
<canvas id="world22"></canvas>发布于 2020-02-17 09:34:48
修复:
(1)将world22元素从canvas更改为div,或将其更改为任何其他合适的元素。显然,matter.js将在容器中创建自己的画布,因此无法在画布中处理画布。
(2)您的条件中缺少一个||,导致rock.position.x > 190 rock.position.y < 430之间出现语法错误。不知道这是否成为了一个错误粘贴到这样。
(3)在创建呈现时指定容器元素时使用属性element。不要使用canvas: canvas。使用element: canvas。关于这里,Render.create({ ... });
这是修改后的源。我想您可以安全地忽略我如何包含matter.js脚本。我不知道版本是否重要,假设你有最新版本。
<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.14.2/matter.js"></script>
<script>
window.addEventListener('load', function() {
//Fetch our canvas
var canvas = document.getElementById('world22');
var Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
Composites = Matter.Composites,
Events = Matter.Events,
Constraint = Matter.Constraint,
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: canvas,
engine: engine,
options: {
width: 800,
height: 600,
showAngleIndicator: true,
}
});
Render.run(render);
// create runner
var runner = Runner.create();
Runner.run(runner, engine);
// add bodies
var ground = Bodies.rectangle(395, 600, 815, 50, { isStatic: true }),
rockOptions = { density: 0.004 },
rock = Bodies.polygon(170, 450, 8, 20, rockOptions),
anchor = { x: 170, y: 450 },
elastic = Constraint.create({
pointA: anchor,
bodyB: rock,
stiffness: 0.05
});
var pyramid = Composites.pyramid(500, 300, 9, 10, 0, 0, function(x, y) {
return Bodies.rectangle(x, y, 25, 40);
});
var ground2 = Bodies.rectangle(610, 250, 200, 20, { isStatic: true });
var pyramid2 = Composites.pyramid(550, 0, 5, 10, 0, 0, function(x, y) {
return Bodies.rectangle(x, y, 25, 40);
});
World.add(engine.world, [ground, pyramid, ground2, pyramid2, rock, elastic]);
Events.on(engine, 'afterUpdate', function() {
if (mouseConstraint.mouse.button === -1 && (rock.position.x > 190 ||
rock.position.y < 430)) {
rock = Bodies.polygon(170, 450, 7, 20, rockOptions);
World.add(engine.world, rock);
elastic.bodyB = rock;
}
});
// 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 }
});
});
</script>
</head>
<body>
<div id="world22"></div>
</body>
<html>
我的调试基础是参考这里提供的一个最小示例https://github.com/liabru/matter-js/wiki/Getting-started .
起初,我没有理解canvas问题,但后来我使用了浏览器开发工具并检查了world22元素。另一个画布是嵌套的,所以我就知道了。
https://stackoverflow.com/questions/60236612
复制相似问题