javascript - jquery - 将函数转换为变量
问题描述
我有几个用于创建动画背景的免费插件particles.js的预定义参数。用户可以更改动画颜色和不透明度。稍后我想使用 jsZip 将particle.js 参数添加到 zip 文件中。
问题:我无法将参数/函数存储到 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>
推荐阅读
- github - 从 bitbucket 克隆 repo 时出现 Google Cloud Platform 错误
- c# - 如何在 XamarinForms 中获取设备时间格式
- ios - 屏幕关闭/设备在 iOS 中锁定时计时器不运行
- imagemagick - ImageMagick 在玻璃上的雕刻效果
- php - 如何用特殊符号(。)替换字符串中的单词?
- java - 自定义标记位图错误 无法解码图像。提供的图像必须是位图
- vba - 作为计划任务运行时从 ms 访问保存图形图像
- unit-testing - 在 Visual Studio 和 MSTest 中使用测试资源管理器执行自动化测试的问题
- java - 如何在 Map 中查找键的重复值
- intel-pin - 如何在指定时间后终止 PINTool