Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="c"></canvas>
              
            
!

CSS

              
                body {
  overflow: hidden;
}

canvas {
  width: 100vw; height: 100vh;
}
              
            
!

JS

              
                console.clear()

class Utils {
  static randomRange(min, max) {
    return Math.random() * (max - min) + min
  }

  static mapRange (value, inputMin, inputMax, outputMin, outputMax, clamp) {
    if (Math.abs(inputMin - inputMax) < Number.EPSILON) {
      return outputMin;
    } else {
      var outVal = ((value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin);
      if (clamp) {
        if (outputMax < outputMin) {
          if (outVal < outputMax) outVal = outputMax;
          else if (outVal > outputMin) outVal = outputMin;
        } else {
          if (outVal > outputMax) outVal = outputMax;
          else if (outVal < outputMin) outVal = outputMin;
        }
      }
      return outVal;
    }
  }
}

Utils.simplex = new SimplexNoise(666)

class Particle {
  constructor(pos, color) {
    this.pos = pos
    this.pastPos = pos.clone()
    this.color = color
  }

  update(newPos, pastPos, newColor) {
    this.pastPos.copy(pastPos || this.pos)
    this.pos.copy(newPos)
    this.color = newColor

    return this
  }

  draw(ctx) {
    ctx.beginPath()
    ctx.moveTo(this.pastPos.x, this.pastPos.y)
    ctx.lineTo(this.pos.x, this.pos.y)
    ctx.strokeStyle = this.color
    ctx.stroke()

    return this
  }
}

class App {
  constructor() {
    this.config = {
      bgColor: chroma({ h: 230, s: 0.5, l: 0.92}).hex(),
      numOfParticles: 6000,
      nZoom: 180,
      particleLength: 10,
      speed: 0.5
    }
    
    this.canvas = document.getElementById('c')
    this.ctx = this.canvas.getContext('2d')

    this.shadowCanvas = document.createElement('canvas')
    this.shadowCtx = this.shadowCanvas.getContext('2d')

    this.setUpVars()
    this.setUpListeners()
    this.setUpGui()

    this.particles = this.getParticles()

    this.update()
  }

  getParticles() {
    const particles = []

    for (let i = 0; i < this.config.numOfParticles; i++) {
      particles.push(new Particle(
        new Victor(this.wWidth * Math.random(), this.wHeight * Math.random()),
        '#000'
      ))
    }

    return particles
  }

  setUpGui() {
    const pane = new Tweakpane()
    const folder = pane.addFolder({
      expanded: false,
      title: 'Settings',
    })
    folder.addInput(this.config, 'bgColor')
    folder.addInput(this.config, 'numOfParticles', {
      min: 10,
      max: 10000,
      step: 10
    }).on('change', () => {
      this.particles = this.getParticles()
    })

    folder.addInput(this.config, 'nZoom', {
      min: 10,
      max: 1000,
      step: 1
    })

    folder.addInput(this.config, 'particleLength', {
      min: 1,
      max: 50,
      step: 1
    })

    folder.addInput(this.config, 'speed', {
      min: 0.01,
      max: 1,
      step: 0.01
    })
  }

  setUpVars() {
    this.canvas.width = this.shadowCanvas.width = this.wWidth = window.innerWidth
    this.canvas.height = this.shadowCanvas.height = this.wHeight = window.innerHeight
    this.wCenterX = this.wWidth / 2
    this.wCenterY = this.wHeight / 2
    this.wHypot = Math.hypot(this.wWidth, this.wHeight)
    this.wMin = Math.min(this.wWidth, this.wHeight)

    this.particles = this.getParticles()
  }

  setUpListeners() {
    window.addEventListener('resize', this.setUpVars.bind(this))
  }

  isWithinBounds(x, y) {
    let answer = true

    if (x < 0 || x > this.wWidth || y < 0 || y > this.wHeight) {
      answer = false
    }

    return answer
  }

  draw(ctx) {
    ctx.save()
    ctx.globalAlpha = 0.1
    ctx.fillStyle = this.config.bgColor
    ctx.fillRect(0, 0, this.wWidth, this.wHeight)
    ctx.restore()

    ctx.save()
    ctx.globalAlpha = 0.1
    this.particles.forEach(p => p.draw(ctx))
    ctx.restore()
  }

  update(timestamp) {
    if (timestamp) {
      this.timestamp = timestamp / (5000 + ((1 - this.config.speed) * 15000))
      const nZoom = this.config.nZoom * (1 + (1 + Math.sin(this.timestamp) / 2))

      this.particles.forEach(p => {
        const noise = Utils.simplex.noise3D(p.pos.x/nZoom, p.pos.y/nZoom, this.timestamp)
        const angle = Math.PI * noise
        const angleSin = Math.sin(angle)
        const angleCos = Math.cos(angle)
        const newX = p.pos.x + (angleSin * this.config.particleLength)
        const newY = p.pos.y + (angleCos * this.config.particleLength)
        let newPos, pastPos, newColor

        newColor = 'hsl('+(150 + Math.floor(noise * 50))+', 50%, 50%)'

        if (!this.isWithinBounds(p.pos.x, p.pos.y)) {
          newPos = new Victor(this.wWidth * Math.random(), this.wHeight * Math.random())
          pastPos = new Victor(newPos.x, newPos.y)
        } else {
          newPos = new Victor(newX, newY)
        }

        p.update(newPos, pastPos, newColor)
      })

      this.draw(this.shadowCtx)
    }

    // this.ctx.fillStyle = this.config.bgColor
    this.ctx.clearRect(0, 0, this.wWidth, this.wHeight)
    this.ctx.drawImage(this.shadowCanvas, 0, 0)

    window.requestAnimationFrame(this.update.bind(this))
  }
}

new App()
              
            
!
999px

Console