Skip to content

Commit 81f68d7

Browse files
committed
Section 22: Generating Random Shapes
1 parent 9f803f9 commit 81f68d7

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

22-Javascript-with-the-Canvas-API/01-Maze/index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
// Matter.js library
22
const { Engine, Render, Runner, World, Bodies, MouseConstraint, Mouse } = Matter
33

4+
const width = 800
5+
const height = 600
6+
47
const engine = Engine.create()
58
const { world } = engine
69
const render = Render.create({
710
element: document.body,
811
engine: engine,
912
options: {
10-
width: 800,
11-
height: 600,
13+
// width: width,
14+
// height: height,
15+
wireframes: false,
16+
width,
17+
height,
1218
},
1319
})
1420

@@ -32,4 +38,19 @@ const walls = [
3238

3339
World.add(world, walls)
3440

35-
World.add(world, Bodies.rectangle(200, 200, 50, 50))
41+
// Random Shapes
42+
43+
for (let i = 0; i < 20; i++) {
44+
if (Math.random() < 0.5) {
45+
World.add(
46+
world,
47+
Bodies.rectangle(Math.random() * width, Math.random() * height, 50, 50)
48+
)
49+
} else {
50+
World.add(world, Bodies.circle(Math.random() * width, Math.random() * height, 35, {
51+
render: {
52+
fillStyle: 'green'
53+
}
54+
}))
55+
}
56+
}

22-Javascript-with-the-Canvas-API/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ World.add(world, shape)
2727
## 4. 06 Drawing Borders
2828

2929
## 5. 07 Clicking and Dragging
30+
31+
## 6. 08 Generating Random Shapes
32+
33+

0 commit comments

Comments
 (0)