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 class="controls">
  <button id="reset">Reset</button> --
  <button id="move-1">Box 1</button>
  <button id="move-2">Box 2</button>
  <button id="move-3">Box 3</button>
  <button id="move-4">Box 4</button>
  <button id="move-5">Box 5</button>
  <button id="move-6">Box 6</button>
</div>

<section class="container">
  
  <div id="viewport-border" class="viewport"></div>
  
  <div id="viewport" class="viewport">
    <div id="world">
      <div id="box-1" class="box">1</div>
      <div id="box-2" class="box">2</div>
      <div id="box-3" class="box">3</div>
      <div id="box-4" class="box">4</div>
      <div id="box-5" class="box">5</div>
      <div id="box-6" class="box">6</div>
    </div>
  </div> 
  
</section>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  overflow: scroll;
}

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

#viewport-border {
  position: absolute;
  border: 4px solid firebrick;
  z-index: 500;
}

#viewport {
  position: relative;
  overflow: visible;
}

#world {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: left top;
  background: #ccc;
  background: rgba(128,128,128,0.3);
}

.container {
  position: relative;
  margin: 20px;
}

.controls {
  padding: 20px;
  position: relative;
  z-index: 1000;
}

.box {
  position: absolute;
  top: 0;
  left: 0;  
  padding: 4px;  
  color: rgba(255,255,255, 0.8);  
  opacity: 0.8;
}

#box-1 {
  background: tomato;
}

#box-2 {
  background: limegreen;
}

#box-3 {
  background: dodgerblue;
}

#box-4 {
  background: mediumvioletred;
}

#box-5 {
  background: darkslateblue;
}

#box-6 {
  background: #FFC107;
}
              
            
!

JS

              
                console.clear();
var log = console.log.bind(console);

function demo() { 
  
  var viewSize  = new Point(500, 500);
  var worldSize = new Point(1000, 700);
  
  var $world = $("#world")[0];
  var $view  = $("#viewport")[0];
  
  TweenLite.set($world, { width: worldSize.x, height: worldSize.y });
  TweenLite.set(".viewport", { 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 - 100, view.height - 100);  
  var camera = new Camera($world, world, view);
  var zIndex = 1000; 
  var rects  = [];
  
  $(".box").each((index, element) => {
    
    var width  = _.random(25, 250);
    var height = _.random(50, 100);
    var x = _.random(worldSize.x - width  - 50);
    var y = _.random(worldSize.y - height - 50);    
    var i = index + 1;
    
    TweenLite.set(element, { x, y, width, height });
    
    var rect = Rectangle.fromElement(element, origin);
    rects.push(rect);
    
    $("#move-" + i).click(() => {
      
      var scale = rect.aspectRatio(bounds);      
      var point = camera.getPointAt(rect.centerPoint(), scale);
  
      TweenLite.set(element, { zIndex: zIndex++ });
      TweenLite.to(camera, 1, { scale, x: point.x, y: point.y }); 
    });    
  });
  
  $("#reset").click(() => {
    TweenLite.to(camera, 1, { x: 0, y: 0, scale: 1 });
  }); 
}

//
// 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 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;
  }
}

demo();
              
            
!
999px

Console