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

              
                <canvas>
	Your browser does not support the HTML5 canvas tag.
</canvas>
<button>Animate</button>
              
            
!

CSS

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

// Reset

*
  margin: 0
  padding: 0

html
  box-sizing: border-box

*, *:before, *:after
  box-sizing: inherit

body, h1,h2,h3,h4,p
  border: 0

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

// Canvas

canvas
  position: fixed
  margin: 0
  padding: 0
  width: 100vw
  height: 100vh
  top: 0
  
// -----------------------------------------

// Body

body
  height: 500vh
  
button
  position: fixed
  top: 20px
  right: 20px
  padding: 10px
              
            
!

JS

              
                console.clear();

// bind es6 methods to context
let es6BindAll = function(context, methodNames) {
    if (!Array.isArray(methodNames)) {
      methodNames = [methodNames];
    }
    methodNames.map(function (method) {
      try {
        context[method] = context[method].bind(context);
      } catch (e) {
        var cName = context.name ? ", " + context.name : "";
        var mName = typeof method === "function" ? method.name : method;
        console.log("Error: " + e);
        throw new Error("Cannot bind method " + mName + " to the supplied context" + cName);
      }
      return null;
    });
 }

/**
 * Store Viewport, Scroll Position & Cells
*/

let html = document.documentElement,
    body = document.body;

let app = {
  viewport: {
    w: Math.max(window.innerWidth || html.clientWidth || body.clientWidth || screen.availWidth),
    h: Math.max(window.innerHeight || html.clientHeight || body.clientHeight || screen.availHeight)
  },
  feed: {
    cells: []
  },
  scroll: {
    top: 0
  }
}

// listen for scroll event
window.addEventListener('scroll', ()=> {
  app.scroll.top = document.getElementsByTagName('html')[0].scrollTop;
  PubSub.publish('viewport.scroll');
});


/**
 * Cell
*/

class Cell {
   
  constructor(sourceX, sourceY, width, height, context) {
    this.originalY = sourceY;
    
    this.x = sourceX;
    this.y = sourceY;
    this.w = width;
    this.h = height;

    this.targetX = sourceX;
    this.targetY = sourceY;
    this.targetWidth = width;
    this.targetHeight = height;

    this.context = context;
    
    es6BindAll(this, [
      "draw"
    ]);
  }

  draw(){
    window.requestAnimationFrame(()=> {
      this.context.rect(this.x, this.y, this.w, this.h);
      this.context.fill();
    });
  }
}

/**
 * Layout
*/

class Layout {
   
  constructor() {
    this.state;
    this.button = document.querySelector('button');
    this.canvas = document.querySelector('canvas'); 
    this.context = this.canvas.getContext("2d");
    this.counter = 0;
    this.tl;

    this.time = 6;
    this.scrollOffset = 0;
    es6BindAll(this, [
      "init",
      "addEventListeners",
      "setCanvasSize",
      "addCell",
      "render",
      "animate",
      "layout",
      "onScroll",
      "clear",
    ]);
    this.init();
  }

  init(){
    this.addEventListeners();
    this.setCanvasSize();
    for(let i = 0; i < 5; i++){
      this.addCell();
    }
    this.render();
  }

  addEventListeners(){
    PubSub.subscribe('viewport.scroll', this.onScroll);
    this.button.addEventListener('click', this.layout);
  }

  setCanvasSize(){
    this.context.mozImageSmoothingEnabled = false;
    this.context.webkitImageSmoothingEnabled = false;
    this.context.msImageSmoothingEnabled = false;
    this.context.imageSmoothingEnabled = false;
    
    const devicePixelRatio = window.devicePixelRatio || 1;

    // determine the 'backing store ratio' of the canvas context
    const backingStoreRatio = (
      this.context.webkitBackingStorePixelRatio ||
      this.context.mozBackingStorePixelRatio ||
      this.context.msBackingStorePixelRatio ||
      this.context.oBackingStorePixelRatio ||
      this.context.backingStorePixelRatio || 1
    );

    // determine the actual ratio we want to draw at
    let ratio = devicePixelRatio / backingStoreRatio;

    if (devicePixelRatio !== backingStoreRatio) {
      // set the 'real' canvas size to the higher width/height
      this.canvas.width = app.viewport.w * ratio;
      this.canvas.height = app.viewport.h * ratio;

      // ...then scale it back down with CSS
      this.canvas.style.width = app.viewport.w + 'px';
      this.canvas.style.height = app.viewport.h + 'px';
    }
    else {
      // this is a normal 1:1 device; just scale it simply
      this.canvas.width = app.viewport.w;
      this.canvas.height = app.viewport.h;
      this.canvas.style.width = '';
      this.canvas.style.height = '';
    }

    // scale the drawing context so everything will work at the higher ratio
    this.context.scale(ratio, ratio);
  }

  addCell(){
    let width = 100;
    let height = 100;
    let x = 0;
    let y = (this.counter * height) + this.counter * 20;
    app.feed.cells.push(new Cell(x, y, width, height, this.context));  
    this.counter++;
  }

  render(){
    for (let cell of app.feed.cells) {
      cell.draw(); 
    }
  }

  animate() {
    if(this.tl){
      this.tl.kill();
      this.tl = undefined;
    }
    this.tl = new TimelineLite({
      paused: true,
      onUpdate: ()=> {
        this.clear();
        this.render();
      },
      onComplete: ()=> {
        this.state = undefined;
      }
    });
    let i = 0;
    for (let cell of app.feed.cells) {
      this.tl.to(cell, this.time, {
        x: cell.targetX,
        y: cell.targetY,
        ease: Power1.easeOut
      }, 0);
      i++;
    }
    this.tl.play();
  }

  layout(){
    for (let cell of app.feed.cells) {
      cell.targetX = app.viewport.w - 100; 
      cell.originalY = cell.targetY;
    }
    this.time = 6;
    this.animate();
  }

  onScroll(){
    if(this.state == 'layout'){
      return;
    }
    let scrollOffset = app.scroll.top;
    for (let cell of app.feed.cells) {
      cell.targetY = cell.originalY - scrollOffset;
    }
    this.time = 0;
    this.state = 'scroll';
    this.animate();
  }

  clear(){
    this.context.save();

    this.context.setTransform(1, 0, 0, 1, 0, 0);
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    this.context.beginPath();

    this.context.restore();
  }
}

new Layout();
              
            
!
999px

Console