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 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

              
                <div id="main">
  <div id="stage-wrapper">
    <div id="stage-container">
      <div id="stage">
        <svg id="stage-master" version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0" y="0" width="800" height="600" viewBox="0 0 800 600">
          <g id="canvas-container">
            <g id="canvas" font-family="alpha_echoregular" fill="#ffffff"></g>
          </g>
        </svg>
      </div>
    </div>
  </div>
</div>
<div id="waveform"></div>

<div id="form-wrapper">
  <button id="reset">RESET ANIMATION</button>
  <button id="play">PLAY</button>
<textarea id="copy" name="copy" id="" cols="40" rows="33">
A politician 
needs the 
ability

to foretell
what is

going to 
happen

tomorrow,
next week,  

next month,
and
next year.

And to 
have the 
ability 

afterwards 

to explain 
why it 
didn't
happen.

Winston 
Churchill
</textarea>
</div>
              
            
!

CSS

              
                // $borderSize: 2px

// *
//   box-sizing: border-box

@font-face
  font-family: 'alpha_echoregular'
  src: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.eot')
  src: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.eot?#iefix') format("embedded-opentype"), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.woff') format("woff"), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.ttf') format("truetype"), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.svg#alpha_echoregular') format("svg")
  font-weight: normal
  font-style: normal

body
  font-family: 'alpha_echoregular', sans-serif
  background: black

#main
  display: block
  margin: 0 auto
  width: 800px
  height: 600px
  position: relative
  
#stage-wrapper
  width: 800px
  height: 600px
  margin: 5px auto 15px
  clear: both
  position: relative
  top: auto
  left: auto
  transform: translate3d(0px, 0px, 0px)

#stage-container
  position: relative
  width: 800px
  height: 600px
  margin: 0

#stage
  width: 800px
  height: 600px
  overflow: hidden
  position: relative
  background: transparent
  position: relative
  
#stage-master
  background: #a66bbe
  position: absolute
  margin: 0
  overflow: hidden
  
  tspan
    line-height: 1.0em
    display: inline-block
    // opacity: 0

html, body
  height: 100%

body
  overflow: scroll

#waveform
  width: 800px
  position: relative
  margin: 0 auto

button 
  padding: 4px 10px
  margin-right: 5px
  cursor: pointer

#form-wrapper
  display: block
  position: relative
  margin: 20px auto
  width: 800px
  text-align: center

button 
  background: green
  box-shadow: none
  color: white
  border: none
  padding: 6px 20px
  border-radius: 5px
  margin: 0 0 10px 0

textarea 
  display: block
  background: #2c0a3a
  color: antiquewhite
  font-size: 14px
  border: 4px solid burlywood
  height: 350px
  margin: 0 auto
              
            
!

JS

              
                console.clear();

// init the audio file
var wavesurfer = WaveSurfer.create({
  container: '#waveform',
  waveColor: '#4286f4',
  progressColor: '#b20000'
});

wavesurfer.load('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/bg-simple2.mp3');

