我正在尝试使用particle.js with multiple images,但是我不想每个图像都显示一次。然后我尝试记录该数组以了解但我不太了解它的加载方式... 这是我尝试更改的代码的原始部分:
/* if shape is image */
var shape_type = pJS.particles.shape.type;
if(typeof(shape_type) == 'object'){
if(shape_type instanceof Array){
var shape_selected = shape_type[Math.floor(Math.random() * shape_type.length)];
this.shape = shape_selected;
}
}else{
this.shape = shape_type;
}
var substring = this.shape.substring(0, 5);
if(substring == 'image'){
var sh = pJS.particles.shape;
this.img = {
src: sh.image.src,
ratio: sh.image.width / sh.image.height
}
if(!this.img.ratio) this.img.ratio = 1;
if(pJS.tmp.img_type == 'svg' && pJS.tmp.source_svg != undefined){
pJS.fn.vendors.createSvgImg(this);
if(pJS.tmp.pushing){
this.img.loaded = false;
}
}
}
但是它加载了我的数组的四个随机图像,然后我希望每个图像一次,然后我尝试了...:
/* if shape is image */
var shape_type = pJS.particles.shape.type;
if(typeof(shape_type) == 'object'){
if(shape_type instanceof Array){
for (let index = 0; index < shape_type.length; index++) {
console.log("test:"+shape_type[index]);
var shape_selected = shape_type[index];
this.shape = shape_type[index];
}
console.log("_shape:"+this.shape);
}
}else{
this.shape = shape_type;
}
var substring = this.shape.substring(0, 5);
if(substring == 'image'){
var sh = pJS.particles.shape;
this.img = {
src: sh.image.src,
ratio: sh.image.width / sh.image.height
}
if(!this.img.ratio) this.img.ratio = 1;
if(pJS.tmp.img_type == 'svg' && pJS.tmp.source_svg != undefined){
pJS.fn.vendors.createSvgImg(this);
if(pJS.tmp.pushing){
this.img.loaded = false;
}
}
}
在控制台中,我有:
test:image1
test:image2
test:image3
test:image4
_shape:image4
test:image1
test:image2
test:image3
test:image4
_shape:image4
test:image1
test:image2
test:image3
test:image4
_shape:image4
test:image1
test:image2
test:image3
test:image4
_shape:image4
我在加载方式上确实错了,但是我无法解释为什么...