@@ -93,7 +93,12 @@ var pJS = function(tag_id, params){
93
93
events : {
94
94
onhover : {
95
95
enable : true ,
96
- mode : 'grab'
96
+ mode : 'grab' ,
97
+ parallax : {
98
+ enable : true ,
99
+ force : 2 ,
100
+ smooth : 10
101
+ }
97
102
} ,
98
103
onclick : {
99
104
enable : true ,
@@ -261,6 +266,10 @@ var pJS = function(tag_id, params){
261
266
if ( this . y > pJS . canvas . h - this . radius * 2 ) this . y = this . y - this . radius ;
262
267
else if ( this . y < this . radius * 2 ) this . y = this . y + this . radius ;
263
268
269
+ /* parallax */
270
+ this . offsetX = 0 ;
271
+ this . offsetY = 0 ;
272
+
264
273
/* check position - avoid overlap */
265
274
if ( pJS . particles . move . bounce ) {
266
275
pJS . fn . vendors . checkOverlap ( this , position ) ;
@@ -423,10 +432,13 @@ var pJS = function(tag_id, params){
423
432
pJS . canvas . ctx . fillStyle = color_value ;
424
433
pJS . canvas . ctx . beginPath ( ) ;
425
434
435
+ var p_x = p . x + p . offsetX ,
436
+ p_y = p . y + p . offsetY ;
437
+
426
438
switch ( p . shape ) {
427
439
428
440
case 'circle' :
429
- pJS . canvas . ctx . arc ( p . x , p . y , radius , 0 , Math . PI * 2 , false ) ;
441
+ pJS . canvas . ctx . arc ( p_x , p_y , radius , 0 , Math . PI * 2 , false ) ;
430
442
break ;
431
443
432
444
case 'edge' :
@@ -526,6 +538,16 @@ var pJS = function(tag_id, params){
526
538
p . y += p . vy * ms ;
527
539
}
528
540
541
+ /* parallax */
542
+ if ( pJS . interactivity . mouse . pos_x && pJS . interactivity . events . onhover . parallax . enable ) {
543
+ /* smaller is the particle, longer is the offset distance */
544
+ var tmp_x = ( pJS . interactivity . mouse . pos_x - ( window . innerWidth / 2 ) ) / ( pJS . interactivity . events . onhover . parallax . force * p . radius ) ;
545
+ p . offsetX += ( tmp_x - p . offsetX ) / pJS . interactivity . events . onhover . parallax . smooth ; // Easing equation
546
+
547
+ var tmp_y = ( pJS . interactivity . mouse . pos_y - ( window . innerHeight / 2 ) ) / ( pJS . interactivity . events . onhover . parallax . force * p . radius ) ;
548
+ p . offsetY += ( tmp_y - p . offsetY ) / pJS . interactivity . events . onhover . parallax . smooth ; // Easing equation
549
+ }
550
+
529
551
/* change opacity status */
530
552
if ( pJS . particles . opacity . anim . enable ) {
531
553
if ( p . opacity_status == true ) {
@@ -560,37 +582,37 @@ var pJS = function(tag_id, params){
560
582
}
561
583
} else {
562
584
var new_pos = {
563
- x_left : - p . radius ,
564
- x_right : pJS . canvas . w + p . radius ,
565
- y_top : - p . radius ,
566
- y_bottom : pJS . canvas . h + p . radius
585
+ x_left : - p . radius - p . offsetX ,
586
+ x_right : pJS . canvas . w + p . radius + p . offsetX ,
587
+ y_top : - p . radius - p . offsetY ,
588
+ y_bottom : pJS . canvas . h + p . radius - p . offsetY
567
589
}
568
590
}
569
591
570
- if ( p . x - p . radius > pJS . canvas . w ) {
592
+ if ( ( p . x ) - p . radius > pJS . canvas . w - p . offsetX ) {
571
593
p . x = new_pos . x_left ;
572
594
p . y = Math . random ( ) * pJS . canvas . h ;
573
595
}
574
- else if ( p . x + p . radius < 0 ) {
596
+ else if ( ( p . x ) + p . radius < 0 - p . offsetX ) {
575
597
p . x = new_pos . x_right ;
576
598
p . y = Math . random ( ) * pJS . canvas . h ;
577
599
}
578
- if ( p . y - p . radius > pJS . canvas . h ) {
600
+ if ( ( p . y ) - p . radius > pJS . canvas . h - p . offsetY ) {
579
601
p . y = new_pos . y_top ;
580
602
p . x = Math . random ( ) * pJS . canvas . w ;
581
603
}
582
- else if ( p . y + p . radius < 0 ) {
604
+ else if ( ( p . y ) + p . radius < 0 - p . offsetY ) {
583
605
p . y = new_pos . y_bottom ;
584
606
p . x = Math . random ( ) * pJS . canvas . w ;
585
607
}
586
608
587
609
/* out of canvas modes */
588
610
switch ( pJS . particles . move . out_mode ) {
589
611
case 'bounce' :
590
- if ( p . x + p . radius > pJS . canvas . w ) p . vx = - p . vx ;
591
- else if ( p . x - p . radius < 0 ) p . vx = - p . vx ;
592
- if ( p . y + p . radius > pJS . canvas . h ) p . vy = - p . vy ;
593
- else if ( p . y - p . radius < 0 ) p . vy = - p . vy ;
612
+ if ( ( p . x + p . offsetX ) + p . radius > pJS . canvas . w ) p . vx = - p . vx ;
613
+ else if ( ( p . x + p . offsetX ) - p . radius < 0 ) p . vx = - p . vx ;
614
+ if ( ( p . y + p . offsetY ) + p . radius > pJS . canvas . h ) p . vy = - p . vy ;
615
+ else if ( ( p . y + p . offsetY ) - p . radius < 0 ) p . vy = - p . vy ;
594
616
break ;
595
617
}
596
618
@@ -676,8 +698,8 @@ var pJS = function(tag_id, params){
676
698
677
699
pJS . fn . interact . linkParticles = function ( p1 , p2 ) {
678
700
679
- var dx = p1 . x - p2 . x ,
680
- dy = p1 . y - p2 . y ,
701
+ var dx = ( p1 . x + p1 . offsetX ) - ( p2 . x + p2 . offsetX ) ,
702
+ dy = ( p1 . y + p1 . offsetY ) - ( p2 . y + p2 . offsetY ) ,
681
703
dist = Math . sqrt ( dx * dx + dy * dy ) ;
682
704
683
705
/* draw a line between p1 and p2 if the distance between them is under the config distance */
@@ -695,8 +717,8 @@ var pJS = function(tag_id, params){
695
717
696
718
/* path */
697
719
pJS . canvas . ctx . beginPath ( ) ;
698
- pJS . canvas . ctx . moveTo ( p1 . x , p1 . y ) ;
699
- pJS . canvas . ctx . lineTo ( p2 . x , p2 . y ) ;
720
+ pJS . canvas . ctx . moveTo ( ( p1 . x + p1 . offsetX ) , ( p1 . y + p1 . offsetY ) ) ;
721
+ pJS . canvas . ctx . lineTo ( ( p2 . x + p2 . offsetX ) , ( p2 . y + p2 . offsetY ) ) ;
700
722
pJS . canvas . ctx . stroke ( ) ;
701
723
pJS . canvas . ctx . closePath ( ) ;
702
724
@@ -792,8 +814,8 @@ var pJS = function(tag_id, params){
792
814
/* on hover event */
793
815
if ( pJS . interactivity . events . onhover . enable && isInArray ( 'bubble' , pJS . interactivity . events . onhover . mode ) ) {
794
816
795
- var dx_mouse = p . x - pJS . interactivity . mouse . pos_x ,
796
- dy_mouse = p . y - pJS . interactivity . mouse . pos_y ,
817
+ var dx_mouse = ( p . x + p . offsetX ) - pJS . interactivity . mouse . pos_x ,
818
+ dy_mouse = ( p . y + p . offsetY ) - pJS . interactivity . mouse . pos_y ,
797
819
dist_mouse = Math . sqrt ( dx_mouse * dx_mouse + dy_mouse * dy_mouse ) ,
798
820
ratio = 1 - dist_mouse / pJS . interactivity . modes . bubble . distance ;
799
821
@@ -1043,7 +1065,7 @@ var pJS = function(tag_id, params){
1043
1065
1044
1066
/* path */
1045
1067
pJS . canvas . ctx . beginPath ( ) ;
1046
- pJS . canvas . ctx . moveTo ( p . x , p . y ) ;
1068
+ pJS . canvas . ctx . moveTo ( p . x + p . offsetX , p . y + p . offsetY ) ;
1047
1069
pJS . canvas . ctx . lineTo ( pJS . interactivity . mouse . pos_x , pJS . interactivity . mouse . pos_y ) ;
1048
1070
pJS . canvas . ctx . stroke ( ) ;
1049
1071
pJS . canvas . ctx . closePath ( ) ;
@@ -1541,4 +1563,4 @@ window.particlesJS.load = function(tag_id, path_config_json, callback){
1541
1563
} ;
1542
1564
xhr . send ( ) ;
1543
1565
1544
- } ;
1566
+ } ;
0 commit comments