-
Notifications
You must be signed in to change notification settings - Fork 0
/
matter.js
213 lines (184 loc) · 4.84 KB
/
matter.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var canvas = document.querySelector("#wrapper-canvas");
var dimensions = {
width: window.innerWidth,
height: window.innerHeight,
};
Matter.use("matter-attractors");
Matter.use("matter-wrap");
function runMatter() {
// module aliases
var Engine = Matter.Engine,
Events = Matter.Events,
Runner = Matter.Runner,
Render = Matter.Render,
World = Matter.World,
Body = Matter.Body,
Mouse = Matter.Mouse,
Common = Matter.Common,
Composite = Matter.Composite,
Composites = Matter.Composites,
Bodies = Matter.Bodies;
// create engine
var engine = Engine.create();
engine.world.gravity.y = 0;
engine.world.gravity.x = 0;
engine.world.gravity.scale = 0.1;
// create renderer
var render = Render.create({
element: canvas,
engine: engine,
options: {
showVelocity: false,
width: dimensions.width,
height: dimensions.height,
wireframes: false,
background: "transparent",
},
});
// create runner
var runner = Runner.create();
// Runner.run(runner, engine);
// Render.run(render);
// create demo scene
var world = engine.world;
world.gravity.scale = 0;
// create a body with an attractor
var attractiveBody = Bodies.circle(
render.options.width / 2,
render.options.height / 2,
Math.max(dimensions.width / 25, dimensions.height / 25) / 2,
{
render: {
fillStyle: `#000`,
strokeStyle: `#000`,
lineWidth: 0,
},
isStatic: true,
plugin: {
attractors: [
function (bodyA, bodyB) {
return {
x: (bodyA.position.x - bodyB.position.x) * 1e-6,
y: (bodyA.position.y - bodyB.position.y) * 1e-6,
};
},
],
},
}
);
World.add(world, attractiveBody);
// add some bodies that to be attracted
for (var i = 0; i < 60; i += 1) {
let x = Common.random(0, render.options.width);
let y = Common.random(0, render.options.height);
let s =
Common.random() > 0.6 ? Common.random(10, 80) : Common.random(4, 60);
let poligonNumber = Common.random(3, 6);
var body = Bodies.polygon(
x,
y,
poligonNumber,
s,
{
mass: s / 20,
friction: 0,
frictionAir: 0.02,
angle: Math.round(Math.random() * 360),
render: {
fillStyle: "#222222",
strokeStyle: `#000000`,
lineWidth: 2,
},
}
);
World.add(world, body);
let r = Common.random(0, 1);
var circle = Bodies.circle(x, y, Common.random(2, 8), {
mass: 0.1,
friction: 0,
frictionAir: 0.01,
render: {
fillStyle: r > 0.3 ? `#27292d` : `#444444`,
strokeStyle: `#000000`,
lineWidth: 2,
},
});
World.add(world, circle);
var circle = Bodies.circle(x, y, Common.random(2, 20), {
mass: 6,
friction: 0,
frictionAir: 0,
render: {
fillStyle: r > 0.3 ? `#334443` : `#222222`,
strokeStyle: `#111111`,
lineWidth: 4,
},
});
World.add(world, circle);
var circle = Bodies.circle(x, y, Common.random(2, 30), {
mass: 0.2,
friction: 0.6,
frictionAir: 0.8,
render: {
fillStyle: `#191919`,
strokeStyle: `#111111`,
lineWidth: 3,
},
});
World.add(world, circle);
}
// add mouse control
var mouse = Mouse.create(render.canvas);
Events.on(engine, "afterUpdate", function () {
if (!mouse.position.x) return;
// smoothly move the attractor body towards the mouse
Body.translate(attractiveBody, {
x: (mouse.position.x - attractiveBody.position.x) * 0.12,
y: (mouse.position.y - attractiveBody.position.y) * 0.12,
});
});
// return a context for MatterDemo to control
let data = {
engine: engine,
runner: runner,
render: render,
canvas: render.canvas,
stop: function () {
Matter.Render.stop(render);
Matter.Runner.stop(runner);
},
play: function () {
Matter.Runner.run(runner, engine);
Matter.Render.run(render);
},
};
Matter.Runner.run(runner, engine);
Matter.Render.run(render);
return data;
}
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this,
args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
function setWindowSize() {
let dimensions = {};
dimensions.width = $(window).width();
dimensions.height = $(window).height();
m.render.canvas.width = $(window).width();
m.render.canvas.height = $(window).height();
return dimensions;
}
let m = runMatter();
setWindowSize();
$(window).resize(debounce(setWindowSize, 250));