Skip to content

Commit 6d12b1c

Browse files
authored
Tidy the demo examples (#515)
* Tidy the demo examples * Formatting fixes
1 parent 33c7dbb commit 6d12b1c

File tree

4 files changed

+119
-159
lines changed

4 files changed

+119
-159
lines changed

demo/app.jsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import React, { useCallback, useMemo, useState } from "react";
22
import { createRoot } from "react-dom/client";
33

4-
import * as box from "./sketches/box";
5-
import * as plane from "./sketches/plane";
6-
import * as torus from "./sketches/torus";
74
import { ReactP5Wrapper } from "../src/main.tsx";
85
import "./demo.css";
6+
import { sketch as box } from "./sketches/box";
7+
import { sketch as plane } from "./sketches/plane";
8+
import { sketch as torus } from "./sketches/torus";
99

1010
function App() {
11-
const sketches = useMemo(
12-
() => [box.sketch, torus.sketch, plane.sketch],
13-
[box, torus, plane]
14-
);
11+
const sketches = useMemo(() => [box, torus, plane], [box, torus, plane]);
12+
1513
const [state, setState] = useState({
1614
rotation: 160,
17-
sketch: box.sketch,
15+
sketch: box,
1816
unmount: false
1917
});
18+
2019
const onChangeSketch = useCallback(() => {
2120
setState(state => {
2221
const currentSketchIndex = sketches.findIndex(sketch => {
@@ -32,9 +31,11 @@ function App() {
3231
return { ...state, sketch };
3332
});
3433
}, [sketches]);
34+
3535
const onMountStateChange = useCallback(() => {
3636
setState(state => ({ ...state, unmount: !state.unmount }));
3737
}, []);
38+
3839
const onRotationChange = useCallback(
3940
event => {
4041
setState(state => ({

demo/sketches/capture.jsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import "p5.capture";
2+
3+
export function sketch(p5) {
4+
p5.setup = () => {
5+
p5.createCanvas(300, 300);
6+
p5.capture.initialize(p5);
7+
};
8+
9+
p5.draw = () => {
10+
if (p5.frameCount === 1) {
11+
const capture = p5.capture.getInstance();
12+
capture.start({
13+
format: "gif",
14+
duration: 100
15+
});
16+
}
17+
};
18+
19+
p5.keyPressed = () => {
20+
if (key === "c") {
21+
const capture = p5.capture.getInstance();
22+
23+
if (capture.state === "idle") {
24+
capture.start();
25+
} else {
26+
capture.stop();
27+
}
28+
}
29+
};
30+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"jiti": "^2.4.2",
111111
"js": "^0.1.0",
112112
"jsdom": "^26.1.0",
113+
"p5.capture": "^1.6.0",
113114
"prettier": "^3.6.2",
114115
"react": "19.1.0",
115116
"react-dom": "19.1.0",

0 commit comments

Comments
 (0)