xxxxxxxxxx
185
let continuous = true
let initial_fireworks = 5
let fireworks = []
let num_of_stars = 100
let stars = []
let shootingStars = []
let catherineWheel = false
let catherineWheels = []
let fireworkSound
let seconds = 3
function setup() {
createCanvas(1000,500);//windowWidth, windowHeight);
generateStars()
for (let i = 0; i < initial_fireworks; i++){
generateFirework(i)
}
if (catherineWheel){
generateCatherineWheel()
}
//frameRate(5)
}
function preload() {
fireworkSound = loadSound("firework.mp3")
}
function draw() {
background(0);
if (!continuous && frameCount % 150 == 0) {
for (let i = 0; i < initial_fireworks; i++){
generateFirework(i)
}
} else if (continuous && frameCount % 12 == 0) {
generateFirework(0)
}
for (let i = 0; i < fireworks.length ; i++) {
fireworks[i].update()
if (!fireworks[i].completed){
fireworks[i].draw()
}
}
for (let i = 0; i < num_of_stars; i++){
stars[i].update()
stars[i].draw()
}
if (shootingStars.length == 0 && random(0, 1) < 0.01){
generateShootingStars()
}
if (shootingStars.length > 0){
for (let i = 0; i < shootingStars.length; i++){
shootingStars[i].update()
shootingStars[i].draw()
if (shootingStars[i].position.x < 0 || shootingStars[i].position.x > width) {
shootingStars[i].trailAlpha -= shootingStars[i].trailAlphaLoss
if (shootingStars[i].trailAlpha <= 0) {
shootingStars = []
}
}
}
}
if (catherineWheels.length > 0){
for (let i = 0; i < catherineWheels.length; i++){
catherineWheels[i].update()
catherineWheels[i].draw()
}
}
//save(`${nf(frameCount, 5)}.png`)
}
function generateFirework(i){
let x = random(0, width)
let y = height
let theta = random(1.4 * PI, 1.6 * PI)
let speed = random(height/(frameRate() * 2), height/(frameRate() * 1))
let y_vel = abs(speed*sin(theta))
let g = 0.0981
let t = random((height/2)/y_vel, height/y_vel)
let decay_levels = random([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3])
let decay_products = random(10, 25)
let isChild = false
//colorMode(RGB)
//let colour = createVector(random(0, 255), random(0, 255), random(0, 255))
colorMode(HSB, 255)
let colour = createVector(random(0, 255), 255, 255)
let initialVelocity = createVector(0, 0)
fireworks.push(new Firework(str(i), x, y, speed, theta , g, t, decay_levels, decay_products, isChild, colour, initialVelocity, random(0, 1)))
if (fireworks.length > initial_fireworks * 5){
fireworks.slice(initial_fireworks - 1)
}
}
function generateStars(){
for (let i = 0; i < num_of_stars; i++){
let x = random(0, width)
let y = random(0, height)
let maxBrightness = random(0, 255)
let minBrightness = random(0, maxBrightness)
let twinklePeriod = random(360, 720)
let startingPeriod = random(0, twinklePeriod)
let size_ = random(1, 3)
stars.push(new Star(x, y, minBrightness, maxBrightness, startingPeriod, twinklePeriod, size_))
}
}
function generateShootingStars() {
let x = random([0, width])
let y = random(0, height/2)
let v = random(width/(frameRate() * 30), width/(frameRate() * 5))
let theta = PI
if (x == 0){
theta = random(0.975 * 2*PI, 1.025 * 2*PI)
} else {
theta = random(0.975 * PI, 1.025 * PI)
}
let trailLength = 1//random(0, width) / v
let trailAlpha = random(28, 128)
let trailAlphaLoss = trailAlpha / (width/v)
let size_ = 1
shootingStars.push(new ShootingStar(x, y, v, theta, trailLength, trailAlpha, trailAlphaLoss, size_))
//print(shootingStars)
}
function generateCatherineWheel() {
let r = random(50, 100)
let x = random(r, width - r)
let y = random(height * 5/6, height - r) //random(0, height)
let T = random(5, 10)
let ch = 1//random([-1, 1])
let np = random(1, 10)
let c = createVector(random(0, 255), 255, 255)
let s = random(5, 10)
catherineWheels.push(new CatherineWheel(x, y, r, T, ch, np, c, s))
}
function keyPressed() {
if (key === 's') {
saveGif('GIF', 300);
}
}