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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <!-- Codepen -->
  <div class="content">
    <div class="title">
      <h1>WORMS</h1>
      <h3>FUTURISTIC<span>PARTICLES</span></h3>
    </div>
  </div>
  <div class="more-pens">
    <a target="_blank" href="https://codepen.io/plasm/" class="white-mode">VIEW OTHER PENS</a>
    <a target="_blank" href="https://codepen.io/collection/nZpPbz/" class="white-mode">VIEW OTHER PARTICLES</a>
  </div>
  <!-- END Codepen -->
              
            
!

CSS

              
                =transform($prop)
  -webkit-transform: $prop
  -moz-transform: $prop
  -ms-transform: $prop
  -o-transform: $prop
  transform: $prop


@import url('https://fonts.googleapis.com/css?family=Catamaran:300,400,500,600,700')
.more-pens
  position: fixed
  right: 20px
  bottom: 20px
  z-index: 10
  font-family: 'Catamaran', sans-serif
  font-size: 12px

a.white-mode, a.white-mode:link, a.white-mode:visited, a.white-mode:active
  font-family: 'Catamaran', sans-serif
  font-size: 12px
  text-decoration: none
  background: #212121
  padding: 8px 18px
  color: #f7f7f7
  border-radius: 20px
  margin: 0 4px

  &:hover
    background: #edf3f8
    color: #212121

body
  margin: 0
  padding: 0
  overflow: hidden
  width: 100%
  height: 100%
  background: #000000

.content
  z-index: 10
  position: absolute
  left: 0
  top: 0
  padding: 0
  text-align: center
  width: 100vw
  height: 100vh


.title
  z-index: 10
  position: absolute
  left: 50%
  top: 50%
  transform: translateX(-50%) translateY(-50%)
  font-family: "Montserrat"
  text-align: center
  width: 100%
  h1
    position: relative
    color: #EEEEEE
    font-weight: 800
    font-size: 90px
    padding: 0
    margin: 0
    line-height: 1
    text-shadow: 0 0 30px #000155
  

  h3
    font-weight: 200
    font-size: 30px
    padding: 0
    margin: 0
    line-height: 1
    color: #EEEEEE
    letter-spacing: 2px
    text-shadow: 0 0 30px #000155
    span
      font-weight: 700

              
            
!

