Skip to content

Commit 89acf92

Browse files
authored
Merge pull request #2 from react18-tools/scale
Aspect Ratio Preservation, Mouse Tracking Fix, and Entropy Improvement
2 parents 7ade454 + 7a8821d commit 89acf92

File tree

8 files changed

+53
-15
lines changed

8 files changed

+53
-15
lines changed

examples/nextjs/src/app/page.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ export default function Page(): JSX.Element {
2424
origin: [0, -1],
2525
}}
2626
/>
27+
<Particles
28+
style={{
29+
height: "300px",
30+
width: "60vw",
31+
position: "absolute",
32+
left: "20vw",
33+
top: 0,
34+
zIndex: 5,
35+
}}
36+
/>
2737
<Demo />
2838
</LandingPage>
2939
);

lib/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# webgl-generative-particles
2+
3+
## 0.0.1
4+
5+
### Patch Changes
6+
7+
- 8717bbb: Fix mouse following for non-fullscreen layout elements.
8+
- 5414b18: Add extra random number for better entropy.
9+
- 0a8900d: Scale the rendering to preserve aspect ratio.

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "webgl-generative-particles",
33
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
44
"private": false,
5-
"version": "0.0.0",
5+
"version": "0.0.1",
66
"description": "An efficient WebGL-based generative particle system.",
77
"license": "MPL-2.0",
88
"main": "./dist/index.js",

lib/src/react/particles/particles.test.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import { cleanup, render, screen } from "@testing-library/react";
22
import { afterEach, describe, test } from "vitest";
33
import { Particles } from "./particles";
44

5-
describe.concurrent("particles", () => {
6-
afterEach(cleanup);
5+
describe("particles", () => {
6+
afterEach(cleanup);
77

8-
test("Dummy test - test if renders without errors", ({ expect }) => {
9-
const clx = "my-class";
10-
render(<Particles className={clx} />);
11-
expect(screen.getByTestId("particles").classList).toContain(clx);
12-
});
8+
test("Test if renders without errors", ({ expect }) => {
9+
const clx = "my-class";
10+
render(<Particles className={clx} />);
11+
expect(screen.getByTestId("particles").classList).toContain(clx);
12+
});
13+
14+
test("Test fullScreen overlay mode", ({ expect }) => {
15+
render(<Particles fullScreenOverlay />);
16+
expect(screen.getByTestId("particles").style.width).toBe("100vw");
17+
});
1318
});

lib/src/react/particles/particles.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const Particles = ({ options, overlay, fullScreenOverlay, ...props }: Par
3434
() =>
3535
canvasRef.current
3636
? renderParticles(canvasRef.current, { ...options, overlay: resolvedOverlay })
37-
: undefined,
37+
: /* v8 ignore next */
38+
undefined,
3839
[options, resolvedOverlay],
3940
);
4041
return <canvas ref={canvasRef} style={style} {...props} data-testid="particles" />;

lib/src/simulator.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export interface ParticlesOptions {
5757
const defaultOptions: ParticlesOptions = {
5858
rgba: [1, 0, 0, 0.5],
5959
maxParticles: 1000,
60-
generationRate: 0.25,
60+
generationRate: 0.5,
6161
// setting range from -PI to PI craetes some patches because of overflows
6262
angleRange: [-2 * PI, 2 * PI],
6363
origin: [-1, -1],
64-
speedRange: [0.01, 0.1],
65-
ageRange: [0.01, 0.5],
64+
speedRange: [0.02, 0.2],
65+
ageRange: [0.01, 0.6],
6666
forceField: [0, 0.1],
6767
};
6868

@@ -339,10 +339,13 @@ export const renderParticles = (canvas: HTMLCanvasElement, options?: ParticlesOp
339339
const onMouseMove = (e: MouseEvent) => {
340340
const height = canvas.height;
341341
const width = canvas.width;
342+
const boundingRect = canvas.getBoundingClientRect();
343+
const xPos = e.pageX - boundingRect.left - scrollX;
344+
const yPos = e.pageY - boundingRect.top - scrollY;
342345
const scale = height > width ? [1, height / width] : [width / height, 1];
343346
setOrigin(
344-
((e.clientX / canvas.width) * 2 - 1) * scale[0],
345-
(1 - (e.clientY / canvas.height) * 2) * scale[1],
347+
((xPos / canvas.width) * 2 - 1) * scale[0],
348+
(1 - (yPos / canvas.height) * 2) * scale[1],
346349
);
347350
};
348351

packages/shared/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# @repo/shared
2+
3+
## 0.0.1
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [8717bbb]
8+
- Updated dependencies [5414b18]
9+
- Updated dependencies [0a8900d]
10+

packages/shared/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@repo/shared",
3-
"version": "0.0.0",
3+
"version": "0.0.1",
44
"private": true,
55
"sideEffects": false,
66
"main": "./dist/index.js",

0 commit comments

Comments
 (0)