在Particles.js中将多个图像作为形状

时间:2015-08-11 10:25:41

标签: javascript particles particle-system

我使用Particles.js(请参阅此处:https://github.com/VincentGarreau/particles.js/http://vincentgarreau.com/particles.js/)进行设计项目。

是否可以将多个图像作为'形状',不同' image.src'?如果是这样,我该如何做呢?

由于

1 个答案:

答案 0 :(得分:2)

是的,这是可能的。请参阅https://www.josh.ai/(这不是我的网站,但它可以满足您的需求)。

查看源代码,但更具体地说,我认为他修改了particle.js文件:https://www.josh.ai/static/js/particles.min.js

看一下代码的这一部分:

switch (i.canvas.ctx.fillStyle = n, i.canvas.ctx.beginPath(), a.shape) {
            case "circle":
                i.canvas.ctx.arc(a.x, a.y, t, 0, 2 * Math.PI, !1);
                break;
            case "edge":
                i.canvas.ctx.rect(a.x - t, a.y - t, 2 * t, 2 * t);
                break;
            case "triangle":
                i.fn.vendors.drawShape(i.canvas.ctx, a.x - t, a.y + t / 1.66, 2 * t, 3, 2);
                break;
            case "polygon":
                i.fn.vendors.drawShape(i.canvas.ctx, a.x - t / (i.particles.shape.polygon.nb_sides / 3.5), a.y - t / .76, 2.66 * t / (i.particles.shape.polygon.nb_sides / 3), i.particles.shape.polygon.nb_sides, 1);
                break;
            case "star":
                i.fn.vendors.drawShape(i.canvas.ctx, a.x - 2 * t / (i.particles.shape.polygon.nb_sides / 4), a.y - t / 1.52, 2 * t * 2.66 / (i.particles.shape.polygon.nb_sides / 3), i.particles.shape.polygon.nb_sides, 2);
                break;
            case "image":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                r && e()
                break;
            case "image2":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                var element = document.createElement('img');
                element.src = 'https://s3.amazonaws.com/josh.ai/hex2a.png';
                r = element;
                r && e()
                break;
            case "image3":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                var element = document.createElement('img');
                element.src = 'https://s3.amazonaws.com/josh.ai/hex3a.png';
                r = element;
                r && e()
                break;
             case "image4":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                var element = document.createElement('img');
                element.src = 'https://s3.amazonaws.com/josh.ai/hex4a.png';
                r = element;
                r && e()   
                break;
            case "image5":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                var element = document.createElement('img');
                element.src = 'https://s3.amazonaws.com/josh.ai/hex5a.png';
                r = element;
                r && e()
                break;
            case "image6":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                var element = document.createElement('img');
                element.src = 'https://s3.amazonaws.com/josh.ai/hex6a.png';
                r = element;
                r && e()
                break;
             case "image7":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                var element = document.createElement('img');
                element.src = 'https://s3.amazonaws.com/josh.ai/hex7a.png';
                r = element;
                r && e() 
                break;
            case "image8":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                var element = document.createElement('img');
                element.src = 'https://s3.amazonaws.com/josh.ai/hex8a.png';
                r = element;
                r && e()
                break;
            case "image9":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                var element = document.createElement('img');
                element.src = 'https://s3.amazonaws.com/josh.ai/hex9a.png';
                r = element;
                r && e()
                break;
             case "image10":
                if ("svg" == i.tmp.img_type) var r = a.img.obj;
                else var r = i.tmp.img_obj;
                var element = document.createElement('img');
                element.src = 'https://s3.amazonaws.com/josh.ai/hex10a.png';
                r = element;
                r && e() 
        }

image1,image2等的每个案例都链接到不同的png文件。如果您仍然感兴趣的话,我认为看那个人的代码可以让你到处找。