Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div id="two"></div>
              
            
!

CSS

              
                html, body {
  height: 100%;
  background: #111;
  overflow: hidden;
}

svg,
canvas {
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #111;
}
              
            
!

JS

              
                // The width and height of the box
// Also used for placing certain things
const W = 300;
const H = 300;

// Using 'box' to mean 'the larger, wrapper shape that contains all of the small moving parts'
// and using 'square' to mean 'one of the smaller moving parts'
// For both of these consts, the integer is the length of one side of that shape
const BOXSIZE = 150;
const SQSIZE = 15;
// lightnesses of grays
const STARTL = 10;
const ENDL = 30;

function randomDec(min, max) {
  return Math.random() * (max - min) + min;
}

var elem = document.getElementById('two');
var two = new Two({ width: W, height: H, type: Two.Types.canvas}).appendTo(elem);


// Make the canvas
var box = two.makeRectangle(W/2, H/2, BOXSIZE, BOXSIZE);
box.noStroke();
box.noFill();

// Function to convert relative x/y to absolute x/y
function makeAbs(vertex) {
  return {x: vertex.x + box.translation.x, y: vertex.y + box.translation.y}
}
// Convert the corner vertices of the 'canvas' (inner box) into absolute coords
box.tl = makeAbs(box.vertices[0]);
box.tr = makeAbs(box.vertices[1]);
box.br = makeAbs(box.vertices[2]);
box.bl = makeAbs(box.vertices[3]);

function Square() {
  // Called the first time a Square object is constructed
  this.make = () => {
    this.origin = {};
    // Find a starting y, making sure the top edge is at least the top y and that the bottom edge is at most the bottom y
    this.origin.y = randomDec(box.tl.y + (SQSIZE/2), box.bl.y - (SQSIZE/2));
    this.origin.x = Math.floor(randomDec(box.tl.x - (SQSIZE/2) - BOXSIZE, box.tl.x - (SQSIZE/2)));
    // Progress counter will be in integers -- for each .25, .5, or .75
    // movement, we'll be adding 1, 2, or 3 to this, to help figure out
    // which gradient to apply
    this.progress = 0;
    // velocity is pretty literally amount the square travels to the right
    this.velocity = [.25, .5, .75][Math.floor(Math.random() * 3)];
    // Make the actual square shape, make it invisible for now
    this.s = two.makeRectangle(this.origin.x, this.origin.y, SQSIZE, SQSIZE);
    this.s.noFill();
    this.s.noStroke();
  };
  // When the square hits the right edge, reuse it
  this.reset = () => {
    // Move it back to the starting x, make it invisible
    this.s.translation.x = this.origin.x;
    this.s.noFill();
    // Give it a new velocity
    this.velocity = [.25, .5, .75][Math.floor(Math.random() * 3)];
    // Reset the progress ticker
    this.progress = 0;
    if (this.completed) {
      this.completed = 0;
    }
  }
  // run this.make when the object is created
  (() => {
    this.make();
  })();
}

// Make all stops (gradient start/ends) ahead of time
var numStops = BOXSIZE * 4 + 1;
var zeroStops = [];
var oneStops = [];
for (var i = 0; i < numStops; i++) {
  let lightness = i / (numStops - 1) * (ENDL - STARTL);
  // Two.Stop params: (offset, color)
  zeroStops.push(new Two.Stop(0, 'hsl(0, 0%, '+ lightness +'%)'));
  oneStops.push(new Two.Stop(1, 'hsl(0, 0%, '+ lightness +'%)'));
}

// Make all gradients ahead of time, using the stops above
var grads = [];
for (var i = 0; i < numStops - 1; i++) {
  // Two.LinearGradient params: (x1, y1, x2, y2, [Two.Stops])
  // x1/y1/x2/y2 are all offset from the center of the element that gets the gradient
  // so: start at the middle of the left edge, end at the midle of the right edge
  let grad = new Two.LinearGradient(-SQSIZE/2, 0, SQSIZE/2, 0, [zeroStops[i], oneStops[i+1]]);
  grads.push(grad);
}

// Make all squares ahead of time
var sqs = [];
for (var i = 0; i < 500; i++) {
  sqs.push(new Square());
}

// The animation loop
two.bind('update', function(frameCount, timeDelta) {
  for (var i = 0, len = sqs.length; i < len; i++) {
    // Move every square by its velocity
    sqs[i].s.translation.x += sqs[i].velocity;
  }
  // Determine the completion amount and paint the square
  for (var i = 0, len = sqs.length; i < len; i++) {
    // ... but only if the square has gotten to the wrappers' left edge!
    if (sqs[i].s.translation.x - (SQSIZE/2) >= box.tl.x) {
      // Increment progress ticker by   1,  2, or   3,
      // depending on if velocity was .25, .5, or .75
      sqs[i].progress += (sqs[i].velocity / .25);
      // Determine the fill
      sqs[i].s.fill = grads[sqs[i].progress];
      sqs[i].s.noStroke();
    }
  }
 
 // See if we should reset the square
  for (var i = 0, len = sqs.length; i < len; i++) {
    // (i.e., if the right edge of the individual square > the right edge of the wrapping box)
    if (sqs[i].s.translation.x + (SQSIZE/2) > box.tr.x) {
      sqs[i].reset();
    }
  }
}).play();
              
            
!
999px

Console