Skip to content

Commit aae5f5a

Browse files
authored
Support customOptions object + code clean up
Now you can add a *customOptions* property with an object as particles.js settings. In this way, the component becomes compatible with the classic JSON particles.js configuration.
1 parent fefae8b commit aae5f5a

File tree

1 file changed

+127
-224
lines changed

1 file changed

+127
-224
lines changed

src/vue-particles/vue-particles.vue

Lines changed: 127 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -1,250 +1,153 @@
11
<template>
2-
<div
3-
class="particles-js"
4-
:id="id"
5-
:color="color"
6-
:particleOpacity="particleOpacity"
7-
:linesColor="linesColor"
8-
:particlesNumber="particlesNumber"
9-
:shapeType="shapeType"
10-
:particleSize="particleSize"
11-
:linesWidth="linesWidth"
12-
:lineLinked="lineLinked"
13-
:lineOpacity="lineOpacity"
14-
:linesDistance="linesDistance"
15-
:moveSpeed="moveSpeed"
16-
:hoverEffect="hoverEffect"
17-
:hoverMode="hoverMode"
18-
:clickEffect="clickEffect"
19-
:clickMode="clickMode"
20-
></div>
2+
<div id="particles-js"></div>
213
</template>
224
<script>
235
/* eslint-disable */
246
export default {
257
name: 'vue-particles',
26-
data: function () {
27-
return {
28-
id: 'particles-instance-' + Math.floor(Math.random() * 5000)
29-
}
30-
},
318
props: {
32-
color: {
33-
type: String,
34-
default: '#dedede'
35-
},
36-
particleOpacity: {
37-
type: Number,
38-
default: 0.7
39-
},
40-
particlesNumber: {
41-
type: Number,
42-
default: 80
43-
},
44-
shapeType: {
45-
type: String,
46-
default: 'circle'
47-
},
48-
particleSize: {
49-
type: Number,
50-
default: 4
51-
},
52-
linesColor: {
53-
type: String,
54-
default: '#dedede'
55-
},
56-
linesWidth: {
57-
type: Number,
58-
default: 1
59-
},
60-
lineLinked: {
61-
type: Boolean,
62-
default: true
63-
},
64-
lineOpacity: {
65-
type: Number,
66-
default: 0.4
67-
},
68-
linesDistance: {
69-
type: Number,
70-
default: 150
71-
},
72-
moveSpeed: {
73-
type: Number,
74-
default: 3
75-
},
76-
hoverEffect: {
77-
type: Boolean,
78-
default: true
79-
},
80-
hoverMode: {
81-
type: String,
82-
default: 'grab'
83-
},
84-
clickEffect: {
85-
type: Boolean,
86-
default: true
87-
},
88-
clickMode: {
89-
type: String,
90-
default: 'push'
91-
}
9+
color: { type: String, default: '#dedede' },
10+
particleOpacity: { type: Number, default: 0.7 },
11+
particlesNumber: { type: Number, default: 80 },
12+
shapeType: { type: String, default: 'circle' },
13+
particleSize: { type: Number, default: 4 },
14+
linesColor: { type: String, default: '#dedede' },
15+
linesWidth: { type: Number, default: 1 },
16+
lineLinked: { type: Boolean, default: true },
17+
lineOpacity: { type: Number, default: 0.4 },
18+
linesDistance: { type: Number, default: 150 },
19+
moveSpeed: { type: Number, default: 3 },
20+
hoverEffect: { type: Boolean, default: true },
21+
hoverMode: { type: String, default: 'grab' },
22+
clickEffect: { type: Boolean, default: true },
23+
clickMode: { type: String, default: 'push' },
24+
customOptions: { type: Object, default: function () { return {} } }
9225
},
9326
mounted () {
9427
// import particle.js only on client-side
9528
require('particles.js')
96-
this.$nextTick(() => {
97-
this.initParticleJS(
98-
this.color,
99-
this.particleOpacity,
100-
this.particlesNumber,
101-
this.shapeType,
102-
this.particleSize,
103-
this.linesColor,
104-
this.linesWidth,
105-
this.lineLinked,
106-
this.lineOpacity,
107-
this.linesDistance,
108-
this.moveSpeed,
109-
this.hoverEffect,
110-
this.hoverMode,
111-
this.clickEffect,
112-
this.clickMode
113-
)
114-
})
29+
this.$nextTick(() => { this.initParticleJS() })
11530
},
11631
methods: {
117-
initParticleJS (
118-
color,
119-
particleOpacity,
120-
particlesNumber,
121-
shapeType,
122-
particleSize,
123-
linesColor,
124-
linesWidth,
125-
lineLinked,
126-
lineOpacity,
127-
linesDistance,
128-
moveSpeed,
129-
hoverEffect,
130-
hoverMode,
131-
clickEffect,
132-
clickMode
133-
) {
134-
particlesJS(this.id, {
135-
"particles": {
136-
"number": {
137-
"value": particlesNumber,
138-
"density": {
139-
"enable": true,
140-
"value_area": 800
141-
}
142-
},
143-
"color": {
144-
"value": color
145-
},
146-
"shape": {
147-
// circle, edge, triangle, polygon, star, image
148-
"type": shapeType,
149-
"stroke": {
150-
"width": 0,
151-
"color": "#192231"
32+
initParticleJS () {
33+
const isCustom = (Object.keys(this.customOptions).length > 0)
34+
35+
if (isCustom) { particlesJS('particles-js', this.customOptions) }
36+
else {
37+
particlesJS('particles-js', {
38+
"particles": {
39+
"number": {
40+
"value": this.particlesNumber,
41+
"density": {
42+
"enable": true,
43+
"value_area": 800
44+
}
15245
},
153-
"polygon": {
154-
"nb_sides": 5
155-
}
156-
},
157-
"opacity": {
158-
"value": particleOpacity,
159-
"random": false,
160-
"anim": {
161-
"enable": false,
162-
"speed": 1,
163-
"opacity_min": 0.1,
164-
"sync": false
165-
}
166-
},
167-
"size": {
168-
"value": particleSize,
169-
"random": true,
170-
"anim": {
171-
"enable": false,
172-
"speed": 40,
173-
"size_min": 0.1,
174-
"sync": false
175-
}
176-
},
177-
"line_linked": {
178-
"enable": lineLinked,
179-
"distance": linesDistance,
180-
"color": linesColor,
181-
"opacity": lineOpacity,
182-
"width": linesWidth
183-
},
184-
"move": {
185-
"enable": true,
186-
"speed": moveSpeed,
187-
"direction": "none",
188-
"random": false,
189-
"straight": false,
190-
"out_mode": "out",
191-
"bounce": false,
192-
"attract": {
193-
"enable": false,
194-
"rotateX": 600,
195-
"rotateY": 1200
196-
}
197-
}
198-
},
199-
"interactivity": {
200-
"detect_on": "canvas",
201-
"events": {
202-
"onhover": {
203-
"enable": hoverEffect,
204-
"mode": hoverMode
46+
"color": {
47+
"value": this.color
20548
},
206-
"onclick": {
207-
"enable": clickEffect,
208-
"mode": clickMode
49+
"shape": {
50+
// circle, edge, triangle, polygon, star, image
51+
"type": this.shapeType,
52+
"stroke": {
53+
"width": 0,
54+
"color": "#192231"
55+
},
56+
"polygon": {
57+
"nb_sides": 5
58+
}
20959
},
210-
"onresize": {
211-
212-
"enable": true,
213-
"density_auto": true,
214-
"density_area": 400
215-
216-
}
217-
},
218-
"modes": {
219-
"grab": {
220-
"distance": 140,
221-
"line_linked": {
222-
"opacity": 1
60+
"opacity": {
61+
"value": this.particleOpacity,
62+
"random": false,
63+
"anim": {
64+
"enable": false,
65+
"speed": 1,
66+
"opacity_min": 0.1,
67+
"sync": false
22368
}
22469
},
225-
"bubble": {
226-
"distance": 400,
227-
"size": 40,
228-
"duration": 2,
229-
"opacity": 8,
230-
"speed": 3
70+
"size": {
71+
"value": this.particleSize,
72+
"random": true,
73+
"anim": {
74+
"enable": false,
75+
"speed": 40,
76+
"size_min": 0.1,
77+
"sync": false
78+
}
23179
},
232-
"repulse": {
233-
"distance": 200,
234-
"duration": 0.4
80+
"line_linked": {
81+
"enable": this.lineLinked,
82+
"distance": this.linesDistance,
83+
"color": this.linesColor,
84+
"opacity": this.lineOpacity,
85+
"width": this.linesWidth
23586
},
236-
"push": {
237-
"particles_nb": 4
87+
"move": {
88+
"enable": true,
89+
"speed": this.moveSpeed,
90+
"direction": "none",
91+
"random": false,
92+
"straight": false,
93+
"out_mode": "out",
94+
"bounce": false,
95+
"attract": {
96+
"enable": false,
97+
"rotateX": 600,
98+
"rotateY": 1200
99+
}
100+
}
101+
},
102+
"interactivity": {
103+
"detect_on": "canvas",
104+
"events": {
105+
"onhover": {
106+
"enable": this.hoverEffect,
107+
"mode": this.hoverMode
108+
},
109+
"onclick": {
110+
"enable": this.clickEffect,
111+
"mode": this.clickMode
112+
},
113+
"onresize": {
114+
115+
"enable": true,
116+
"density_auto": true,
117+
"density_area": 400
118+
119+
}
238120
},
239-
"remove": {
240-
"particles_nb": 2
121+
"modes": {
122+
"grab": {
123+
"distance": 140,
124+
"line_linked": {
125+
"opacity": 1
126+
}
127+
},
128+
"bubble": {
129+
"distance": 400,
130+
"size": 40,
131+
"duration": 2,
132+
"opacity": 8,
133+
"speed": 3
134+
},
135+
"repulse": {
136+
"distance": 200,
137+
"duration": 0.4
138+
},
139+
"push": {
140+
"particles_nb": 4
141+
},
142+
"remove": {
143+
"particles_nb": 2
144+
}
241145
}
242-
}
243-
},
244-
"retina_detect": true
245-
});
146+
},
147+
"retina_detect": true
148+
})
149+
}
246150
}
247-
248151
}
249152
}
250153
/* eslint-disable */

0 commit comments

Comments
 (0)