jquery - 将函数转换为变量

jquery - convert a function into a variable

我几乎没有为 particles.js 预定义的参数,这是一个创建动画背景的免费插件。用户可以更改动画颜色和不透明度。稍后我想使用 jsZip 将 particles.js 参数添加到一个 zip 文件中。

问题:我无法将 parameters/function 存储到 jsZip 的变量中。

这是我的 Particles.Js

示例代码
function particleBG2(particleColor, particleAlpha) {
  particlesJS("particles-js", {
    particles: {
      number: {
        value: 80,
        density: {
          enable: true,
          value_area: 800
        }
      },
      color: {
        value: particleColor
      },
      shape: {
        type: "circle",
        stroke: {
          width: 0
        },
        polygon: {
          nb_sides: 5
        },
        image: {
          src: "img/github.svg",
          width: 100,
          height: 100
        }
      },
      opacity: {
        value: particleAlpha,
        random: false,
        anim: {
          enable: false,
          speed: 1,
          opacity_min: 0.1,
          sync: false
        }
      },
      size: {
        value: 3,
        random: true,
        anim: {
          enable: false,
          speed: 40,
          size_min: 0.1,
          sync: false
        }
      },
      line_linked: {
        enable: true,
        distance: 150,
        color: particleColor,
        opacity: particleAlpha,
        width: 1
      },
      move: {
        enable: true,
        speed: 3,
        direction: "none",
        random: false,
        straight: false,
        out_mode: "out",
        bounce: false,
        attract: {
          enable: false,
          rotateX: 600,
          rotateY: 1200
        }
      }
    },
    interactivity: {
      detect_on: "canvas",
      events: {
        onhover: {
          enable: false,
          mode: "repulse"
        },
        onclick: {
          enable: false,
          mode: "push"
        },
        resize: true
      },
      modes: {
        grab: {
          distance: 400,
          line_linked: {
            opacity: 1
          }
        },
        bubble: {
          distance: 400,
          size: 40,
          duration: 2,
          opacity: 8,
          speed: 3
        },
        repulse: {
          distance: 200,
          duration: 0.4
        },
        push: {
          particles_nb: 4
        },
        remove: {
          particles_nb: 2
        }
      }
    },
    retina_detect: true
  });
}

如果我能以某种方式将所有这些存储在一个变量中,即 particlescript,那么我可以将它提供给 jsZip 以作为脚本文件添加。

var zip = new JSZip();
zip.file("script.js", particlescript);
zip.generateAsync({type:"blob"}).then(function(content) {
    saveAs(content, "project.zip");
});

您可以将脚本内容存储为字符串。将变量保留为占位符,并将它们替换为用户输入的值。

像这样:

var template = `function particleBG2() {
  var particleColor = '{particleColor}';
  var particleAlpha = '{particleAlpha}';
  particlesJS("particles-js", {
    particles: {
      number: {
        value: 80,
        density: {
          enable: true,
          value_area: 800
        }
      },
      color: {
        value: particleColor
      },
      shape: {
        type: "circle",
        stroke: {
          width: 0
        },
        polygon: {
          nb_sides: 5
        },
        image: {
          src: "img/github.svg",
          width: 100,
          height: 100
        }
      },
      opacity: {
        value: particleAlpha,
        random: false,
        anim: {
          enable: false,
          speed: 1,
          opacity_min: 0.1,
          sync: false
        }
      },
      size: {
        value: 3,
        random: true,
        anim: {
          enable: false,
          speed: 40,
          size_min: 0.1,
          sync: false
        }
      },
      line_linked: {
        enable: true,
        distance: 150,
        color: particleColor,
        opacity: particleAlpha,
        width: 1
      },
      move: {
        enable: true,
        speed: 3,
        direction: "none",
        random: false,
        straight: false,
        out_mode: "out",
        bounce: false,
        attract: {
          enable: false,
          rotateX: 600,
          rotateY: 1200
        }
      }
    },
    interactivity: {
      detect_on: "canvas",
      events: {
        onhover: {
          enable: false,
          mode: "repulse"
        },
        onclick: {
          enable: false,
          mode: "push"
        },
        resize: true
      },
      modes: {
        grab: {
          distance: 400,
          line_linked: {
            opacity: 1
          }
        },
        bubble: {
          distance: 400,
          size: 40,
          duration: 2,
          opacity: 8,
          speed: 3
        },
        repulse: {
          distance: 200,
          duration: 0.4
        },
        push: {
          particles_nb: 4
        },
        remove: {
          particles_nb: 2
        }
      }
    },
    retina_detect: true
  });
}`;


document.querySelector('button').addEventListener('click', function() {
  var particleAlpha = document.querySelector('#particleAlpha').value;
  var particleColor = document.querySelector('#particleColor').value;
  var particlescript = template.replace(/\{(.*)\}/g, function(a, b, c) {
    return eval(b);
  });
  console.log(particlescript);
  
  // var zip = new JSZip();
  // zip.file("script.js", particlescript);
  // zip.generateAsync({type:"blob"}).then(function(content) {
  //   saveAs(content, "project.zip");
  // });
});
<input id="particleColor" type="text" placeholder="particleColor" value="#ddd" /><br />
<input id="particleAlpha" type="text" placeholder="particleAlpha" value="0.5" /><br />
<button>Save</button>