Skip to content

Commit e86e097

Browse files
custom shape image (svg or png)
1 parent 85a93fb commit e86e097

File tree

1 file changed

+95
-14
lines changed

1 file changed

+95
-14
lines changed

particles.js

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function launchParticlesJS(tag_id, params){
1111
var canvas_el = document.querySelector('#'+tag_id+' > canvas');
1212

1313
/* particles.js variables with default values */
14-
pJS = {
14+
window.pJS = {
1515
canvas: {
1616
el: canvas_el,
1717
w: canvas_el.offsetWidth,
@@ -21,8 +21,11 @@ function launchParticlesJS(tag_id, params){
2121
color: '#fff',
2222
color_random: false,
2323
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
2629
},
2730
opacity: {
2831
opacity: 1,
@@ -240,6 +243,17 @@ function launchParticlesJS(tag_id, params){
240243
this.vx = -.5 + Math.random();
241244
this.vy = -.5 + Math.random();
242245

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+
243257
};
244258

245259
pJS.fn.particle.prototype.draw = function() {
@@ -283,6 +297,74 @@ function launchParticlesJS(tag_id, params){
283297
);
284298
break;
285299

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-9A-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+
286368
}
287369

288370
pJS.canvas.ctx.fill();
@@ -315,21 +397,20 @@ function launchParticlesJS(tag_id, params){
315397
}
316398
}
317399

400+
318401
/* 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+
319407
switch(pJS.interactivity.events.onresize.mode){
320408
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;
325413
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;
333414
}
334415

335416

0 commit comments

Comments
 (0)