var kineticLayout = function(resetFirst) {
    resetFirst = resetFirst == 'reset' ? true : false;
  
    var stage = document.getElementById('stage'),
        canvas = document.getElementById('canvas'),
        fontRange = [18,50],
        colors = ["#008000", "#0000ff", "#4b0082", "#9400d3"],
        pattern = [ // thisPosition and matchPosition are based on their respective x/y coords (tl = topLeft, br = bottomRight, etc)
          {thisPosition: 'tl', matchPosition: 'br', rotation: null, origin: 'left top', textAlign: 'start', offsetX: 0, offsetY: 0, multiplier: 1}, // this one is pretty much a dummy until later maybe
          {thisPosition: 'tl', matchPosition: 'br', rotation: 90, origin: 'left top', textAlign: 'start', offsetX: 'lastLineEnd', offsetY: 0, multiplier: -1}, // lastLineEnd = prev.lastLine.width
          {thisPosition: 'tl', matchPosition: 'br', rotation: null, origin: 'left top', textAlign: 'start', offsetX: 0, offsetY: 'FirstLineLastWordTop', multiplier: 1}, // lastWordTop = this.y - prev.firstLine.lastWord.y (should be a negative number already)
          {thisPosition: 'tl', matchPosition: 'br', rotation: null, origin: 'left top', textAlign: 'end', offsetX: 'LastLineLastWordStart', offsetY: 0, multiplier: 1}, // lastWordStart = (prev.x + prev.lastLine.width) - prev.lastLine.lastWord.width
          {thisPosition: 'bl', matchPosition: 'tr', rotation: 90, origin: 'left bottom', textAlign: 'start', offsetX: 0, offsetY: 0, multiplier: 1},
          {thisPosition: 'tl', matchPosition: 'br', rotation: -90, origin: 'left top', textAlign: 'start', offsetX: 0, offsetY: 0, multiplier: 1},
          {thisPosition: 'tl', matchPosition: 'br', rotation: null, origin: 'left top', textAlign: 'start', offsetX: 0, offsetY: 'LastLineLastWordEnd', multiplier: -1}, // prev.lastLine.height
        ];

     function init() {
       
      var dfd = $.Deferred();
      dfd
        .done(function(){
          if(reset) { reset(); }
        })
        .done( createWordMatrix )
        .done( spaceLines )
        .done( positionGroups )
        .done( animateType )
        .done(function(){
      });
      dfd.resolve();
    }

     function createWordMatrix() {
      var sections = breakIntoGroups(),
          wordArr = [],
          grpCnt = 0,
          txtCnt = 0,
          wordCnt = 0;

      for ( let section of sections ) {

        var group = document.createElementNS('http://www.w3.org/2000/svg', 'g');
        // var fontSize = 24;
        var fontSize = randomInt(fontRange[0],fontRange[1]);
        group.setAttribute('font-size',fontSize);
        group.setAttribute('id',`group-${grpCnt}`);
        group.setAttribute('class', 'text-group');
        canvas.appendChild(group);
        grpCnt++;

        var lines = breakIntoLines(section);
        var dims = null; // for knowing the dimensions and location of the previous text element
        for ( let line of lines ) {

          line = line.trim();

          var color = randomInt(0,colors.length-1);
          var T = document.createElementNS('http://www.w3.org/2000/svg', 'text');
          T.setAttribute('class',`text-${txtCnt}`);
          T.setAttribute('font-size',fontSize);
          T.setAttribute('fill', colors[color]);
          group.appendChild(T);

          if( dims == null ) {
            dims = {x: 0, y: fontSize/2};
          }

          var words = line.split(" ");
          wordCnt = 0;
          for ( let word of words ) {
            word = wordCnt < words.length-1 ? word + " " : word;
            var TS = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
            TS.setAttribute('alignment-baseline','hanging');
            TS.setAttribute('class',`word-${wordCnt}`);
            TS.innerHTML = word;
            T.appendChild(TS);
            wordCnt++;
          }


          var pos = $(T).position();
          var HT = parseInt($(T).height());
          var WD = parseInt($(T).width());

          dims = { el: T, x: 0, y: pos.top + HT, ht: HT, wd: WD };
          wordArr.push(dims);
          txtCnt++;
        }
      }
    }

     function spaceLines() {
      $('#canvas g').each(function(){
        var G = $(this),
            cnt = 0,
            fntSize = G.attr('font-size');

        G.find('text').each(function(){
          if(cnt > 0 ) {
            var ts = $(this).find('tspan');
            ts.attr('y',fntSize*cnt);
          }
          cnt++;
        });
      });
    }

     function positionGroups() {
      var gNum = 1, // group number
          pNum = 1, // pattern array iterate
          defaultFontSize = canvas.getAttribute('font-size');

      // set the first group at x: 10, y; 10
      TweenMax.set('#canvas g#group-0',{x: 10, y: 10});

      $('#canvas g#group-0').nextAll().each(function(){

        var curr = $(this),
            prev = $(this).prev(),
            S = pattern[pNum], // settings from pattern[pNum];
            pBox = prev[0].getBoundingClientRect(),
            cBox = curr[0].getBoundingClientRect(),
            pGS = prev[0]._gsTransform,
            topPad = (curr[0].getAttribute('font-size')/4) >= 0 ? (curr[0].getAttribute('font-size')/3) : 0,
            id = curr.attr('id'),
            stageOffset = $('#stage').offset();
        
        
        var lines = document.querySelectorAll(`g#${id} text`);
        for ( let line of lines ) {
          line.setAttribute('text-anchor', S.textAlign);
          var lineBox = line.getBoundingClientRect();
        }
        
        var offX = stageOffset.left,
            offY = stageOffset.top,
            X = pBox.right - offX,
            Y = pBox.top - offY,
            B = pBox.bottom - offY;
        
        switch(S.matchPosition) {
          case 'br':
              // check for rotation
              if( S.rotation == null ) {
                TweenMax.set(curr,{x: X, y: B});
              } else {
                TweenMax.set(curr,{x: X, y: B+topPad, rotation: S.rotation, transformOrigin: S.origin});
              }
            break;
          case 'tr':
              // check for rotation
              if( S.rotation == null ) {
                TweenMax.set(curr,{x: X, y: Y});
              } else {
                TweenMax.set(curr,{x: X, y: Y, rotation: S.rotation, transformOrigin: S.origin});
              }
            break;
        }
        pNum = pNum < (pattern.length-1) ? pNum+1 : 0;
      });
    }

    function breakIntoLines(section) {
      return section.split(/\n/);
    }

    function breakIntoGroups() {
      return $('#copy').val().trim().split(/\n\n/);
    }

    function breakIntoWords(line) {
      return $(line.trim().split(" "));
    }

    function randomInt(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min)) + min;
    }

    function reset() {
      var groupElements = document.querySelectorAll('g.text-group');
      for ( let group of groupElements ) {
        canvas.removeChild(group);
      }
    }
  
  init();
}

