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

              
                <canvas id="canvas"></canvas>

<div class="path-data-container">
  <pre id="path-data"></pre>
</div>
              
            
!

CSS

              
                html, body {
  height: 100%;
}

#canvas {
  user-select: none;
  width: 950px;
  height: 600px;
  
  position: absolute;
  left: 170px;
  top: 5px;
}

.path-data-container {
  position: absolute;
  width: 700px;
  left: 220px;
  top: 608px;
}

pre {
  font-size: 12px;
  white-space: normal;
}

              
            
!

JS

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

class CustomEase extends Ease {
  constructor(public points = {}) {
    super();
        
    if (points.last && points.last.y !== 1) {
      this._calcEnd = true;
    }    
  }
  
  getRatio(k) {
    
    var points = this.points; 
    var p1 = points.first;
    var p0 = null;
    
    while (p1) {
      
      if (p1.x >= k) {
        p0 = p1.prev;
        break;
      }
      
      p1 = p1.next;
    }
        
    if (!p0) return p1.y;

    var t = ((k - p0.x) / (p1.x - p0.x)) || 0;    
    return p0.y + (p1.y - p0.y) * t;    
  }
}

var pathData = document.querySelector("#path-data");
var canvas = document.querySelector("#canvas");

paper.setup(canvas);
paper.settings.handleSize = 5;

var project = paper.project;
var view = paper.view;

var hitOptions = {
  segments: true,
  stroke: true,
  fill: true,
  tolerance: 5,
  handles: true
};

var easePath = null;
var handle1  = null;
var handle2  = null;
var segment  = null;
var hitStart = null;
var hitEnd   = null;
var plotPath = null;

var tween1 = null;
var tween2 = null;

var offset = new paper.Point(50, 50);
var graphSize = new paper.Size(700, 500);

var graph = new paper.Path.Rectangle(offset, graphSize);
graph.style = {
  fillColor: "#eee",
  strokeColor: "#ccc",
  strokeWidth: 0.5
};

var startPoint = offset.add([0, graphSize.height]);
var endPoint = offset.add([graphSize.width, 0]);

var path = new paper.Path({
  strokeWidth: 5,
  strokeColor: "black"
});

var box = new paper.Shape.Rectangle({
  point: [800, 450],
  size: [100, 100],
  fillColor: "green",
  locked: true
});

path.add(startPoint);
path.add(endPoint);

path.fullySelected = true;

paper.view.on("mousedown", downAction);
paper.view.on("mousedrag", dragAction);

var smoothTypes = [
  { type: "continuous" },
  { type: "catmull-rom", factor: 0.5 },
  { type: "geometric", factor: 0.4 },
  { type: "none" },
];

var smoothNames = smoothTypes.map(smooth => smooth.type);
var smoothValue = smoothTypes[0];

var settings = QuickSettings.create(30, 55, "Editor")
  .addDropDown("Smooth Type", smoothNames, value => {
    smoothValue = smoothTypes[value.index];
  })
  .addButton("Smooth Path", smoothPath)
  // .addButton("Ease Points", easePoints)
  .addButton("Reset", clearPoints)
  .addButton("Play", playAnimation)

updatePathData();

function updatePathData() {
  pathData.textContent = path.pathData;
}

function addPoint(point) {
    
  if (point.equals(startPoint) || point.equals(endPoint)) return;
  
  var i = 1;
  
  if (path.segments.length === 2) {
    // temp
  } else {
    
    var max = point.x;    
    var total = path.segments.length;
    
    for (i = 0; i < total; i++) {      
      var seg = path.segments[i];
      if (seg.point.x > max) break;
    }        
  }  
  
  path.insert(i, point);
  segment = path.segments[i];
  smoothPath();
  path.fullySelected = true;  
}

