-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfall.html
More file actions
79 lines (67 loc) · 1.82 KB
/
fall.html
File metadata and controls
79 lines (67 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Falling Balls Example</title>
<script src="https://cdn.jsdelivr.net/npm/matter-js@0.19.0/build/matter.min.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
background: #111;
overflow: hidden;
}
</style>
</head>
<body>
<script>
const { Engine, Render, Runner, World, Bodies } = Matter;
// world + physics
const engine = Engine.create();
const world = engine.world;
const width = window.innerWidth;
const height = window.innerHeight;
// renderer
const render = Render.create({
element: document.body,
engine,
options: {
width,
height,
background: "#00FF00",
wireframes: false
}
});
Render.run(render);
const runner = Runner.create();
Runner.run(runner, engine);
// static walls
const ground = Bodies.rectangle(width / 2, height + 50, width, 100, { isStatic: true });
const left = Bodies.rectangle(-50, height / 2, 100, height, { isStatic: true });
const right = Bodies.rectangle(width + 50, height / 2, 100, height, { isStatic: true });
World.add(world, [ground, left, right]);
// generate balls
function spawnBall() {
const x = Math.random() * width;
const r = 10 + Math.random() * 30;
const color = `hsl(${Math.random() * 360}, 80%, 60%)`;
const ball = Bodies.circle(x, -50, r, {
restitution: 0.6, // 推薦 0.3~0.6
friction: 0,
render: {
fillStyle: color
}
});
World.add(world, ball);
}
// drop a ball every 70ms
setInterval(spawnBall, 70);
// handle window resize
window.addEventListener("resize", () => {
location.reload(); // 簡單粗暴,保持一致
});
</script>
</body>
</html>