function init() {
  kineticLayout();
}

function animateType() {

  var audioSettings = {currentVolume: 1, fadeTime: 5};
  
  var tl = new TimelineMax({paused: true, onStart: playAudio, onStartParams: [0], onComplete: fadeAudio});
  var canvasDims = $('#canvas')[0].getBoundingClientRect();

  var viewSize  = new Point(800, 600);
  var worldSize = new Point(canvasDims.width, canvasDims.height);
  
  var $view = $("#stage-master")[0];
  var $world = $("#canvas")[0];
  var $container = $("#canvas-container")[0];
  
  TweenLite.set($container, { width: worldSize.x, height: worldSize.y });
  TweenLite.set("#stage-master", { width: viewSize.x, height: viewSize.y });
     
  var origin = Rectangle.fromElement($view);
  var view   = Rectangle.fromElement($view,  origin);
  var world  = Rectangle.fromElement($world, origin);
  var bounds = new Rectangle(0, 0, view.width, view.height);
  var camera = new Camera($world, world, view);
  
  var scene  = { rotation: 0, scale: 1 };
  var zIndex = 1000;
  var rects  = [];
  var maxScale = 3;

  function createAnimation() {
    
    // loop through each group and inside that, each word!!
    var wordAnimations = [
          [{from: {opacity: 0}, to: {opacity: 1}}],
          [{from: {opacity: 0}, to: {opacity: 1}}],
          [{from: {opacity: 0, attr:{y: 0},ease: 'Bounce.easeOut'}, to: null}]
        ],
        textElementAnimations = [
          [{from: {scaleY: 0, ease: 'Bounce.easeOut'}, to: null}],
          [{from: {scaleY: 0, ease: 'Bounce.easeOut'}, to: null}]
        ];
    
    $('#canvas g').each(function(){
      var rotation = $(this)[0]._gsTransform.rotation;
      var newRotate;
      switch(rotation) {
        case 0:
          newRotate = 0;
          break;
        case 90:
          newRotate = -90;
          break;
        case -90:
          newRotate = 90;
          break;
      }
      tl.to($container, 0.5, { rotation: newRotate }, tl.duration());
      
      $(this).find('text').each(function(){
        
        var chanceForContainerAnimation = 5;
        var randNum = _.random(1, 10);
        var addTextElementAnimation = false;
        
        if ( randNum > chanceForContainerAnimation ) {
          // animate in the whole text element
          var aniNum = _.random(0,textElementAnimations.length-1);
          var textAnimation = textElementAnimations[aniNum][0].from;
          console.log(textAnimation);
          
          tl.from( $(this), 0.5, textAnimation , tl.duration() );
          addTextElementAnimation = true;
        }
        
        $(this).find('tspan').each((index, element) => {

          var i = index + 1;

          var rect = Rectangle.fromTspan(element, origin);
          rects.push(rect);

          var scale = rect.aspectRatio(bounds);
          scale = scale > maxScale ? maxScale : scale;
          var point = camera.getPointAt(rect.centerPoint(), scale);
          scene.scale = scale;
          var startAt = tl.duration();
          if(addTextElementAnimation) {
            startAt -= 0.5;
            addTextElementAnimation = false;
          }
          var min = 0;
          var max = wordAnimations.length-1;
          var num = _.random(min, max);
          var animation = wordAnimations[num][0];

          if( animation.to == null ) {
            tl.from(element, 0.25, animation.from, startAt+.5);
          } else {
            tl.fromTo(element, 0.25, animation.from, animation.to, startAt+.5);
          }
          tl.to(camera, 0.75, { scale, x: point.x, y: point.y, ease:Power4.easeOut }, startAt);

        });
      });
      
    });
  }
  createAnimation();
  
  function playAudio(location) {
    wavesurfer.seekTo(location);
    wavesurfer.play();
    wavesurfer.on('finish', function(){
      playAudio(0);
    });
  }

  function stopAudio() {
    if(wavesurfer.isPlaying()) {
      wavesurfer.stop();
    }
  }

  function fadeAudio() {
    TweenLite.to(audioSettings, audioSettings.fadeTime, {
      currentVolume: 0,
      ease: Sine.easeOut,
      onUpdate: function(){
        wavesurfer.setVolume(audioSettings.currentVolume);
      },
      onComplete: function(){
        stopAudio();
        wavesurfer.setVolume(1);
        audioSettings.currentVolume = 1;
      }
    });
  }
  
  $("#reset").click(reset);
  $("#play").click(play);
  // $("#rotate").click(rotate);
  // $("#overflow").on("change", applyOverflow);
  
  TweenLite.set($container, { transformOrigin: `${view.centerX}px ${view.centerY}px` });
  
  function rotate() {        
    scene.rotation = scene.rotation === 0 ? -90 : 0;    
    TweenLite.to($container, 0.5, { rotation: scene.rotation });    
  }
  
  function reset() {
    tl.seek(0).stop().clear();
    tl.eventCallback("onStart", null);
    tl.eventCallback("onComplete", null);
    tl.kill();
    scene.rotation = 0;
    TweenLite.to(camera, 1, { x: 0, y: 0, scale: 1 });
    TweenLite.to($container, 1, { rotation: 0 });
    stopAudio();
    var dfd = $.Deferred();
    dfd
      .done( function(){
        kineticLayout.call('reset');
      })
      // .done( animateType )
      .done(function(){
      });
    dfd.resolve();
  }
  
  function play() {
    tl.seek(0).play();
  }
  
  function applyOverflow() {
    TweenLite.set($view, { overflow: this.checked ? "visible" : "hidden" });
  }
}

