const t=["#ff0000","#ff9b1e","#ffff1e","#00ff00","#0000ff","#641eff","#c81eff"],i=(t,i)=>Math.random()*(i-t)+t;let h=[],e=t[0];const n=(t,s)=>({x:t,y:s,xM:0,yM:0,c:e,d(){const t=window.c;t.beginPath(),t.fillStyle=this.c,t.arc(this.x,this.y,6,0,2*Math.PI),t.fill()},cl(){const t=n(this.x,this.y);return t.xM=Math.random()<.5?i(5,10):i(-10,-5),t.yM=i(-45,-35),t},m(){7+this.y+this.yM<window.a.height?(this.yM++,this.y+=this.yM):(h=h.filter(t=>t!==this)).push(this.cl(),this.cl()),this.x+this.xM<=window.a.width&&this.x+this.xM>=0?this.x+=this.xM:(this.c=e,this.xM=-this.xM)}}),s=()=>{window.c.clearRect(0,0,window.a.width,window.a.height),window.c.fillStyle="rgb(0,0,0)",window.c.fillRect(0,0,window.a.width,window.a.height),h.length>4e3&&(h=h.slice(h.length-4e3,h.length)),h.forEach(t=>{t.m(),t.d()}),window.requestAnimationFrame(s)};window.addEventListener("load",()=>{s(),setInterval(()=>{const i=t.indexOf(e);e=i===t.length-1?t[0]:t[i+1]},700)}),window.addEventListener("click",t=>{h.push(n(t.clientX,t.clientY))});
          const colors = [
        "#ff0000",
        "#ff9b1e",
        "#ffff1e",
        "#00ff00",
        "#0000ff",
        "#641eff",
        "#c81eff"
      ]
    , getRandom = (min, max) => Math.random() * (max - min) + min
let animatedObjects = []
  , currentColor = colors[0]
const Circle = (x, y) => {
  return {
    x,
    y,
    xM: 0,
    yM: 0,
    c: currentColor,
    d() {
      const ctx = window.c
      ctx.beginPath()
      ctx.fillStyle = this.c
      ctx.arc(this.x, this.y, 6, 0, Math.PI * 2)
      ctx.fill()
    },
    cl() {
      const newCircle = Circle(this.x, this.y)
      newCircle.xM = Math.random() < 0.5 ? getRandom(5, 10) : getRandom(-10, -5)
      newCircle.yM = getRandom(-45, -35)
      return newCircle
    },
    m() {
      if (7 + this.y + this.yM < window.a.height) { // Falling
        this.yM++
        this.y += this.yM
      } else { // Hit the ground
        animatedObjects = animatedObjects.filter(x => x !== this)
        animatedObjects.push(this.cl(), this.cl())
      }
      // X Motion
      if (this.x + this.xM <= window.a.width && this.x + this.xM >= 0) {
        this.x += this.xM
      } else {
        this.c = currentColor
        this.xM = -this.xM
      }
    }
  };
};
const render = () => {
  window.c.clearRect(0, 0, window.a.width, window.a.height)
  window.c.fillStyle = "rgb(0,0,0)"
  window.c.fillRect(0, 0, window.a.width, window.a.height)
  if (animatedObjects.length > 4000) {
    animatedObjects = 
      animatedObjects.slice(animatedObjects.length - 4000, animatedObjects.length)
  }
  animatedObjects
    .forEach(obj => {
      obj.m()
      obj.d()
    })
  
  window.requestAnimationFrame(render)
}
window.addEventListener("load", () => {
  render()
  setInterval(() => {
    const currentIndex = colors.indexOf(currentColor)
    currentColor = currentIndex === colors.length - 1
                 ? colors[0]
                 : colors[currentIndex + 1]; 
  }, 700)
})
window.addEventListener("click", e => {
  animatedObjects.push(Circle(e.clientX, e.clientY))
})