Skip to content

Tidy the demo examples #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions demo/app.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import React, { useCallback, useMemo, useState } from "react";
import { createRoot } from "react-dom/client";

import * as box from "./sketches/box";
import * as plane from "./sketches/plane";
import * as torus from "./sketches/torus";
import { ReactP5Wrapper } from "../src/main.tsx";
import "./demo.css";
import { sketch as box } from "./sketches/box";
import { sketch as plane } from "./sketches/plane";
import { sketch as torus } from "./sketches/torus";

function App() {
const sketches = useMemo(
() => [box.sketch, torus.sketch, plane.sketch],
[box, torus, plane]
);
const sketches = useMemo(() => [box, torus, plane], [box, torus, plane]);

const [state, setState] = useState({
rotation: 160,
sketch: box.sketch,
sketch: box,
unmount: false
});

const onChangeSketch = useCallback(() => {
setState(state => {
const currentSketchIndex = sketches.findIndex(sketch => {
Expand All @@ -32,9 +31,11 @@ function App() {
return { ...state, sketch };
});
}, [sketches]);

const onMountStateChange = useCallback(() => {
setState(state => ({ ...state, unmount: !state.unmount }));
}, []);

const onRotationChange = useCallback(
event => {
setState(state => ({
Expand Down
30 changes: 30 additions & 0 deletions demo/sketches/capture.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "p5.capture";

export function sketch(p5) {
p5.setup = () => {
p5.createCanvas(300, 300);
p5.capture.initialize(p5);
};

p5.draw = () => {
if (p5.frameCount === 1) {
const capture = p5.capture.getInstance();
capture.start({
format: "gif",
duration: 100
});
}
};

p5.keyPressed = () => {
if (key === "c") {
const capture = p5.capture.getInstance();

if (capture.state === "idle") {
capture.start();
} else {
capture.stop();
}
}
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"jiti": "^2.4.2",
"js": "^0.1.0",
"jsdom": "^26.0.0",
"p5.capture": "^1.6.0",
"prettier": "^3.5.1",
"react": "19.1.0",
"react-dom": "19.1.0",
Expand Down
Loading