function downAction(event) {
  
  easePath && easePath.remove();
  plotPath && plotPath.remove();
  
  startPoint = path.firstSegment.point.clone();
  endPoint = path.lastSegment.point.clone();
  
  handle1 = handle2 = segment = hitStart = hitEnd = null;
  
  var hit = paper.project.hitTest(event.point, hitOptions);
  
  if (hit) {
        
    if (hit.type === "fill" || hit.type === "stroke") {      
      addPoint(event.point);
      updatePathData();
      return;
    }
    
    if (hit.segment && hit.segment.point.equals(startPoint)) {
      hitStart = true;
    }
    
    if (hit.segment && hit.segment.point.equals(endPoint)) {
      hitEnd = true;
    }
    
    if (hit.type == "handle-in") {

      handle1 = hit.segment.handleIn;      
      handle2 = hit.segment.handleOut;      
      
    } else if (hit.type == "handle-out") {

      handle1 = hit.segment.handleOut;      
      handle2 = hit.segment.handleIn;
      
    } else if (hit.type == "segment") {
      segment = hit.segment;
    }    
  } 
}

function dragAction(event) {
  
  if (segment && !hitStart) {
       
    segment.point = segment.point.add(event.delta);
    
    if (hitEnd) {
      segment.point.x = offset.x + graphSize.width;
    }
  }

  if (handle1) {
    
    handle1.x += event.delta.x;
    handle1.y += event.delta.y;    
    
    // Lock handles?
    // if (hitStart || hitEnd) { return; }
    
    // handle2.x -= event.delta.x;
    // handle2.y -= event.delta.y;
  }
  
  
  updatePathData();
}

function clearPoints() {
  
  easePath && easePath.remove();
  plotPath && plotPath.remove();
  
  var i = path.segments.length - 1;
  
  while (i-- > 1) {
    var seg = path.segments[i];
    seg.remove();
  }    
  
  updatePathData();
}

function smoothPath() {
    
  easePath && easePath.remove();
  plotPath && plotPath.remove();
  
  if (path) {
    
    if (smoothValue.type === "none") {
      path.segments.forEach(segment => {        
        segment.handleIn = segment.handleOut = null;
      });
    } else {
      path.smooth(smoothValue);
    }
  }
  
  updatePathData();
}

function easePoints() {
    
  plotPath && plotPath.remove();
  
  easePath && easePath.remove();
  easePath = path.clone();
  easePath.flatten(0.05);
  easePath.selected = true;
    
  easePath.style = {
    strokeWidth: 1,
    strokeJoin: "round",
    strokeColor: "#00ff00",
    selectedColor: "#00ff00"
  };
  
  var segments = easePath.segments;
  var size = segments.length;
  var first = null;
  var last = null;
  var prev = null;
  
  for (var i = 0; i < size; i++) {
    
    var point = segments[i].point.subtract(offset);
    
    var node = {
      prev: prev,
      next: null,
      x: point.x / graphSize.width, 
      y: 1 - (point.y / graphSize.height)
    };
    
    if (!first) first = node;
    if (prev) prev.next = node;
    prev = node;
    last = node;
  }
  
  return { first, last, size };
}

function playAnimation() {
  
  tween1 && tween1.kill();
  tween2 && tween2.kill();
  plotPath && plotPath.remove();
  
  var points = easePoints();
  
  easePath.remove();
  
  var color = "#eee"
  
  plotPath = new paper.Path();
  plotPath.selected = false;
  
  plotPath.style = {
    strokeWidth: 3,
    strokeJoin: "round",
    strokeColor: color,
    selectedColor: color
  };
  
  plotPath.add(new paper.Point(offset.x, offset.y + graphSize.height));
  
  var obj = {
    y: offset.y + graphSize.height
  }
  
  var ease = new CustomEase(points);
  var delay = 0.25;
  
  box.position.y = 500;
  
  tween1 = TweenMax.to(box.position, 2, { y: 100, delay, ease });    
  
  tween2 = TweenMax.to(obj, 2, {
    y: 50,
    delay, 
    ease,
    onUpdate: function() {
      var x = this.progress() * graphSize.width + offset.x;
      var y = obj.y;
      plotPath.add(new paper.Point(x, y))
    }
  });    
}


















              
            
!
999px

Console