Skip to content

Commit 4686521

Browse files
committed
Added 1-polygon example
1 parent bccd648 commit 4686521

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

1-polygon/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<script language="javascript" type="text/javascript" src="../libraries/p5.js"></script>
6+
<script language="javascript" type="text/javascript" src="../libraries/matter.js"></script>
7+
<script language="javascript" type="text/javascript" src="sketch.js"></script>
8+
<script language="javascript" type="text/javascript" src="../classes/BlockCore.js"></script>
9+
<script language="javascript" type="text/javascript" src="../classes/Block.js"></script>
10+
<script language="javascript" type="text/javascript" src="../classes/Polygon.js"></script>
11+
<script language="javascript" type="text/javascript" src="../classes/PolygonFromPoints.js"></script>
12+
<script language="javascript" type="text/javascript" src="../classes/Mouse.js"></script>
13+
<style> body {padding: 0; margin: 0;} </style>
14+
</head>
15+
<body>
16+
</body>
17+
</html>

1-polygon/sketch.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
let pentagon;
2+
let polygon;
3+
let ground;
4+
let mouse;
5+
6+
function setup() {
7+
const canvas = createCanvas(800, 600);
8+
9+
// create an engine
10+
let engine = Matter.Engine.create();
11+
let world = engine.world;
12+
13+
// create polygons and a ground
14+
ground = new Block(
15+
world,
16+
{ x: 400, y: 500, w: 810, h: 15, color: 'grey' },
17+
{ isStatic: true, angle: PI/36 }
18+
);
19+
pentagon = new Polygon(world, { x: 400, y: 100, s: 5, r: 50, color: 'white' });
20+
21+
const points = [
22+
{ x: 0, y: 0 },
23+
{ x: 100, y: 10 },
24+
{ x: 150, y: 100 },
25+
{ x: 150, y: 150 },
26+
{ x: 0, y: 50 + random(0, 400) }
27+
];
28+
polygon = new PolygonFromPoints(world,
29+
{ x: 300, y: 0, points: points, color: 'white' }
30+
);
31+
32+
// add a mouse to manipulate Matter objects
33+
mouse = new Mouse(engine, canvas, { stroke: 'magenta', strokeWeight: 2 });
34+
35+
// run the engine
36+
Matter.Runner.run(engine);
37+
}
38+
39+
function draw() {
40+
background('black');
41+
ground.draw();
42+
pentagon.draw();
43+
polygon.draw();
44+
mouse.draw();
45+
}

README-detailed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ p5.js × matter.js
44
## Examples
55
* [1-basics](https://b-g.github.io/p5-matter-examples/1-basics/)
66
* [1-mouse](https://b-g.github.io/p5-matter-examples/1-mouse/)
7+
* [1-polygon](https://b-g.github.io/p5-matter-examples/1-polygon/)
78
* [1-sprites](https://b-g.github.io/p5-matter-examples/1-sprites/)
89
* [1-stacks](https://b-g.github.io/p5-matter-examples/1-stacks/)
910
* [1-wrap-coordinates](https://b-g.github.io/p5-matter-examples/1-wrap-coordinates/)

0 commit comments

Comments
 (0)