Skip to content

Commit 3e5be2d

Browse files
committed
Cosmetics around react component
1 parent f9125f0 commit 3e5be2d

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

examples/nextjs/src/app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function RootLayout({ children }: { children: React.ReactNode }):
2020
{children}
2121
</Layout>
2222
<GlobalLoader />
23-
<Particles style={{ width: "100vw", height: "100vh" }} options={{ overlay: true }} />
23+
<Particles fullScreenOverlay />
2424
</body>
2525
</html>
2626
);
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { HTMLProps, useEffect, useRef } from "react";
1+
import { CSSProperties, HTMLProps, useEffect, useRef } from "react";
22
import { ParticlesOptions, renderParticles } from "../../simulator";
33

44
export interface ParticlesProps extends HTMLProps<HTMLCanvasElement> {
55
options?: ParticlesOptions;
6+
overlay?: boolean;
7+
fullScreenOverlay?: boolean;
68
}
79

810
/**
@@ -15,18 +17,21 @@ export interface ParticlesProps extends HTMLProps<HTMLCanvasElement> {
1517
*
1618
* @source - Source code
1719
*/
18-
export const Particles = ({ options, style, ...props }: ParticlesProps) => {
20+
export const Particles = ({ options, overlay, fullScreenOverlay, ...props }: ParticlesProps) => {
1921
const canvasRef = useRef<HTMLCanvasElement>(null);
22+
23+
const fsStyles = fullScreenOverlay
24+
? { position: "fixed", top: 0, left: 0, width: "100vw", height: "100vh" }
25+
: {};
26+
27+
const style = (
28+
overlay ? { pointerEvents: "none", ...fsStyles, ...props.style } : props.style
29+
) as CSSProperties;
30+
2031
useEffect(
21-
() => (canvasRef.current ? renderParticles(canvasRef.current, options) : undefined),
22-
[options],
23-
);
24-
return (
25-
<canvas
26-
ref={canvasRef}
27-
style={{ pointerEvents: "none", position: "fixed", top: 0, left: 0, ...style }}
28-
{...props}
29-
data-testid="particles"
30-
/>
32+
() =>
33+
canvasRef.current ? renderParticles(canvasRef.current, { ...options, overlay }) : undefined,
34+
[options, overlay],
3135
);
36+
return <canvas ref={canvasRef} style={style} {...props} data-testid="particles" />;
3237
};

lib/src/simulator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const defaultOptions: ParticlesOptions = {
4747

4848
const getInitialData = (maxParticles: number) => {
4949
const data = [];
50-
for (let i = 0; i < maxParticles; i++) data.push(0.0, 0.0, 1.0, 0.0, 0.0);
50+
for (let i = 0; i < maxParticles; i++) data.push(0, 0, 1, 0, 0);
5151
return data;
5252
};
5353

@@ -171,7 +171,7 @@ const simulate = (gl: WebGL2RenderingContext, options = defaultOptions) => {
171171
gl.bindBuffer(gl.ARRAY_BUFFER, null);
172172
});
173173

174-
gl.clearColor(1.0, 0, 0, 1);
174+
gl.clearColor(0, 0, 0, 0);
175175

176176
const rgNoiseTexture = gl.createTexture();
177177
gl.bindTexture(gl.TEXTURE_2D, rgNoiseTexture);
@@ -213,12 +213,12 @@ const simulate = (gl: WebGL2RenderingContext, options = defaultOptions) => {
213213
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
214214
gl.useProgram(updateProgram);
215215

216-
setUniform(U_DT, dt / 1000.0);
217-
setUniform(U_FORCE_FIELD, 0.0, -0.1);
216+
setUniform(U_DT, dt / 1000);
217+
setUniform(U_FORCE_FIELD, 0, -0.1);
218218
setUniform(U_ORIGIN, mouseX, mouseY);
219219
gl.activeTexture(gl.TEXTURE0);
220220
gl.bindTexture(gl.TEXTURE_2D, rgNoiseTexture);
221-
setUniform(U_RANDOM_RG, 0.0);
221+
setUniform(U_RANDOM_RG, 0);
222222

223223
gl.bindVertexArray(vertexArrayObjects[readIndex]);
224224
gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, 0, buffers[writeIndex]);

0 commit comments

Comments
 (0)