Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion public/r/Ballpit-JS-CSS.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/r/Ballpit-JS-TW.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/r/Ballpit-TS-CSS.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/r/Ballpit-TS-TW.json

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions src/content/Backgrounds/Ballpit/Ballpit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,19 @@ function createBallpit(e, t = {}) {
setCount(e) {
initialize({ ...s.config, count: e });
},
updateConfig(newProps) {
if (newProps.count !== undefined && newProps.count !== s.config.count) {
initialize({ ...s.config, ...newProps });
} else {
Object.assign(s.config, newProps);
if (newProps.colors) {
s.setColors(s.config.colors);
}
if (newProps.minSize !== undefined || newProps.maxSize !== undefined || newProps.size0 !== undefined) {
s.physics.setSizes();
}
}
},
togglePause() {
c = !c;
},
Expand All @@ -724,6 +737,7 @@ function createBallpit(e, t = {}) {
const Ballpit = ({ className = '', followCursor = true, ...props }) => {
const canvasRef = useRef(null);
const spheresInstanceRef = useRef(null);
const isFirstRender = useRef(true);

useEffect(() => {
const canvas = canvasRef.current;
Expand All @@ -734,11 +748,22 @@ const Ballpit = ({ className = '', followCursor = true, ...props }) => {
return () => {
if (spheresInstanceRef.current) {
spheresInstanceRef.current.dispose();
spheresInstanceRef.current = null;
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}
if (spheresInstanceRef.current) {
spheresInstanceRef.current.updateConfig({ followCursor, ...props });
}
}, [props, followCursor]);

return <canvas className={className} ref={canvasRef} style={{ width: '100%', height: '100%' }} />;
};

Expand Down
25 changes: 25 additions & 0 deletions src/tailwind/Backgrounds/Ballpit/Ballpit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,19 @@ function createBallpit(e, t = {}) {
setCount(e) {
initialize({ ...s.config, count: e });
},
updateConfig(newProps) {
if (newProps.count !== undefined && newProps.count !== s.config.count) {
initialize({ ...s.config, ...newProps });
} else {
Object.assign(s.config, newProps);
if (newProps.colors) {
s.setColors(s.config.colors);
}
if (newProps.minSize !== undefined || newProps.maxSize !== undefined || newProps.size0 !== undefined) {
s.physics.setSizes();
}
}
},
togglePause() {
c = !c;
},
Expand All @@ -724,6 +737,7 @@ function createBallpit(e, t = {}) {
const Ballpit = ({ className = '', followCursor = true, ...props }) => {
const canvasRef = useRef(null);
const spheresInstanceRef = useRef(null);
const isFirstRender = useRef(true);

useEffect(() => {
const canvas = canvasRef.current;
Expand All @@ -734,11 +748,22 @@ const Ballpit = ({ className = '', followCursor = true, ...props }) => {
return () => {
if (spheresInstanceRef.current) {
spheresInstanceRef.current.dispose();
spheresInstanceRef.current = null;
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}
if (spheresInstanceRef.current) {
spheresInstanceRef.current.updateConfig({ followCursor, ...props });
}
}, [props, followCursor]);

return <canvas className={`${className} w-full h-full`} ref={canvasRef} />;
};

Expand Down
26 changes: 26 additions & 0 deletions src/ts-default/Backgrounds/Ballpit/Ballpit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ interface CreateBallpitReturn {
three: X;
spheres: Z;
setCount: (count: number) => void;
updateConfig: (newProps: { [key: string]: any }) => void;
togglePause: () => void;
dispose: () => void;
}
Expand Down Expand Up @@ -837,6 +838,19 @@ function createBallpit(canvas: HTMLCanvasElement, config: any = {}): CreateBallp
setCount(count: number) {
initialize({ ...spheres.config, count });
},
updateConfig(newProps: { [key: string]: any }) {
if (newProps.count !== undefined && newProps.count !== spheres.config.count) {
initialize({ ...spheres.config, ...newProps });
} else {
Object.assign(spheres.config, newProps);
if (newProps.colors) {
spheres.setColors(spheres.config.colors);
}
if (newProps.minSize !== undefined || newProps.maxSize !== undefined || newProps.size0 !== undefined) {
spheres.physics.setSizes();
}
}
},
togglePause() {
isPaused = !isPaused;
},
Expand All @@ -856,6 +870,7 @@ interface BallpitProps {
const Ballpit: React.FC<BallpitProps> = ({ className = '', followCursor = true, ...props }) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const spheresInstanceRef = useRef<CreateBallpitReturn | null>(null);
const isFirstRender = useRef(true);

useEffect(() => {
const canvas = canvasRef.current;
Expand All @@ -869,11 +884,22 @@ const Ballpit: React.FC<BallpitProps> = ({ className = '', followCursor = true,
return () => {
if (spheresInstanceRef.current) {
spheresInstanceRef.current.dispose();
spheresInstanceRef.current = null;
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}
if (spheresInstanceRef.current) {
spheresInstanceRef.current.updateConfig({ followCursor, ...props });
}
}, [props, followCursor]);

return <canvas className={className} ref={canvasRef} style={{ width: '100%', height: '100%' }} />;
};

Expand Down
28 changes: 27 additions & 1 deletion src/ts-tailwind/Backgrounds/Ballpit/Ballpit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ class Y extends MeshPhysicalMaterial {
thicknessPower: { value: 2 },
thicknessScale: { value: 10 }
};
defines: { USE_UV: string; };
defines: { USE_UV: string };

constructor(params: any) {
super(params);
Expand Down Expand Up @@ -771,6 +771,7 @@ interface CreateBallpitReturn {
three: X;
spheres: Z;
setCount: (count: number) => void;
updateConfig: (newProps: { [key: string]: any }) => void;
togglePause: () => void;
dispose: () => void;
}
Expand Down Expand Up @@ -833,6 +834,19 @@ function createBallpit(canvas: HTMLCanvasElement, config: any = {}): CreateBallp
setCount(count: number) {
initialize({ ...spheres.config, count });
},
updateConfig(newProps: { [key: string]: any }) {
if (newProps.count !== undefined && newProps.count !== spheres.config.count) {
initialize({ ...spheres.config, ...newProps });
} else {
Object.assign(spheres.config, newProps);
if (newProps.colors) {
spheres.setColors(spheres.config.colors);
}
if (newProps.minSize !== undefined || newProps.maxSize !== undefined || newProps.size0 !== undefined) {
spheres.physics.setSizes();
}
}
},
togglePause() {
isPaused = !isPaused;
},
Expand All @@ -852,6 +866,7 @@ interface BallpitProps {
const Ballpit: React.FC<BallpitProps> = ({ className = '', followCursor = true, ...props }) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const spheresInstanceRef = useRef<CreateBallpitReturn | null>(null);
const isFirstRender = useRef(true);

useEffect(() => {
const canvas = canvasRef.current;
Expand All @@ -865,11 +880,22 @@ const Ballpit: React.FC<BallpitProps> = ({ className = '', followCursor = true,
return () => {
if (spheresInstanceRef.current) {
spheresInstanceRef.current.dispose();
spheresInstanceRef.current = null;
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}
if (spheresInstanceRef.current) {
spheresInstanceRef.current.updateConfig({ followCursor, ...props });
}
}, [props, followCursor]);

return <canvas className={`${className} w-full h-full`} ref={canvasRef} />;
};

Expand Down