@@ -8,6 +8,7 @@ export default class ParticleLine {
8
8
constructor ( tagId , options ) {
9
9
this . tagId = tagId
10
10
this . options = options
11
+ this . init ( )
11
12
}
12
13
13
14
init ( ) {
@@ -17,37 +18,28 @@ export default class ParticleLine {
17
18
canvas . height = document . body . clientHeight > minHeight ? document . body . clientHeight : minHeight
18
19
ctx . lineWidth = ( this . options && this . options . lineWidth ) || 0.3
19
20
ctx . strokeStyle = ( new Color ( 150 ) ) . style
20
- this . mousePosition = {
21
- x : 30 * canvas . width / 100 ,
22
- y : 30 * canvas . height / 100
23
- }
24
21
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 ,
28
24
array : [ ]
29
25
}
30
- // this.dots = {
31
- // nb: 250, // 250
32
- // distance: 100, // 100
33
- // d_radius: 150, // 150
34
- // array: []
35
- // }
36
26
this . canvas = canvas
37
27
this . ctx = ctx
38
28
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
+ }
51
43
}
52
44
53
45
resize ( ) {
@@ -73,7 +65,7 @@ export default class ParticleLine {
73
65
74
66
createDots ( ctx , canvasWidth , canvasHeight ) {
75
67
this . dots . array = [ ]
76
- for ( let i = 0 ; i < this . dots . nb ; i ++ ) { // TODO:屏幕变化应该添加进去,不是替换
68
+ for ( let i = 0 ; i < this . dots . nb ; i ++ ) {
77
69
this . dots . array . push ( new Dot ( ctx , canvasWidth , canvasHeight ) )
78
70
}
79
71
}
@@ -94,19 +86,11 @@ export default class ParticleLine {
94
86
}
95
87
96
88
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 ++ ) {
99
91
const iDot = this . dots . array [ i ]
100
92
const jDot = this . dots . array [ j ]
101
93
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
- // }
110
94
this . ctx . beginPath ( )
111
95
this . ctx . strokeStyle = this . averageColorStyles ( iDot , jDot )
112
96
this . ctx . moveTo ( iDot . x , iDot . y )
@@ -119,17 +103,17 @@ export default class ParticleLine {
119
103
}
120
104
121
105
drawDots ( ) {
122
- for ( let i = 0 ; i < this . dots . nb ; i ++ ) {
106
+ for ( let i = 0 ; i < this . dots . array . length ; i ++ ) {
123
107
const dot = this . dots . array [ i ]
124
108
dot . draw ( )
125
109
}
126
110
}
127
111
128
112
animateDots ( ) {
129
113
this . ctx . clearRect ( 0 , 0 , this . canvas . width , this . canvas . height )
130
- this . moveDots ( )
131
- this . connectDots ( )
132
114
this . drawDots ( )
115
+ this . connectDots ( )
116
+ this . moveDots ( )
133
117
requestAnimationFrame ( this . animateDots . bind ( this ) )
134
118
}
135
119
}
0 commit comments