Skip to content

Commit cf00354

Browse files
authored
Add p5.record example (#520)
1 parent d5b5feb commit cf00354

File tree

4 files changed

+653
-585
lines changed

4 files changed

+653
-585
lines changed

demo/app.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import { ReactP5Wrapper } from "../src/main.tsx";
55
import "./demo.css";
66
import { sketch as box } from "./sketches/box";
77
import { sketch as plane } from "./sketches/plane";
8+
import { sketch as record } from "./sketches/record";
89
import { sketch as torus } from "./sketches/torus";
910

1011
function App() {
11-
const sketches = useMemo(() => [box, torus, plane], [box, torus, plane]);
12+
const sketches = useMemo(
13+
() => [box, torus, plane, record],
14+
[box, torus, plane, record]
15+
);
1216

1317
const [state, setState] = useState({
1418
rotation: 160,
15-
sketch: box,
19+
sketch: record,
1620
unmount: false
1721
});
1822

@@ -58,6 +62,14 @@ function App() {
5862
return (
5963
<>
6064
<ReactP5Wrapper sketch={state.sketch} rotation={state.rotation} />
65+
{state.sketch === record && (
66+
<div style={{ marginTop: "1rem", marginBottom: "1rem" }}>
67+
<button id="start-recording">Start Recording</button>
68+
<button id="stop-recording">Stop Recording</button>
69+
<button id="pause-recording">Pause Recording</button>
70+
<button id="resume-recording">Resume Recording</button>
71+
</div>
72+
)}
6173
<input
6274
type="range"
6375
defaultValue={state.rotation}

demo/sketches/record.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as record from "p5.record.js";
2+
3+
export function sketch(p5) {
4+
let recording;
5+
6+
p5.setup = () => {
7+
const canvas = p5.createCanvas(400, 400);
8+
p5.background(200);
9+
10+
recording = new record.Recorder({
11+
frameRate: 60,
12+
source: canvas.elt
13+
});
14+
15+
const startBtn = p5.select("#start-recording");
16+
startBtn.mouseClicked(() => recording.start());
17+
18+
const stopBtn = p5.select("#stop-recording");
19+
stopBtn.mouseClicked(() => recording.stop());
20+
21+
const pauseBtn = p5.select("#pause-recording");
22+
pauseBtn.mouseClicked(() => recording.pause());
23+
24+
const resumeBtn = p5.select("#resume-recording");
25+
resumeBtn.mouseClicked(() => recording.resume());
26+
};
27+
28+
p5.draw = () => {
29+
p5.line(p5.mouseX, p5.mouseY, p5.pmouseX, p5.pmouseY);
30+
};
31+
32+
p5.keyPressed = () => {
33+
if (p5.key === "a") {
34+
recording.start();
35+
} else if (p5.key === "s") {
36+
recording.stop();
37+
} else if (p5.key === "d") {
38+
recording.pause();
39+
} else if (p5.key === "f") {
40+
recording.resume();
41+
}
42+
};
43+
}

package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,36 @@
8989
"devDependencies": {
9090
"@babel/eslint-plugin": "^7.27.1",
9191
"@eslint/compat": "^1.3.1",
92-
"@eslint/js": "^9.30.1",
92+
"@eslint/js": "^9.32.0",
9393
"@testing-library/jest-dom": "6.6.3",
9494
"@testing-library/react": "^16.3.0",
9595
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
9696
"@types/jest": "^30.0.0",
97-
"@types/node": "^24.0.10",
97+
"@types/node": "^24.1.0",
9898
"@types/p5": "^1.7.6",
99-
"@types/react": "^19.1.8",
100-
"@types/react-dom": "^19.1.6",
101-
"@typescript-eslint/eslint-plugin": "^8.35.1",
102-
"@vitejs/plugin-react": "^4.6.0",
99+
"@types/react": "^19.1.9",
100+
"@types/react-dom": "^19.1.7",
101+
"@typescript-eslint/eslint-plugin": "^8.38.0",
102+
"@vitejs/plugin-react": "^4.7.0",
103103
"@vitest/coverage-v8": "^3.2.4",
104104
"babel-plugin-react-compiler": "19.1.0-rc.2",
105-
"eslint": "^9.30.1",
105+
"eslint": "^9.32.0",
106106
"eslint-plugin-react": "^7.37.5",
107107
"eslint-plugin-react-compiler": "19.1.0-rc.2",
108108
"eslint-plugin-react-hooks": "^5.2.0",
109109
"gh-pages": "^6.3.0",
110-
"jiti": "^2.4.2",
110+
"jiti": "^2.5.1",
111111
"js": "^0.1.0",
112112
"jsdom": "^26.1.0",
113113
"p5.capture": "^1.6.0",
114+
"p5.record.js": "^0.2.0",
114115
"prettier": "^3.6.2",
115116
"react": "19.1.0",
116117
"react-dom": "19.1.0",
117118
"rimraf": "^6.0.1",
118119
"typescript": "^5.8.3",
119-
"typescript-eslint": "^8.35.1",
120-
"vite": "^7.0.2",
120+
"typescript-eslint": "^8.38.0",
121+
"vite": "^7.0.6",
121122
"vite-plugin-dts": "^4.5.4",
122123
"vitest": "^3.2.4",
123124
"vitest-canvas-mock": "^0.3.3"

0 commit comments

Comments
 (0)