$(function(){
  init();
});

//
// CAMERA
// =========================================================================== 
class Camera {
  
  _x = this.world.x;
  _y = this.world.y;

  scale  = 1;
  width  = this.viewport.width;
  height = this.viewport.height;
  
  timeline = new TimelineMax({ repeat: -1 });

  constructor(public target, public world, public viewport) {
    
    // To avoid calling TweenLite.set() on every animation frame for transforms
    this.timeline.to(target, 999999, {
      x: 0,
      y: 0,
      scale: 0,
      modifiers: {
        x: () => this.world.x,
        y: () => this.world.y,
        scaleX: () => this.scale,
        scaleY: () => this.scale
      }
    });    
  }
  
  get x() { return this._x; }
  get y() { return this._y; }
  
  set x(x) {
    this._x = x;
    this.world.x = -x;
  }
  
  set y(y) {
    this._y = y;
    this.world.y = -y;
  }  
      
  get centerX() { return this.x + this.halfWidth; }
  get centerY() { return this.y + this.halfHeight; }  
  set centerX(x) { this.x = x - this.halfWidth;  }
  set centerY(y) { this.y = y - this.halfHeight; }

  get halfWidth()  { return this.width  / 2; }
  get halfHeight() { return this.height / 2; }
    
  getPointAt(point, scale) {
    
    scale = scale || this.scale || 1;
        
    var x = point.x * scale - this.halfWidth;
    var y = point.y * scale - this.halfHeight;
    
    return new Point(x, y);
  }
  
