gpt4 book ai didi

javascript - 空白屏幕 - Matter.js

转载 作者:行者123 更新时间:2023-12-04 10:34:45 24 4
gpt4 key购买 nike

问题.js -

这是 one of the demo来自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>

最佳答案

修复:

(1) 更改您的 world22元素来自 canvasdiv ,或任何其他合适的元素。显然,matter.js 会在容器中创建它自己的 Canvas ,因此不能对 Canvas 内的 Canvas 做任何事情。

(2) 缺少||在您的情况下,此处导致语法错误,介于 rock.position.x > 190 rock.position.y < 430 之间.不知道在粘贴到 SO 时这是否成为一个错字。

(3) 使用属性element在创建渲染时指定容器元素时。不要使用 canvas: canvas .使用 element: canvas .关于这里Render.create({ ... });
这是修改后的源代码。我想你可以放心地忽略我是如何包含 matt.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 ....

起初我没有 catch canvas问题,但后来我使用浏览器开发工具并检查了 world22 元素。另一个 Canvas 是嵌套的,所以我就是这样知道的。

关于javascript - 空白屏幕 - Matter.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60236612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com