-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstar.html
More file actions
210 lines (165 loc) · 6.24 KB
/
Copy pathstar.html
File metadata and controls
210 lines (165 loc) · 6.24 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="matter.js"></script>
<script src="matter-attractors.js"></script>
</head>
<body onmousedown="gravity()">
<script>
Matter.use(
'matter-attractors' // PLUGIN_NAME
);
var Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
Events = Matter.Events,
Composites = Matter.Composites,
Common = Matter.Common,
MouseConstraint = Matter.MouseConstraint,
Mouse = Matter.Mouse,
Composite = Matter.Composite,
Vector = Matter.Vector,
Bounds = Matter.Bounds,
Bodies = Matter.Bodies,
World = Matter.World;
// create engine
var engine = Engine.create(),
worldi = engine.world;
var render = Render.create({
element: document.body,
engine: engine,
options: {
wireframes: false,
background: "rgb(0,0,0)"
}});
render.canvas.width = window.innerWidth;
render.canvas.height = window.innerHeight;
// create runner
var runner = Runner.create();
Runner.run(runner, engine);
Render.run(render);
var world = engine.world;
world.gravity.scale = 0;
// create an engine
// add mouse control
var mouse = Mouse.create(render.canvas),
mouseConstraint = MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
}
}
});
Composite.add(world, mouseConstraint);
// keep the mouse in sync with rendering
render.mouse = mouse;
document.onmousemove = function(event) {
pointerX = event.pageX;
pointerY = event.pageY;
}
// add bodies
var boxC = Bodies.circle(window.innerWidth/2, window.innerHeight/2, 1, { isStatic: true, plugin: {
attractors: [
function(bodyA, bodyB) {
return {
x: (bodyA.position.x - bodyB.position.x) * 1e-7,
y: (bodyA.position.y - bodyB.position.y) * 1e-7,
};
}
]
}});
World.add(world, boxC);
var ground = Bodies.rectangle(window.innerWidth/2, window.innerHeight, window.innerWidth, 60, { isStatic: true });
for (var i = 0; i < 150; i += 1) {
var body = Bodies.polygon(
Common.random(0, render.canvas.width),
Common.random(0, render.options.height),
Common.random(1, 5),
Common.random() > 0.9 ? Common.random(15, 25) : Common.random(5, 10)
);
// World.add(world, body);
}
// add all of the bodies to the world
function gravity() {
var boxA = Bodies.rectangle(pointerX , pointerY , 30, Common.random(5, 20), { render: {fillStyle: "rgb("+Common.random(100, 200)+","+Common.random(50, 200)+","+Common.random(150, 250)+")" }});
World.add(world, boxA)
}
// get the centre of the viewport
var viewportCentre = {
x: render.options.width * 0.5,
y: render.options.height * 0.5
};
// create limits for the viewport
var extents = {
min: { x: -300, y: -300 },
max: { x: 1100, y: 900 }
};
// keep track of current bounds scale (view zoom)
var boundsScaleTarget = 1,
boundsScale = {
x: 1,
y: 1
};
// use a render event to control our view
Events.on(render, 'beforeRender', function() {
var worldi = engine.world,
mouse = mouseConstraint.mouse,
translate;
// mouse wheel controls zoom
var scaleFactor = mouse.wheelDelta * -0.1;
if (scaleFactor !== 0) {
if ((scaleFactor < 0 && boundsScale.x >= 0.6) || (scaleFactor > 0 && boundsScale.x <= 1.4)) {
boundsScaleTarget += scaleFactor;
}
}
// if scale has changed
if (Math.abs(boundsScale.x - boundsScaleTarget) > 0.01) {
// smoothly tween scale factor
scaleFactor = (boundsScaleTarget - boundsScale.x) * 0.2;
boundsScale.x += scaleFactor;
boundsScale.y += scaleFactor;
// scale the render bounds
render.bounds.max.x = render.bounds.min.x + render.options.width * boundsScale.x;
render.bounds.max.y = render.bounds.min.y + render.options.height * boundsScale.y;
// translate so zoom is from centre of view
translate = {
x: render.options.width * scaleFactor * -0.5,
y: render.options.height * scaleFactor * -0.5
};
Bounds.translate(render.bounds, translate);
// update mouse
Mouse.setScale(mouse, boundsScale);
Mouse.setOffset(mouse, render.bounds.min);
}
// get vector from mouse relative to centre of viewport
var deltaCentre = Vector.sub(mouse.absolute, viewportCentre),
centreDist = Vector.magnitude(deltaCentre);
// translate the view if mouse has moved over 50px from the centre of viewport
if (centreDist > 50) {
// create a vector to translate the view, allowing the user to control view speed
var direction = Vector.normalise(deltaCentre),
speed = Math.min(10, Math.pow(centreDist - 50, 2) * 0.0002);
translate = Vector.mult(direction, speed);
// prevent the view moving outside the extents
if (render.bounds.min.x + translate.x < extents.min.x)
translate.x = extents.min.x - render.bounds.min.x;
if (render.bounds.max.x + translate.x > extents.max.x)
translate.x = extents.max.x - render.bounds.max.x;
if (render.bounds.min.y + translate.y < extents.min.y)
translate.y = extents.min.y - render.bounds.min.y;
if (render.bounds.max.y + translate.y > extents.max.y)
translate.y = extents.max.y - render.bounds.max.y;
// move the view
Bounds.translate(render.bounds, translate);
// we must update the mouse too
Mouse.setOffset(mouse, render.bounds.min);
}
});
// context for MatterTools.Demo
</script>
</body>
</html>