cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div class="title">
    <h3>PLASM.it - 2017</h3>
    <h1>DISTANCES</h1>
    <h3>Connections and differences</h3>
  </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>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Montserrat:200,300,400,600')
.more-pens
  position: fixed
  left: 20px
  bottom: 20px
  z-index: 10
  font-family: "Montserrat"
  font-size: 12px

a.white-mode, a.white-mode:link, a.white-mode:visited, a.white-mode:active
  font-family: "Montserrat"
  font-size: 12px
  text-decoration: none
  background: #212121
  padding: 4px 8px
  color: #f7f7f7
  &:hover
    background: #edf3f8
    color: #212121

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


.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: 600
    font-size: 60px
    padding: 0
    margin: 0
    line-height: 1
    text-shadow: 0 0 30px #000155

    span
      font-weight: 600
      padding: 0
      margin: 0
      color: #BBB

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

            
          
!
            
              let max_particles    = 150;
let particles        = [];
let frequency        = 200;

// Popolate particles
popolate(max_particles);

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

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

class Particle{
  constructor(canvas){
    let colors = ["#E5493F","#55C1FF","#26B9A0", "#5A52FF"]
    // let colors = ["#feea00","#a9df85","#5dc0ad", "#ff9a00","#fa3f20"]
    let types  = ["full","fill","empty","square","square-in-circle"]
    this.random = Math.random()
    this.canvas = canvas;

    this.lineWidth = 0.2 + (2.8*this.random);

    this.speed = 2 + Math.random() * 3

    this.progress = 0;
    this.progress_inc = this.random > 0.5 ? this.random*(this.speed*1.5) : this.random*-(this.speed*1.5);

    this.vx = (Math.random() * this.speed) - (Math.random() * this.speed)
    this.vy = (Math.random() * this.speed) - (Math.random() * this.speed)
    this.radius = 10 + Math.round(Math.random() * 50)
    this.w = $(window).width()
    this.h = $(window).height()
    this.x = (this.w-this.radius)/2
    this.y = (this.h-this.radius)/2


    this.radius = 1 + (8*this.random)
    this.type  = types[this.randomIntFromInterval(0,types.length-1)];
    this.color = colors[this.randomIntFromInterval(0,colors.length-1)];
  }

  getCoordinates(){
    return {
      x: this.x,
      y: this.y
    }
  }

  randomIntFromInterval(min,max){
      return Math.floor(Math.random()*(max-min+1)+min);
  }

  render(){
    // Create arc
    let color = this.color;
    switch(this.type){
      case "square":
        this.drawRotatedRect(this.x, this.y, this.radius*2, this.radius*2, this.progress, color)
      break;
      case "square-in-circle":
        this.drawRotatedRect(this.x, this.y, this.radius*2, this.radius*2, this.progress, color)
        this.createArcEmpty(this.radius+this.lineWidth*3, this.lineWidth, color)
      break;
      case "full":
        this.createArcFill(this.radius, color)
        this.createArcEmpty(this.radius+this.lineWidth, this.lineWidth/2, color)
      break;
      case "fill":
        this.createArcFill(this.radius, color)
      break;
      case "empty":
        this.createArcEmpty(this.radius, this.lineWidth, color)
      break;
    }
  }

  createArcFill(radius, color){
    this.canvas.beginPath();
    this.canvas.arc(this.x, this.y, radius/1.5, 0, 2 * Math.PI);
    this.canvas.fillStyle = color;
    this.canvas.fill();
    this.canvas.closePath();
  }

  createArcEmpty(radius, lineWidth, color){
    this.canvas.beginPath();
    this.canvas.arc(this.x, this.y, radius, 0, 2 * Math.PI);
    this.canvas.lineWidth = lineWidth;
    this.canvas.strokeStyle = color;
    this.canvas.stroke();
    this.canvas.closePath();
  }

  drawRotatedRect(x,y,width,height,degrees, color){
    this.canvas.save();
    this.canvas.beginPath();
    this.canvas.translate( (x-this.radius)+width/2, (y-this.radius)+height/2 );
    this.canvas.rotate(degrees*Math.PI/180);
    this.canvas.rect( -(width/2), -(height/2), width,height);
    this.canvas.fillStyle=color;
    this.canvas.fill();
    this.canvas.restore();
  }


  move(){
    this.progress += this.progress_inc
    let diff = this.radius
    switch(this.type){
      case "full":
        diff = this.radius + this.lineWidth*2
      break;
      case "square-in-circle":
        diff = this.radius + this.lineWidth*3
      break;
      case "empty":
        diff = this.radius + this.lineWidth
      break;
    }

    this.x = this.x + this.vx
    this.y = this.y + this.vy

    if(this.x < diff || this.x > this.w - diff){
      this.vx = -this.vx
    }
    if(this.y < diff || this.y > this.h - diff){
      this.vy = -this.vy
    }
    this.render()
    return true
  }

  calculateDistance(v1, v2){
    let x = Math.abs(v1.x - v2.x);
    let y = Math.abs(v1.y - v2.y);
    return Math.sqrt((x * x) + (y * y));
  }
}

/*
 * Function to clear layer canvas
 * @num:number number of particles
 */
function popolate(num){
  for (var i = 0; i < num; i++) {
   setTimeout(
     function(x){
       return function(){
         // Add particle
         particles.push(new Particle(canvas))
       };
     }(i)
     ,frequency*i);
  }
  return particles.length
 }

function clear(){
  canvas.fillStyle='#101018';
  canvas.fillRect(0, 0, tela.width, tela.height);
}

function calculateDistance(v1, v2){
  let x = Math.abs(v1.x - v2.x);
  let y = Math.abs(v1.y - v2.y);
  return Math.sqrt((x * x) + (y * y));
}

function connection(){
  let old_element = null
  $.each(particles, function(i, element){
    if(i> 0 && (i%3==0 || i%7==0)){
      let box1 = old_element.getCoordinates()
      let box2 = element.getCoordinates()
      canvas.beginPath();
      canvas.moveTo(box1.x,box1.y);
      canvas.lineTo(box2.x,box2.y);
      canvas.lineWidth = 0.8;
      canvas.strokeStyle="#32323C";
      if(i%7==0){
        //canvas.strokeStyle="#3f47ff";
      }
      canvas.stroke();
      canvas.closePath();
    }

    old_element = element
  })
}

/*
 * Function to update particles in canvas
 */
function update(){
  clear();
  connection()
  particles = particles.filter(function(p) { return p.move() })
  requestAnimationFrame(update.bind(this))
}

update()

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console