JS

              
                class Particles{
  static initClass() {
    this.prototype.defaults = {
      speed: 1.8,
      radius: 25,
      innerColor: "#444444",
      outerColor: "#111111",
      aroundColor: "#999999",
      innerColorHero: "#CBF6FC",
      outerColorHero: "#9CECFA",
      aroundColorHero: "#9CECFA",
      aroundme: false,
      speedAroudme: 3,
      offsetAroudme: 0,
      hero: false,
      onCreate: function(){},
      onDead: function(){}
    };
  }

  constructor(canvas, options){
    Particles.initClass();
    //
    this.options  = $.extend({}, this.defaults, options );


    let timeout,
        random,
        startAngle,
        progress,
        direction,
        currentRadius,
        oldx,
        oldy,
        x,
        y,
        w,
        h;

    // Set default value
    this.canvas     = canvas;
    this.timeout    = false;
    this.random     = Math.random();
    this.startAngle = this.random * 360;
    this.w          = 0;
    this.h          = 0;
    this.progress   = 0;
    this.direction  = 0;
    this.currentRadius = 0;

    if( this.options.hero )
      this.options.offsetAroudme = 20

    // Init dimensions
    this.getWindowDimensions();

    // Initial position
    this.x = Math.floor(Math.random() * this.w);
    this.y = Math.floor(Math.random() * this.h);

    // init Behaviours
    this.behaviours();
    this.options.onCreate(this.options);
  }

  behaviours(){
    // window.resize event listener
    window.addEventListener('resize', function() {
      this.getWindowDimensions()
    }.bind(this));
  }

  render(){
    // this.getWindowDimensions();
    this.drawFirstParticle();
    this.drawSecondParticleAroundMe();
    this.nextPosition();
    this.progress++;
  }

  drawFirstParticle(){
    // Set current dimension radius
    this.currentRadius = Math.min(this.progress/2, this.options.radius);
    // GRADIENT
    let grd = this.canvas.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.currentRadius);
    if(this.options.hero == true){
      grd.addColorStop(0, this.options.innerColorHero);
      grd.addColorStop(1, this.options.outerColorHero);
    }else{
      grd.addColorStop(0, this.options.innerColor);
      grd.addColorStop(1, this.options.outerColor);
    }

    // Draw first circle
    this.canvas.beginPath();
    this.canvas.arc(this.x, this.y, this.currentRadius, 0, 2 * Math.PI);
    this.canvas.lineWidth = 1;
    this.canvas.fillStyle = grd;
    this.canvas.fill();
    this.canvas.closePath();
    return;
  }
  drawSecondParticleAroundMe(){
    if( !this.options.aroundme ) return;

    let color = this.options.hero == true ? this.options.aroundColorHero : this.options.aroundColor;;
    let angle = (this.progress*this.options.speedAroudme + this.startAngle) * Math.PI / 180;
    let x = this.x + Math.cos(angle)*(this.currentRadius-1 + this.options.offsetAroudme);
    let y = this.y + Math.sin(angle)*(this.currentRadius-1 + this.options.offsetAroudme);
    // this.canvas.beginPath();
    // this.canvas.arc(x, y, 1, 0, 2 * Math.PI);
    // this.canvas.lineWidth = 1;
    // this.canvas.fillStyle = color;
    // this.canvas.fill();
    // this.canvas.closePath();

    if(this.oldx){
      this.canvas.beginPath();
      this.canvas.moveTo(this.oldx, this.oldy);
      this.canvas.lineTo(x, y);
      this.canvas.strokeStyle = color;
      this.canvas.lineWidth = 1;
      this.canvas.stroke();
    }

    this.oldx = x;
    this.oldy = y;

    return;
  }

  nextPosition(){
    this.x += Math.cos(this.direction ) * this.options.speed;
    this.y += Math.sin(this.direction ) * this.options.speed;
    this.direction  += Math.random() * 0.7 - 0.32;
    return;
  }

  move(){
    this.render()
    return this.isInBound();
  }

  isInBound(){
    // console.log(this.w)
    if(
        this.x < -this.options.radius ||
        this.x > this.w + this.options.radius ||
        this.y < -this.options.radius ||
        this.y > this.h + this.options.radius
      ){
      this.options.onDead(this.options);
      return false
    }
    return true
  }

  getWindowDimensions(){
    this.w = window.innerWidth;
    this.h = window.innerHeight;
  }
}

var tela = document.createElement('canvas');
    $("body").append(tela);

    let canvas = tela.getContext('2d');

// ---------------------------

let config = {
    maxParticles: 100,
    maxHeros: 24,
    speedAroudme: 3,
    offsetAroudme: 1
  }

let dimensions = {
  width: 0,
  height: 0
}

let heros = 0;
let data_particles =[];

function setWindowSize(){
  dimensions.width  = $(window).width();
  dimensions.height = $(window).height();
  tela.width = dimensions.width;
  tela.height = dimensions.height;
}

function popolate(num){
  for (var i = 0; i < num; i++) {
    let isHero = false;
    if(heros < config.maxHeros){
      isHero = true;
    }
    data_particles.push(new Particles(canvas, {
      speed: .8,
      radius: Math.round(18 + (Math.random() * 40)),
      aroundme: true,
      hero: isHero,
      speedAroudme: config.speedAroudme,
      offsetAroudme: config.offsetAroudme,
      onCreate: function(data){
        if( data.hero == true )
          heros++;
      },
      onDead: function(data){
        if( data.hero == true )
          heros--;
      }
    }))
  }
  return data_particles.length
}

  function clear(){
    canvas.globalAlpha = 0.018;
    canvas.fillStyle = '#181719';
    canvas.fillRect(0, 0, dimensions.width, dimensions.height);
    canvas.globalAlpha = 1;
  }

  function update(){
    clear();
    data_particles = data_particles.filter(function(p) {
      return p.move()
    })
    if(data_particles.length < config.maxParticles){
      popolate(1)
    }
    requestAnimationFrame(update)
  }


setWindowSize()
popolate(config.maxParticles)
update()

$(window).resize(setWindowSize)

              
            
!
999px

Console