  centerOver(rectangle, scale) {
       
    this.scale = scale || this.scale || 1;    
    
    this.x = rectangle.centerX * this.scale - this.halfWidth;
    this.y = rectangle.centerY * this.scale - this.halfHeight;
    
    return this;
  }

  follow(leader) {
    
  }
}

//
// RECTANGLE
// =========================================================================== 
class Rectangle {
  
  constructor(public x = 0, public y = 0, public width = 0, public height = 0) {
    
  }
  
  get centerX() { return this.x + this.halfWidth; }
  get centerY() { return this.y + this.halfHeight; }  
  set centerX(x) { this.x = x - this.halfWidth;  }
  set centerY(y) { this.y = y - this.halfHeight; }
  
  get halfWidth()  { return this.width  / 2; }
  get halfHeight() { return this.height / 2; }
  
  get left()   { return this.x; }
  get top()    { return this.y; }
  get right()  { return this.x + this.width;  }
  get bottom() { return this.y + this.height; } 
  
  get volume() { return this.width * this.height; }
  
  static fromElement(element, offset) {
    
    var rect = element.getBoundingClientRect();
    // var rect = element.getClientRects()[0];
    
    var x = rect.left;
    var y = rect.top;
    
    if (offset) {            
      x -= offset.x;
      y -= offset.y;
    } 
    
    return new Rectangle(x, y, rect.width, rect.height);    
  }
  
  static fromTspan(element, offset) {
    
    // var rect = element.getBoundingClientRect();
    var rect = element.getClientRects()[0];
    
    var x = rect.left;
    var y = rect.top;
    
    if (offset) {            
      x -= offset.x;
      y -= offset.y;
    } 
    
    return new Rectangle(x, y, rect.width, rect.height);    
  }
  
  static contains(a, x, y) {
    
    if (a.width <= 0 || a.height <= 0) {
      return false;
    }

    return (x >= a.x && x < a.right && y >= a.y && y < a.bottom);
  }
  
  static containsRaw(rx, ry, rw, rh, x, y) {
    return (x >= rx && x < (rx + rw) && y >= ry && y < (ry + rh));
  }
  
  static containsPoint(a, point) {
    return Rectangle.contains(a, point.x, point.y);
  }
  
  static containsRect(a, b) {
    
    if (a.volume > b.volume) {
      return false;
    }

    return (a.x >= b.x && a.y >= b.y && a.right < b.right && a.bottom < b.bottom);
  }
  
  static intersection() {
    
    var x = Math.max(a.x, b.x);
    var y = Math.max(a.y, b.y);
    var width  = Math.min(a.right,  b.right)  - x;
    var height = Math.min(a.bottom, b.bottom) - y;
    
    return new Rectangle(x, y, width, height);
  }
  
  static intersects(a, b) {
    
    if (a.width <= 0 || a.height <= 0 || b.width <= 0 || b.height <= 0) {
      return false;
    }

    return !(a.right < b.x || a.bottom < b.y || a.x > b.right || a.y > b.bottom);
  }
  
  static intersectsRaw(a, left, right, top, bottom, tolerance = 0) {
    return !(left > a.right + tolerance || right < a.left - tolerance || top > a.bottom + tolerance || bottom < a.top - tolerance);
  }
   
  aspectRatio(rectangle) {
    
    var scaleX = rectangle.width  / this.width;
    var scaleY = rectangle.height / this.height;
    
    return Math.min(scaleX, scaleY);
  }
  
  centerPoint() {
    return new Point(this.centerX, this.centerY);
  }
  
  centerOn(x, y) {
    
    this.centerX = x;
    this.centerY = y;

    return this;
  }
  
  clone() {
    return new Rectangle(this.x, this.y, this.width, this.height);
  }
  
  contains(x, y) {
    
    if (this.width <= 0 || this.height <= 0) {
      return false;
    }
    
    if (x >= this.left && x < this.right) {
      if (y >= this.top && y < this.bttom) {
        return true;
      }
    }
    
    return false;
  }
   
