-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
125 lines (110 loc) · 2.79 KB
/
index.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Slingshot</title>
<style>
body {
width: 100vw;
height: 100vh;
margin: 0;
display: flex;
flex-direction: column-reverse;
align-items: center;
justify-content: center;
background-color: #0063a5;
}
button {
margin-top: 10px;
}
</style>
</head>
<body>
<button onclick="location.reload()">REPLAY</button>
<script src="./matter.min.js"></script>
<script>
const engine = Matter.Engine.create();
const render = Matter.Render.create({
element: document.body,
engine: engine,
options: {
width: 1280,
height: 720,
wireframes: false,
background: "#0099ff",
},
});
const ground = Matter.Bodies.rectangle(1020, 500, 180, 8, {
isStatic: true,
});
const ground2 = Matter.Bodies.rectangle(820, 300, 70, 8, {
isStatic: true,
});
let ball = Matter.Bodies.circle(300, 550, 20);
const sling = Matter.Constraint.create({
pointA: { x: 300, y: 550 },
bodyB: ball,
stiffness: 0.05,
});
const mouse = Matter.Mouse.create(render.canvas);
const mouseConstraint = Matter.MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
render: { visible: false },
},
});
render.mouse = mouse;
let firing = false;
Matter.Events.on(mouseConstraint, "enddrag", function (e) {
if (e.body === ball) firing = true;
});
Matter.Events.on(engine, "afterUpdate", function () {
if (
firing &&
Math.abs(ball.position.x - 300) < 20 &&
Math.abs(ball.position.y - 550) < 20
) {
ball = Matter.Bodies.circle(300, 550, 20);
Matter.World.add(engine.world, ball);
sling.bodyB = ball;
firing = false;
}
});
const stack = Matter.Composites.stack(
960,
300,
3,
4,
0,
0,
function (x, y) {
return Matter.Bodies.polygon(x, y, 4, 30);
}
);
const stack2 = Matter.Composites.stack(
800,
120,
1,
3,
0,
0,
function (x, y) {
return Matter.Bodies.polygon(x, y, 4, 30);
}
);
Matter.World.add(engine.world, [
stack,
stack2,
ground,
ground2,
ball,
sling,
mouseConstraint,
]);
Matter.Engine.run(engine);
Matter.Render.run(render);
</script>
</body>
</html>