Skip to content

Commit 3a03961

Browse files
committed
added experimental react-js integration sandbox link
1 parent 83fcd8c commit 3a03961

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Verly.js
3+
* https://github.com/anuraghazra/Verly.js
4+
*
5+
* @author <http://anuraghazra.github.io>
6+
*
7+
* this integration is still in beta, i'm experimenting with it
8+
*
9+
* give Verly.js a Star on github if you liked it
10+
*/
11+
import React, { useState, useEffect, useRef } from 'react';
12+
13+
// rect
14+
export function Rect({ verly, x, y, width, height }) {
15+
verly.createBox(x, y, width, height);
16+
return null;
17+
}
18+
19+
// rope
20+
export function Rope({ verly, x, y, segs, gap, pin }) {
21+
verly.createRope(x, y, segs, gap, pin);
22+
return null;
23+
}
24+
25+
// cloth
26+
export function Cloth({ verly, x, y, width, height, segs, pin }) {
27+
verly.createCloth(x, y, width, height, segs, pin);
28+
return null;
29+
}
30+
31+
// hexagon
32+
export function Hexagon({ verly, x, y, segs, radius, stride1, stride2 }) {
33+
verly.createHexagon(x, y, segs, radius, stride1, stride2);
34+
return null;
35+
}
36+
37+
//ragdoll
38+
export function Ragdoll({ verly, x, y }) {
39+
verly.createRagdoll(x, y);
40+
return null;
41+
}
42+
43+
// VerlyEntity
44+
export function VerlyEntity({ verly, children }) {
45+
let entity = new Entity(verly.iterations, verly);
46+
let childrenWithProps = React.Children.map(children, child =>
47+
React.cloneElement(child, { entity: entity })
48+
);
49+
verly.addEntity(entity);
50+
return childrenWithProps;
51+
}
52+
53+
// Point
54+
export function Point({ entity, x, y }) {
55+
entity.addPoint(x, y);
56+
return null;
57+
}
58+
59+
// Stick
60+
export function Stick({ entity, indices }) {
61+
entity.addStick(indices[0], indices[1]);
62+
return null;
63+
}
64+
65+
// VerlyStage
66+
export function VerlyStage({ width, height, iterations, children }) {
67+
let canvasRef = useRef();
68+
let [verly, setVerly] = useState(null);
69+
let [childrenWithProps, setChilds] = useState(null);
70+
71+
useEffect(() => {
72+
let canvas = canvasRef.current;
73+
canvas.width = width;
74+
canvas.height = height;
75+
let ctx = canvas.getContext('2d');
76+
let verly = new Verly(iterations, canvas, ctx);
77+
// verly.createBox(100, 100, 100, 100);
78+
setVerly(verly);
79+
let childrenWithProps = React.Children.map(children, child =>
80+
React.cloneElement(child, { verly: verly })
81+
);
82+
setChilds(childrenWithProps);
83+
84+
function animate() {
85+
ctx.clearRect(0, 0, canvas.width, canvas.height);
86+
87+
verly.update();
88+
verly.render();
89+
verly.interact();
90+
91+
requestAnimationFrame(animate);
92+
}
93+
animate();
94+
}, []);
95+
96+
return <canvas ref={canvasRef}>{childrenWithProps}</canvas>;
97+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Verly.js
3+
* https://github.com/anuraghazra/Verly.js
4+
*
5+
* @author <http://anuraghazra.github.io>
6+
*
7+
* this integration is still in beta, i'm experimenting with it
8+
*
9+
* give Verly.js a Star on github if you liked it
10+
*/
11+
import React from 'react';
12+
import ReactDOM from 'react-dom';
13+
14+
import './styles.css';
15+
16+
import {
17+
VerlyStage,
18+
Rect,
19+
VerlyEntity,
20+
Stick,
21+
Point,
22+
Rope,
23+
Cloth,
24+
Hexagon,
25+
Ragdoll
26+
} from './VerlyComponent';
27+
28+
const MyCustomEntity = ({ verly }) => {
29+
return (
30+
<VerlyEntity verly={verly}>
31+
<Point x={100} y={150} />
32+
<Point x={200} y={100} />
33+
<Point x={200} y={220} />
34+
<Point x={100} y={200} />
35+
<Stick indices={[0, 1]} />
36+
<Stick indices={[1, 2]} />
37+
<Stick indices={[2, 3]} />
38+
<Stick indices={[3, 0]} />
39+
<Stick indices={[3, 1]} />
40+
<Stick indices={[2, 0]} />
41+
</VerlyEntity>
42+
);
43+
};
44+
45+
function App() {
46+
return (
47+
<div>
48+
<VerlyStage width={500} height={500} iterations={20}>
49+
<Rect x={30} y={100} width={100} height={100} />
50+
<Rope x={30} y={100} segs={10} gap={10} pin={0} />
51+
<Cloth x={200} y={100} width={200} height={200} segs={15} pin={2} />
52+
<Hexagon
53+
x={300}
54+
y={100}
55+
segs={12}
56+
stride1={5}
57+
stride2={8}
58+
radius={50}
59+
/>
60+
<Ragdoll x={300} y={100} />
61+
<MyCustomEntity />
62+
</VerlyStage>
63+
</div>
64+
);
65+
}
66+
67+
const rootElement = document.getElementById('root');
68+
ReactDOM.render(<App />, rootElement);

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Easy to integrate verlet physics engine.
88
**Read my article on medium about [Making a Verlet Physics Engine in JavaScript](https://medium.com/better-programming/making-a-verlet-physics-engine-in-javascript-1dff066d7bc5)**
99

1010

11-
1211
## cool things made with Verly.js
1312
- [VerlyRangeSlider](https://anuraghazra.github.io/VerlyRangeSlider/)
1413
- [Ship](https://anuraghazra.github.io/Verly.js/examples/ship/)
@@ -21,6 +20,9 @@ Easy to integrate verlet physics engine.
2120
### want more info? Check out the API **[documentation](https://anuraghazra.github.io/Verly.js/docs)**
2221

2322

23+
### *Experimental Reactjs Integration - check out the [sandbox](https://codesandbox.io/s/verlyjs-react-w5kfr)*
24+
25+
2426
## Installation
2527

2628
```bash

0 commit comments

Comments
 (0)