  copy(rectangle) {
    
    this.x = rectangle.x;
    this.y = rectangle.y;
    this.width  = rectangle.width;
    this.height = rectangle.height;

    return this;
  }
  
  intersection(rectangle) {
    return Rectangle.intersection(this, rectangle);
  }
  
  intersects(rectangle) {
    return Rectangle.intersects(this, rectangle);
  }
  
  intersectsRaw(left, right, top, bottom, tolerance) {
    return Rectangle.intersectsRaw(this, left, right, top, bottom, tolerance);
  }
  
  resize(width, height) {
    
    this.width  = width;
    this.height = height;

    return this;
  }
  
  scale(scalar) {
    
    this.width  *= scalar;
    this.height *= scalar;
    
    return this;
  }
  
  set(x, y, width, height) {
    
    this.x = x;
    this.y = y;
    this.width  = width;
    this.height = height;

    return this;
  } 
}

//
// POINT
// =========================================================================== 
  class Point {

  constructor(public x = 0, public y = 0) {
    
  }

  static fromBezier(bezier, t) {

    var a = bezier[0];
    var b = bezier[1];
    var c = bezier[2];
    var d = bezier[3];

    var ab = Point.lerp(a, b, t);
    var bc = Point.lerp(b, c, t);
    var cd = Point.lerp(c, d, t);

    ab.lerp(bc, t);
    bc.lerp(cd, t);

    return ab.lerp(bc, t);
  }

  static centroid(points) {

    var point = new Point();
    var total = points.length;

    if (total === 1) {
      return point.copy(points[0]);
    }

    for (var i = 0; i < total; i++) {
      Point.add(point, points[i]);
    }

    return point;
  }

  static lerp(point1, point2, t) {
    var x = point1.x + (point2.x - point1.x) * t;
    var y = point1.y + (point2.y - point1.y) * t;
    return new Point(x, y);
  }

  static copy(point) {
    return new Point(point.x, point.y);
  }

  static equals(point1, point2) {
    return point1.x === point2.x && point1.y === point2.y;
  }

  static fuzzyEquals(point1, point2, epsilon = 1e-6) {

    var a0 = point1.x;
    var a1 = point1.y;
    var b0 = point2.x;
    var b1 = point2.y;

    return (Math.abs(a0 - b0) <= epsilon * Math.max(1.0, Math.abs(a0), Math.abs(b0))
            && Math.abs(a1 - b1) <= epsilon * Math.max(1.0, Math.abs(a1), Math.abs(b1)));
  }

  static add(point1, point2) {
    return new Point(point1.x + point2.x, point1.y + point2.y);
  }

  static subtract(point1, point2) {
    return new Point(point2.x - point1.x, point2.y - point1.y);
  }

  static multiply(point, scalar) {
    return new Point(point.x * scalar, point.y * scalar);
  }

  static divide(point, scalar) {
    return new Point(point.x / scalar, point.y / scalar);
  }

  static fromPoints(point1, point2) {
    return new Point(point2.x - point1.x, point2.y - point1.y);
  }

  static negate(point) {
    return new Point(-point.x, -point.y);
  }

  static normalize(point) {

    var x = point.x;
    var y = point.y;

    var len = x * x + y * y;

    if (len > 0) {

      len = 1 / Math.sqrt(len);        
      point.x = point.x * len;
      point.y = point.y * len;       
    }

    return point;      
  }

  static parse(obj, xProp = "x", yProp = "y") {
    var point = new Point();
    if (obj[xProp]) point.x = parseInt(obj[xProp], 10);
    if (obj[yProp]) point.y = parseInt(obj[yProp], 10);
    return point;
  }

  static mid(point1, point2) {
    var x = (point1.x + point2.x) / 2;
    var y = (point1.y + point2.y) / 2;
    return new Point(x, y);
  }

  add(point) {
    this.x += point.x;
    this.y += point.y;
    return this;
  }

  // Horizontal angle
  angle() {
    var angle = Math.atan2(this.y, this.x);      
    if (angle < 0) angle += 2 * Math.PI;
    return angle;
  }

  ceil() {
    this.x = Math.ceil(this.x);
    this.y = Math.ceil(this.y);
    return this;
  }

  clone() {
    return new Point(this.x, this.y);
  }

  copy(point) {      
    return this.set(point.x, point.y);
  }

  copyTo(point) {
    point.x = this.x;
    point.y = this.y;
    return point;
  }

  floor() {
    this.x = Math.floor(this.x);
    this.y = Math.floor(this.y);
    return this;
  }

  subtract(point) {
    this.x -= point.x;
    this.y -= point.y;
    return this;
  }

  multiply(scalar) {
    this.x *= scalar;
    this.y *= scalar;
    return this;
  }

  divide(scalar) {
    this.x /= scalar;
    this.y /= scalar;
    return this;
  }    

  cross(point) {
    return this.x * point.y - this.y * point.x;
  }

  dot(point) {
    return this.x * point.x + this.y * point.y;
  }

  distance(point) {
    var dx = this.x - point.x;
    var dy = this.y - point.y;
    return Math.sqrt(dx * dx + dy * dy);
  }

  distanceSq(point) {
    var dx = this.x - point.x;
    var dy = this.y - point.y;
    return dx * dx + dy * dy;
  }

  inverse() {
    this.x = 1 / this.x;
    this.y = 1 / this.y;
    return this;
  }

  invert() {
    return this.set(this.y, this.x);
  }

  isZero() {
    return this.x === 0 && this.y === 0;
  }

  length() {
    return Math.sqrt(this.x * this.x + this.y * this.y);
  }

  lengthSq() {
    return this.x * this.x + this.y * this.y;
  }

  lerp(point, t) {      
    this.x += (point.x - this.x) * t;
    this.y += (point.y - this.y) * t;
    return this;
  }   

  min(point) {      
    this.x = Math.min(this.x, point.x);
    this.y = Math.min(this.y, point.y);
    return this;
  }

  max(point) {      
    this.x = Math.max(this.x, point.x);
    this.y = Math.max(this.y, point.y);
    return this;
  }

  negate() {      
    this.x = -this.x;
    this.y = -this.y;
    return this;
  }

  normalize() {

    var x = this.x;
    var y = this.y;

    var len = x * x + y * y;

    if (len > 0) {

      len = 1 / Math.sqrt(len);        
      this.x = this.x * len;
      this.y = this.y * len;       
    }

    return this;
  }

  normalRightHand() {
    return this.set(this.y * -1, this.x);
  }

  perp() {
    return this.set(-this.y, this.x);
  }

  rperp() {
    return this.set(this.y, -this.x);
  }

  random(scale = 1) {

    var r = Math.random() * 2 * Math.PI;

    this.x = Math.cos(r) * scale;
    this.y = Math.sin(r) * scale;
    return this;
  }

  resize(length) {
    return this.normalize().scale(length);
  }

  round() {      
    this.x = Math.round(this.x);
    this.y = Math.round(this.y);
    return this;
  }

  rotate(center, angle, distance) {

    var x = this.x - center.x;
    var y = this.y - center.y;

    if (distance == null) {

      var s = Math.sin(angle);
      var c = Math.cos(angle);

      this.x = x * c - y * s + center.x;
      this.y = x * s + y * c + center.y;

    } else {

      var t = angle + Math.atan2(y, x);
      this.x = center.x + distance * Math.cos(t);
      this.y = center.y + distance * Math.sin(t);        
    }

    return this;
  }

  scale(scalar) {      
    this.x *= scalar;
    this.y *= scalar;
    return this;
  }

  scaleAndAdd(point, scalar) {      
    this.x += (point.x * scalar);
    this.y += (point.y * scalar);
    return this;
  }

  set(x, y) {      
    this.x = x || 0;
    this.y = y || ((y !== 0) ? x : 0);      
    return this;
  }

  transformMatrix(m) {

    var x = this.x;
    var y = this.y;

    this.x = (m.a * x) + (m.c * y) + m.tx;
    this.y = (m.b * x) + (m.d * y) + m.ty;      
    return this;
  }

  verticalAngle() {
    return Math.atan2(this.x, this.y);
  }

  zero() {
    this.x = this.y = 0;
    return this;
  }
}

// animateType();
              
            
!
999px

Console