Skip to content

Commit b3aa350

Browse files
authored
Merge pull request #2 from matteobruni/master
replaced particles.js with tsParticles
2 parents 17c7e98 + 9c66764 commit b3aa350

File tree

5 files changed

+44
-47
lines changed

5 files changed

+44
-47
lines changed

‎.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pkg
33
build
44
coverage
55
node_modules
6-
storybook-static
6+
storybook-static
7+
.idea/

‎package-lock.json

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,6 @@
145145
"dependencies": {
146146
"core-js": "^2.6.5",
147147
"lodash.merge": "^4.6.1",
148-
"particles.js": "^2.0.0"
148+
"tsparticles": "^1.12.11"
149149
}
150150
}

‎src/components/Particles/Particles.tsx

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import merge from 'lodash.merge';
2-
import React, { PureComponent } from 'react';
3-
import { getParams } from '../../utils';
2+
import React, {PureComponent} from 'react';
3+
import {getParams} from '../../utils';
4+
import {tsParticles} from 'tsparticles';
45

56
interface ParticlesProps {
67
/**
@@ -43,11 +44,6 @@ interface ParticlesProps {
4344
withDefaults?: boolean;
4445
}
4546

46-
interface CustomWindow extends Window {
47-
particlesJS?: any;
48-
pJSDom?: Array<any>;
49-
}
50-
5147
export class Particles extends PureComponent<ParticlesProps> {
5248
static displayName = 'Particles';
5349

@@ -60,38 +56,19 @@ export class Particles extends PureComponent<ParticlesProps> {
6056
};
6157

6258
componentDidMount() {
63-
require('particles.js');
64-
65-
var w: CustomWindow = window;
66-
67-
if ('particlesJS' in w) {
68-
w.particlesJS(this.props.id, this.getParticles());
69-
}
59+
tsParticles.load(this.props.id ?? "tsparticles", this.getParticles());
7060
}
7161

7262
componentDidUpdate(prevProps: ParticlesProps) {
73-
var w: CustomWindow = window;
74-
75-
if (
76-
'particlesJS' in w &&
77-
prevProps.id !== this.props.id &&
78-
prevProps.params !== this.props.params
79-
) {
80-
w.particlesJS(this.props.id, this.getParticles());
81-
}
63+
tsParticles.load(this.props.id ?? "tsparticles", this.getParticles());
8264
}
8365

8466
componentWillUnmount() {
85-
var w: CustomWindow = window;
86-
87-
if ('pJSDom' in w && w.pJSDom instanceof Array && w.pJSDom.length > 0) {
88-
w.pJSDom.forEach(({ pJS }) => pJS && pJS.fn.vendors.destroypJS());
89-
w.pJSDom = [];
90-
}
67+
tsParticles.dom().forEach((container) => container.destroy());
9168
}
9269

9370
render() {
94-
const { className, style, width, height, id } = this.props;
71+
const {className, style, width, height, id} = this.props;
9572

9673
return (
9774
<div

‎src/utils/index.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import merge from 'lodash.merge';
2+
import {RecursivePartial} from "tsparticles/dist/Types/RecursivePartial";
3+
import {IOptions} from "tsparticles/dist/Interfaces/Options/IOptions";
4+
import {ShapeType} from "tsparticles/dist/Enums/ShapeType";
5+
import {MoveDirection} from "tsparticles/dist/Enums/MoveDirection";
6+
import {OutMode} from "tsparticles/dist/Enums/OutMode";
7+
import {InteractivityDetect} from "tsparticles/dist/Enums/InteractivityDetect";
8+
import {HoverMode} from "tsparticles/dist/Enums/Modes/HoverMode";
9+
import {ClickMode} from "tsparticles/dist/Enums/Modes/ClickMode";
10+
import {PolygonMaskType} from "tsparticles/dist/Enums/PolygonMaskType";
11+
import {PolygonMaskInlineArrangement} from "tsparticles/dist/Enums/PolygonMaskInlineArrangement";
12+
import {PolygonMaskMoveType} from "tsparticles/dist/Enums/PolygonMaskMoveType";
213

3-
export const getParams = () =>
14+
export const getParams = (): RecursivePartial<IOptions> =>
415
merge(
516
{},
617
{
@@ -17,7 +28,7 @@ export const getParams = () =>
1728
value: '#FFF',
1829
},
1930
shape: {
20-
type: 'circle',
31+
type: ShapeType.circle,
2132
stroke: {
2233
width: 0,
2334
color: '#000000',
@@ -67,10 +78,10 @@ export const getParams = () =>
6778
move: {
6879
enable: true,
6980
speed: 3,
70-
direction: 'none',
81+
direction: MoveDirection.none,
7182
random: false,
7283
straight: false,
73-
out_mode: 'bounce',
84+
out_mode: OutMode.bounce,
7485
bounce: true,
7586
attract: {
7687
enable: false,
@@ -81,15 +92,15 @@ export const getParams = () =>
8192
array: [],
8293
},
8394
interactivity: {
84-
detect_on: 'canvas',
95+
detect_on: InteractivityDetect.canvas,
8596
events: {
8697
onhover: {
8798
enable: false,
88-
mode: 'grab',
99+
mode: HoverMode.grab,
89100
},
90101
onclick: {
91102
enable: false,
92-
mode: 'repulse',
103+
mode: ClickMode.repulse,
93104
},
94105
resize: true,
95106
},
@@ -122,9 +133,9 @@ export const getParams = () =>
122133
polygon: {
123134
enable: false,
124135
scale: 1,
125-
type: 'inline',
136+
type: PolygonMaskType.inline,
126137
inline: {
127-
arrangement: 'one-per-point',
138+
arrangement: PolygonMaskInlineArrangement.onePerPoint,
128139
},
129140
draw: {
130141
enable: false,
@@ -135,7 +146,7 @@ export const getParams = () =>
135146
},
136147
move: {
137148
radius: 10,
138-
type: 'path',
149+
type: PolygonMaskMoveType.path,
139150
},
140151
url: '',
141152
},

0 commit comments

Comments
 (0)