Skip to content

Commit 740229a

Browse files
committed
docs(changeset): Fix mouse following for non-fullscreen layout elements.
1 parent 54ed6db commit 740229a

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

.changeset/six-cups-decide.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webgl-generative-particles": patch
3+
---
4+
5+
Fix mouse following for non-fullscreen layout elements.

examples/nextjs/src/app/page.tsx

+10
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/src/simulator.ts

+8-5
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

0 commit comments

Comments
 (0)