Skip to content

Commit 3600c9e

Browse files
committed
add props to options
1 parent abcd9e4 commit 3600c9e

File tree

5 files changed

+53
-48
lines changed

5 files changed

+53
-48
lines changed

examples/App.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ body,
4949
text-align: center;
5050
color: #fff;
5151
.banner-wraper {
52-
height: 100%;
5352
display: flex;
5453
justify-content: center;
5554
align-items: center;

examples/components/banner.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export default {
1313

1414
<style lang="scss" scoped>
1515
.banner {
16-
height: 100%;
1716
display: flex;
1817
justify-content: center;
1918
align-items: center;

examples/components/footer.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
<script>
88
export default {
9-
name: 'footer'
9+
name: 'foot'
1010
}
1111
</script>

packages/vue-particle-line/src/particle-line/index.js

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default class ParticleLine {
88
constructor (tagId, options) {
99
this.tagId = tagId
1010
this.options = options
11+
this.init()
1112
}
1213

1314
init () {
@@ -17,37 +18,28 @@ export default class ParticleLine {
1718
canvas.height = document.body.clientHeight > minHeight ? document.body.clientHeight : minHeight
1819
ctx.lineWidth = (this.options && this.options.lineWidth) || 0.3
1920
ctx.strokeStyle = (new Color(150)).style
20-
this.mousePosition = {
21-
x: 30 * canvas.width / 100,
22-
y: 30 * canvas.height / 100
23-
}
2421
this.dots = {
25-
nb: 100, // 250
26-
distance: 80, // 100
27-
d_radius: 10, // 150
22+
nb: (this.options && this.options.dotsNumber) || 100,
23+
distance: (this.options && this.options.dotsDistance) || 100,
2824
array: []
2925
}
30-
// this.dots = {
31-
// nb: 250, // 250
32-
// distance: 100, // 100
33-
// d_radius: 150, // 150
34-
// array: []
35-
// }
3626
this.canvas = canvas
3727
this.ctx = ctx
3828
this.color = new Color()
39-
this.createDots(ctx, canvas.width, canvas.height)
40-
requestAnimationFrame(this.animateDots.bind(this))
41-
// console.log(this.dots.array)
42-
// canvas.addEventListener('mousemove', e => {
43-
// // console.log(this.ctx, this.canvas.width, this.canvas.height, e.pageX, e.pageY)
44-
// // // console.log(e.pageY)
45-
// // this.dots.array.push(new Dot(this.ctx, this.canvas.width, this.canvas.height, e.pageX, e.pageY))
46-
// // this.connectDots()
47-
// this.mousePosition.x = e.pageX
48-
// this.mousePosition.y = e.pageY
49-
// // console.log(this.dots.array)
50-
// })
29+
this.createDots(this.ctx, this.canvas.width, this.canvas.height)
30+
this.animateDots()
31+
this.hoverEffect()
32+
}
33+
34+
hoverEffect () {
35+
if (this.options && this.options.hoverEffect) {
36+
this.canvas.addEventListener('mousemove', e => {
37+
if (this.dots.array.length > this.dots.nb) {
38+
this.dots.array.pop()
39+
}
40+
this.dots.array.push(new Dot(this.ctx, this.canvas.width, this.canvas.height, e.pageX, e.pageY))
41+
})
42+
}
5143
}
5244

5345
resize () {
@@ -73,7 +65,7 @@ export default class ParticleLine {
7365

7466
createDots (ctx, canvasWidth, canvasHeight) {
7567
this.dots.array = []
76-
for (let i = 0; i < this.dots.nb; i++) { // TODO:屏幕变化应该添加进去,不是替换
68+
for (let i = 0; i < this.dots.nb; i++) {
7769
this.dots.array.push(new Dot(ctx, canvasWidth, canvasHeight))
7870
}
7971
}
@@ -94,19 +86,11 @@ export default class ParticleLine {
9486
}
9587

9688
connectDots () {
97-
for (let i = 0; i < this.dots.nb; i++) {
98-
for (let j = 0; j < this.dots.nb; j++) {
89+
for (let i = 0; i < this.dots.array.length; i++) {
90+
for (let j = 0; j < this.dots.array.length; j++) {
9991
const iDot = this.dots.array[i]
10092
const jDot = this.dots.array[j]
10193
if ((iDot.x - jDot.x) < this.dots.distance && (iDot.y - jDot.y) < this.dots.distance && (iDot.x - jDot.x) > -this.dots.distance && (iDot.y - jDot.y) > -this.dots.distance) {
102-
// if ((iDot.x - this.mousePosition.x) < this.dots.d_radius && (iDot.y - this.mousePosition.y) < this.dots.d_radius && (iDot.x - this.mousePosition.x) > -this.dots.d_radius && (iDot.y - this.mousePosition.y) > -this.dots.d_radius) {
103-
// this.ctx.beginPath()
104-
// this.ctx.strokeStyle = this.averageColorStyles(iDot, jDot)
105-
// this.ctx.moveTo(iDot.x, iDot.y)
106-
// this.ctx.lineTo(jDot.x, jDot.y)
107-
// this.ctx.stroke()
108-
// this.ctx.closePath()
109-
// }
11094
this.ctx.beginPath()
11195
this.ctx.strokeStyle = this.averageColorStyles(iDot, jDot)
11296
this.ctx.moveTo(iDot.x, iDot.y)
@@ -119,17 +103,17 @@ export default class ParticleLine {
119103
}
120104

121105
drawDots () {
122-
for (let i = 0; i < this.dots.nb; i++) {
106+
for (let i = 0; i < this.dots.array.length; i++) {
123107
const dot = this.dots.array[i]
124108
dot.draw()
125109
}
126110
}
127111

128112
animateDots () {
129113
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
130-
this.moveDots()
131-
this.connectDots()
132114
this.drawDots()
115+
this.connectDots()
116+
this.moveDots()
133117
requestAnimationFrame(this.animateDots.bind(this))
134118
}
135119
}

packages/vue-particle-line/src/vue-particle-line.vue

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,37 @@
99

1010
<script>
1111
import ParticleLine from './particle-line'
12-
import { debounce } from 'common/js/utils'
12+
// import { debounce } from 'common/js/utils'
1313
export default {
1414
name: 'vue-particle-line',
15+
props: {
16+
lineWidth: {
17+
type: Number,
18+
default: 0.3
19+
},
20+
dotsNumber: {
21+
type: Number,
22+
default: 100
23+
},
24+
dotsDistance: {
25+
type: Number,
26+
default: 100
27+
},
28+
hoverEffect: {
29+
type: Boolean,
30+
default: true
31+
}
32+
},
1533
mounted () {
16-
const particleLine = new ParticleLine('canvas')
17-
particleLine.init()
18-
window.onresize = debounce(() => particleLine.resize(), 500)
34+
/* eslint-disable no-new */
35+
new ParticleLine('canvas', {
36+
lineWidth: this.lineWidth,
37+
dotsNumber: this.dotsNumber,
38+
dotsDistance: this.dotsDistance,
39+
hoverEffect: this.hoverEffect
40+
})
41+
// particleLine.init()
42+
// window.onresize = debounce(() => particleLine.resize(), 500)
1943
}
2044
}
2145
</script>
@@ -26,8 +50,7 @@ export default {
2650
width: 100%;
2751
height: 100%;
2852
.slot-wraper {
29-
height: 100%;
30-
z-index: 99;
53+
z-index: 1;
3154
}
3255
.canvas {
3356
position: absolute;

0 commit comments

Comments
 (0)