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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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="canvas"></canvas>
<button type="button" id="action" onClick="init(true)">
  Watch From Beginning
</button>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Lato:400,100,300);
  body {
    background: RGB(103, 54, 45);
    font-family: 'Lato', sans-serif;
    font-weight: 100;
  }
  canvas {
    top: 7.5px;
    left: 7.5px;
    position: fixed;
    width: calc(100% - 15px);
    height: calc(100% - 15px);
  }
  button {
    position: fixed;
    margin-left: -90px;
    bottom: 15px;
    left: 50%;
    margin-top: 90px;
    width: 180px;
    height: 30px;
    line-height: 20px;
    background: #111;
    border: 1px solid #000;
    color: #fff;
    font-weight: 300;
    outline: none;
    display: inline-block;
    font-family: inherit;
    cursor: hand;
    cursor: pointer;
  }
  button:hover {
    background: #a00;
    color: #fff;
  }
              
            
!

JS

              
                var previewIterations = 0;
var spriteTotal = 1500;
var snapped = false;
  var canvas = document.getElementById('canvas');
  var ctx = canvas.getContext('2d');
  var canvasWidth = 0;
  var canvasHeight = 0;
  var iterations = 0;

  // ==========================
  // Sprite Class
  // ==========================

  window.onresize = updateCanvasSize();

  function Sprite(obj) {
    var property;
    Sprite.total++;
    this.index = Sprite.total;
    this.x = 0;
    this.y = 0;
    this.xStart = 0;
    this.yStart = 0;
    this.flag = 1;
    this.xOffset = 0;
    this.yOffset = 0;
    this.xdir = 1;
    this.ydir = 1;
    this.width = 10;
    this.height = 10;
    this.color = "0,0,0";
    this.alpha = 1;
    this.display = "square";
    this.circle = {
      centerX: 0,
      centerY: 0,
      radius: 0
    },
    this.type = {
      "orb": false,
      "colorPulse": false,
      "backToStart": false,
      "pong": false
    };
    this.deg = 0;
    this.degSpeed = 0;
    this.orbWidth = 0;
    this.orbHeight = 0;
    this.collidable = false;
    this.colorPulse = {
      color: "",
      speed: 0,
      value: 0
    };
    if (obj) {
      for (property in obj) {
        if (this.hasOwnProperty(property)) {
          this[property] = obj[property];
        }
      }
    }
    Sprite.collection.push(this);
  }
  Sprite.collection = [];
  Sprite.total = 0;

  // ==========================
  // Routines
  // ==========================

  init();

  function init(watchBeginFlag) {
    iterations = 0;
    Sprite.total = 0;
    Sprite.collection = []; ctx.clearRect(0,0,canvasWidth,canvasHeight)
    updateCanvasSize();
    for (var x = 0; x < spriteTotal; x++) {
      new Sprite({
        display: "square",
        width: 3,
        height: 3,
        type: {
          orb: true,
          pong: false
        },
        xdir: (Math.random() > .5 ? 1 : -1) * 3,
        ydir: (Math.random() > .5 ? 1 : -1) * 3,
        deg: 0,
        degSpeed: (Math.random() > .5 ? 1 : -1) * Math.random()*0.04 + 0.03,
        orbWidth: Math.random() * canvasWidth,
        orbHeight: Math.random() * canvasHeight,     
        x: Math.random() * canvasWidth-200,
        y: Math.random() * canvasHeight,
        circle: {
          radius: Math.random() * 7
        },
        color: "0,0,03g", //glitch (solve later)
        alpha: .05
      })
    }
    
    if (!watchBeginFlag) {
      for (var x = 0; x< previewIterations; x++) {
        jacksonPollock();
      }
    }

    run();
  }

  function jacksonPollock() {
    iterations++;
      
    // red
    if (iterations === 200) {
   Sprite.collection.forEach(function(sprite) {
          sprite.x = Math.random() * canvasWidth - 200;
          sprite.y = (Math.random() * canvasHeight) + 200;
          sprite.circle.radius = Math.random() * 7;
          sprite.alpha = .4;
          sprite.color = "211,35,0"
        });
    }
    
     // white
    if (iterations === 500) {
 Sprite.collection.forEach(function(sprite) {
        sprite.x =(Math.random() * canvasWidth) - 200;
        sprite.y = (Math.random() * canvasHeight) + 200;
   sprite.deg = 0;
   sprite.degSpeed = (Math.random() > .5 ? 1 : -1) * Math.random()*0.04 + 0.03,
        sprite.orbWidth = Math.random() * canvasWidth;
        sprite.orbHeight = Math.random() * canvasHeight;   
        sprite.circle.radius = Math.random() * 5;
        sprite.color = "254,177,110"
        sprite.alpha = .4;
      });
    }
    
    // Red
    if (iterations === 600) {
      Sprite.collection.forEach(function(sprite) {
        sprite.deg = 0;
        sprite.x = Math.random() * canvasWidth - 300;
        sprite.y = (Math.random() * canvasHeight) + 200;
        sprite.degSpeed = (Math.random() > .5 ? 1 : -1) * Math.random()*0.04 + 0.03,
        sprite.orbWidth = Math.random() * canvasWidth;
        sprite.orbHeight = Math.random() * canvasHeight;
        sprite.circle.radius = Math.random() * 6;
        sprite.alpha = .3;
        sprite.color = "75,2,0";
      });
    }
    
    // dark blue
    if (iterations === 800) {
      Sprite.collection.forEach(function(sprite) {
        sprite.deg = 0;
        sprite.x = Math.random() * canvasWidth - 300;
        sprite.y = (Math.random() * canvasHeight) + 200;
        sprite.degSpeed = (Math.random() > .5 ? 1 : -1) * Math.random()*0.04 + 0.03,
        sprite.orbWidth = Math.random() * canvasWidth;
        sprite.orbHeight = Math.random() * canvasHeight;
        sprite.circle.radius = Math.random() * 6;
        sprite.alpha = .3;
        sprite.color =  "28,24,70";
      });
    }
   
    // blackish
  if (iterations === 900) {
      Sprite.collection.forEach(function(sprite) {
        sprite.deg = 0;
        sprite.x = Math.random() * canvasWidth - 300;
        sprite.y = (Math.random() * canvasHeight) + 200;
        sprite.circle.radius = Math.random() * 6;
        sprite.width = 8;
        sprite.alpha = .1;
        sprite.color = "0,0,0";
      });
    }
    
    // Red
    if (iterations === 1150) {
      Sprite.collection.forEach(function(sprite) {
        sprite.deg = 0;
        sprite.width = 1;
        sprite.x = Math.random() * canvasWidth - 100;
        sprite.y = (Math.random() * canvasHeight) + 200;
        sprite.degSpeed = (Math.random() > .5 ? 1 : -1) * Math.random()*0.04 + 0.03,
        sprite.orbWidth = Math.random() * canvasWidth;
        sprite.orbHeight = Math.random() * canvasHeight;
        sprite.circle.radius = Math.random() * 6;
        sprite.alpha = .3;
        sprite.color = "211,11,0";
      });
    }
    
    if (iterations === 1270) {
 Sprite.collection.forEach(function(sprite) {
        sprite.x =(Math.random() * canvasWidth) - 200;
        sprite.y = (Math.random() * canvasHeight) + 200;
   sprite.deg = 0;
   sprite.degSpeed = (Math.random() > .5 ? 1 : -1) * Math.random()*0.04 + 0.03,
        sprite.orbWidth = Math.random() * canvasWidth;
        sprite.orbHeight = Math.random() * canvasHeight;   
        sprite.circle.radius = Math.random() * 5;
        sprite.color = "254,177,110"
        sprite.alpha = .4;
      });
    }
    
    
    // Yellow
    if (iterations === 1293) {
   Sprite.collection.forEach(function(sprite) {
          sprite.x = Math.random() * canvasWidth - 200;
          sprite.y = (Math.random() * canvasHeight) + 200;
          sprite.circle.radius = Math.random() * 7;
          sprite.display = "circle";
          sprite.width = 8;
          sprite.height = 2;
          sprite.alpha = .6;
          sprite.color = "254,255,0";
        });
    }

    if (iterations === 1297) {
     Sprite.collection.forEach(function(sprite) {
            sprite.x = Math.random() * canvasWidth - 200;
            sprite.y = (Math.random() * canvasHeight) + 200;
            sprite.circle.radius = Math.random() * 7;
            sprite.display = "circle";
            sprite.height = 2;
            sprite.alpha = .6;
            sprite.color = "255,0,0";
          });
      }
      calculateSprites();
      drawSprites();
    }
 
  function run() {
 document.getElementById("action").innerHTML = Math.floor((iterations/1300)*100) + "%";
    if (iterations > 1300) {
      document.getElementById("action").innerHTML = "Watch again";
      return;
    }
    jacksonPollock();
    window.requestAnimationFrame(run);
  }

