@@ -11,7 +11,7 @@ function launchParticlesJS(tag_id, params){
11
11
var canvas_el = document . querySelector ( '#' + tag_id + ' > canvas' ) ;
12
12
13
13
/* particles.js variables with default values */
14
- pJS = {
14
+ window . pJS = {
15
15
canvas : {
16
16
el : canvas_el ,
17
17
w : canvas_el . offsetWidth ,
@@ -21,8 +21,11 @@ function launchParticlesJS(tag_id, params){
21
21
color : '#fff' ,
22
22
color_random : false ,
23
23
shape : {
24
- type : 'circle' , // "circle", "edge", "triangle", "polygon" or "star"
25
- nb_sides : 5
24
+ type : 'circle' , // "circle", "edge", "triangle", "polygon", "star" or "image"
25
+ nb_sides : 5 , // 5+
26
+ img_src : 'http://f.cl.ly/items/3s2O2E0c3L3x2Q3J1y1Q/github.svg' ,
27
+ img_width : 100 ,
28
+ img_height : 100
26
29
} ,
27
30
opacity : {
28
31
opacity : 1 ,
@@ -240,6 +243,17 @@ function launchParticlesJS(tag_id, params){
240
243
this . vx = - .5 + Math . random ( ) ;
241
244
this . vy = - .5 + Math . random ( ) ;
242
245
246
+ /* if shape is image */
247
+ if ( pJS . particles . shape . type == 'image' ) {
248
+ var sh = pJS . particles . shape ;
249
+ this . img = {
250
+ src : sh . img_src ,
251
+ ratio : sh . img_width / sh . img_height ,
252
+ type : sh . img_src . substr ( sh . img_src . length - 3 )
253
+ }
254
+ if ( ! this . img . ratio ) this . img . ratio = 1 ;
255
+ }
256
+
243
257
} ;
244
258
245
259
pJS . fn . particle . prototype . draw = function ( ) {
@@ -283,6 +297,74 @@ function launchParticlesJS(tag_id, params){
283
297
) ;
284
298
break ;
285
299
300
+ case 'image' :
301
+
302
+ var t = this ;
303
+
304
+ if ( t . img . obj ) {
305
+ drawImg ( ) ;
306
+ } else {
307
+ createImgObj ( t . img . type ) ;
308
+ }
309
+
310
+ function drawImg ( ) {
311
+ pJS . canvas . ctx . drawImage (
312
+ t . img . obj ,
313
+ t . x - t . radius ,
314
+ t . y - t . radius ,
315
+ t . radius * 2 ,
316
+ t . radius * 2 / t . img . ratio
317
+ ) ;
318
+ }
319
+
320
+ function createImgObj ( img_type ) {
321
+
322
+ // SVG
323
+
324
+ if ( img_type == 'svg' ) {
325
+
326
+ var xhr = new XMLHttpRequest ( ) ;
327
+ xhr . open ( 'GET' , t . img . src ) ;
328
+ xhr . onreadystatechange = function ( data ) {
329
+
330
+ var svgXml = data . currentTarget . response ,
331
+ rgbHex = / # ( [ 0 - 9 A - F ] { 3 , 6 } ) / gi,
332
+ coloredSvgXml = svgXml . replace ( rgbHex , function ( m , r , g , b ) {
333
+ return 'rgb(' + t . color . r + ','
334
+ + t . color . g + ','
335
+ + t . color . b + ')' ;
336
+ } ) ;
337
+
338
+ var svg = new Blob ( [ coloredSvgXml ] , { type : 'image/svg+xml;charset=utf-8' } ) ,
339
+ DOMURL = window . URL || window . webkitURL || window ,
340
+ url = DOMURL . createObjectURL ( svg ) ;
341
+
342
+ var img = new Image ( ) ;
343
+ img . onload = function ( ) {
344
+ t . img . obj = img ;
345
+ DOMURL . revokeObjectURL ( url ) ;
346
+ }
347
+ img . src = url ;
348
+
349
+ }
350
+ xhr . send ( ) ;
351
+
352
+ }
353
+
354
+ // PNG
355
+
356
+ else if ( img_type == 'png' ) {
357
+ var img = new Image ( ) ;
358
+ img . onload = function ( ) {
359
+ t . img . obj = img ;
360
+ }
361
+ img . src = t . img . src ;
362
+ }
363
+
364
+ }
365
+
366
+ break ;
367
+
286
368
}
287
369
288
370
pJS . canvas . ctx . fill ( ) ;
@@ -315,21 +397,20 @@ function launchParticlesJS(tag_id, params){
315
397
}
316
398
}
317
399
400
+
318
401
/* change particle position if it is out of canvas */
402
+ if ( p . x - p . radius > pJS . canvas . w ) p . x = p . radius ;
403
+ else if ( p . x + p . radius < 0 ) p . x = pJS . canvas . w + p . radius ;
404
+ if ( p . y - p . radius > pJS . canvas . h ) p . y = p . radius ;
405
+ else if ( p . y + p . radius < 0 ) p . y = pJS . canvas . h + p . radius ;
406
+
319
407
switch ( pJS . interactivity . events . onresize . mode ) {
320
408
case 'bounce' :
321
- if ( p . x - p . radius > pJS . canvas . w ) p . vx = - p . vx ;
322
- else if ( p . x + p . radius < 0 ) p . vx = - p . vx ;
323
- if ( p . y - p . radius > pJS . canvas . h ) p . vy = - p . vy ;
324
- else if ( p . y + p . radius < 0 ) p . vy = - p . vy ;
409
+ if ( p . x + p . radius > pJS . canvas . w ) p . vx = - p . vx ;
410
+ else if ( p . x - p . radius < 0 ) p . vx = - p . vx ;
411
+ if ( p . y + p . radius > pJS . canvas . h ) p . vy = - p . vy ;
412
+ else if ( p . y - p . radius < 0 ) p . vy = - p . vy ;
325
413
break ;
326
-
327
- case 'out' :
328
- if ( p . x - p . radius > pJS . canvas . w ) p . x = p . radius ;
329
- else if ( p . x + p . radius < 0 ) p . x = pJS . canvas . w + p . radius ;
330
- if ( p . y - p . radius > pJS . canvas . h ) p . y = p . radius ;
331
- else if ( p . y + p . radius < 0 ) p . y = pJS . canvas . h + p . radius ;
332
- break ;
333
414
}
334
415
335
416
0 commit comments