微件:SaltFirework:修订间差异

添加1,243字节 、​ 2021年1月20日 (星期三)
添加了自定义烟花颜色色相,颜色波动范围,粒子数量三种自定义参数
(快过年了)
 
(添加了自定义烟花颜色色相,颜色波动范围,粒子数量三种自定义参数)
*/
(function () {
// 阻止重复调用
if (document.getElementById('saltFireWorkCanvas')) return
// 参数
const hueRange = [0, 360] // 颜色范围
const hueDiffhueRange = 30(function //() 颜色变化区间{
let defaultValue = (function () {
const count = 110 // 粒子效果数量
var x = []
for (let i = 1; i < 361; i++)
x.push(i)
return x
})()
let e = document.getElementById('saltForeworkHueRange')
if (!e) return defaultValue
let c = e.textContent.replace(/[\s\n_]+/g, '').replace(/[\;\/\|\/\\,;、\-]+/g, ',').split(',').map((v) => parseInt(v)).filter(Boolean).filter((v) => v > 0 && v < 361)
if (!c || c.length < 1 || c.length > 360) return defaultValue
return c
})()// [0, 360]
// 颜色变化区间
const hueDiff = (function () {
let e = document.getElementById('saltForeworkHueDiff')
if (!e) return 30
let c = parseInt(e.textContent)
if (isNaN(c) || c < 0 || c > 180) return 30
return c
})()
const count = 110 // 粒子效果数量
const count = (function () {
let e = document.getElementById('saltForeworkCount')
if (!e) return 110
let c = parseInt(e.textContent)
if (isNaN(c) || c < 1 || c > 500) return 110
return c
})()
const baseRange = [1, 4] // 粒子大小
const speedMutiply = 6 // 粒子速度范围
clearCanvas()
tick() // 开始tick
document.addEventListener('mousedown', function (e) => { createFireworks(e.clientX, e.clientY) }) // 监听事件
}
/**清理绘图区 */
function createFireworks(x, y) {
/**这个烟花的颜色 */
let hue = Math.floor(Math.random() * randomChoice(hueRange[1] - hueRange[0])) + hueRange[0]
for (let i = 0; i < count; i++) {
let spd = Math.random() * speedMutiply + baseSpeed
clearParticles()
requestAnimationFrame(tick)
}
/**随机选择 */
function randomChoice(arr) {
if (arr.length < 1) {
return null;
}
return arr[Math.floor(Math.random() * arr.length)];
}
})()