function calculateSprites() {
    Sprite.collection.forEach(function(sprite) {
      if (sprite.flag === 0) {
        return;
      }

      if (sprite.type.backToStart) {
        sprite.x+=sprite.xdir;
        sprite.y+=sprite.ydir;
        if (sprite.x >= sprite.xStart) {
          sprite.xdir=-Math.abs(sprite.xdir);
        }
        if (sprite.x <= sprite.xStart) {
          sprite.xdir=Math.abs(sprite.xdir);
        }
        if (sprite.y >= sprite.yStart) {
          sprite.ydir=-Math.abs(sprite.ydir);
        }
        if (sprite.y <= sprite.yStart) {
          sprite.ydir=Math.abs(sprite.ydir);
        }

        // Lazy Course correction
        sprite.x+=sprite.xdir;
        sprite.y+=sprite.ydir;

      }

      if (sprite.type.colorPulse) {
        sprite.colorPulse.value+=sprite.colorPulse.speed;
        if (sprite.colorPulse.value >= 255 || sprite.colorPulse.value < 0) {
          sprite.colorPulse.speed = -sprite.colorPulse.speed;
        }
        sprite.colorPulse.value+=sprite.colorPulse.speed;
        if (sprite.colorPulse.color === "red") {
          sprite.color = sprite.colorPulse.value.toString() + ",0,0";
        }
        if (sprite.colorPulse.color === "green") {
          sprite.color = "0," + sprite.colorPulse.value.toString() + ",0";
        }
        if (sprite.colorPulse.color === "blue") {
          sprite.color = "0,0," + sprite.colorPulse.value.toString();
        }
      }

      if (sprite.type.orb) {
        sprite.deg+=sprite.degSpeed;
        sprite.xOffset = (Math.cos(sprite.deg) * sprite.orbWidth);
        sprite.yOffset = (-Math.sin(sprite.deg) * sprite.orbHeight);
      }

      if (sprite.type.pong) {
        sprite.x+=sprite.xdir;
        sprite.y+=sprite.ydir;
        if (collisionDetected(sprite)) {
          sprite.xdir = -sprite.xdir;
          sprite.ydir = -sprite.ydir;
          sprite.x+=sprite.xdir;
          sprite.y+=sprite.ydir;
        } else {
          if (sprite.x+sprite.xdir+sprite.width >= canvasWidth || sprite.x + sprite.xdir <= 0) {
            sprite.xdir = -sprite.xdir;
          }
          if (sprite.y+sprite.ydir+sprite.height >= canvasHeight || sprite.y + sprite.ydir <= 0) {
            sprite.ydir = -sprite.ydir;
          }
        }
        if (sprite.xdir === 0 || sprite.ydir === 0) {
          sprite.flag = 0;
        }
      }

    });
  }

  function drawSprites() {
    Sprite.collection.forEach(function(sprite) {
      if (sprite.flag === 0) {
        return;
      }
      ctx.beginPath();
      ctx.fillStyle = "rgba(" + sprite.color + "," + sprite.alpha + ")";
      if (sprite.display === "square") {
        ctx.fillRect(sprite.x+sprite.xOffset, sprite.y+sprite.yOffset, sprite.width, sprite.height);
      }
      if (sprite.display === "circle") {
        
 ctx.arc(sprite.x+sprite.xOffset,sprite.y+sprite.yOffset,sprite.circle.radius/2,0,2*Math.PI);
      }
      ctx.fill();
      
      
      
    });
  }

  function updateCanvasSize() {
    getCanvasDimensions();
    canvas.width = canvasWidth;
    canvas.height = canvasHeight;
  }

function collisionDetect(sprite1, sprite2) {
    if (sprite1.x+sprite1.xOffset < sprite2.x+sprite2.xOffset + sprite2.width
    && sprite1.x+sprite1.xOffset + sprite1.width > sprite2.x+sprite2.xOffset
    && sprite1.y+sprite1.yOffset < sprite2.y+sprite2.yOffset + sprite2.height
    && sprite1.height + sprite1.y+sprite1.yOffset > sprite2.y+sprite2.yOffset) {
      return true;
    }
    return false;
  }

  function collisionDetected(sprite) {
     
    // until we make faster
    return false;
    var index = 0;
    var foundCollision = false;
    for (index; index < Sprite.total; index++) {
      if (Sprite.collection[index].index === sprite.index || !Sprite.collection[index].collidable || Sprite.collection[index].flag === 0) {
        continue;
      }
      if (collisionDetect(sprite, Sprite.collection[index])) {
        foundCollision = true;
        break;
      }
    }
    return foundCollision;
  }

  function getCanvasDimensions() {
    canvasWidth = window.innerWidth;
    canvasHeight = window.innerHeight;
  }
              
            
!
999px

Console