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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <svg id="svg" width="200" height="200" viewBox="0 0 200 200">
  <path id="circle-path" d="
M10,100a90,90 0 1,0 180,0a90,90 0 1,0 -180,0
"/>
  <path id="rect-path" d="M90,10 110,10 110,190 90,190 90,10" />
  
  <path id="triangle-path" d="M100,40 170,160 30,160 100,40" />
</svg>
<canvas id="canvas" width="200" height="200"></canvas>
              
            
!

CSS

              
                body {
  background: #fafafa;
}

svg {
  stroke: none;
  fill: none;
  display: none;
  position: absolute;
}

canvas {
  position: absolute;
  display: block;
  margin: -100px -100px;
  top: 50%;
  left: 50%;
}
              
            
!

JS

              
                var colors = ["#EDEEC0", "#ED7B84", "#7397C3", "#7EB77F"];

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var pathPointsFrom, pathPointsTo, pathPointsNow; 
var steps = 200;
var offset = 0;
var pathCount = 0;
var interpolationPoint = {percentage: 0};
ctx.lineWidth = 4;
ctx.lineCap = "round";

function drawPathToCanvas() {
  var thisColor, lastColor = getColorSegment(0);
  ctx.strokeStyle = lastColor;
  ctx.beginPath();
  for (var i = 0, l = pathPointsNow.length; i < l; i++) {
    if (pathPointsNow[i+1]) {
      ctx.moveTo(pathPointsNow[i].x, pathPointsNow[i].y);
      ctx.lineTo(pathPointsNow[i+1].x, pathPointsNow[i+1].y);
    } else {
      ctx.lineTo(pathPointsNow[i].x, pathPointsNow[i].y);
    }
    thisColor = getColorSegment(i);
    if (thisColor) {
      if (thisColor != lastColor) {
        ctx.closePath();
        ctx.stroke();
        ctx.beginPath();
        ctx.strokeStyle = thisColor;
        lastColor = thisColor;
      }
    }
  }
  ctx.closePath();
  ctx.stroke();
}

function samplePath(pathSelector) {
  var path = document.getElementById(pathSelector);
  var length = path.getTotalLength();
  var points = [];
  for (var i = 0; i <= steps; i++) {
    points.push(path.getPointAtLength(length*i/steps));
  }
  return points;
}

function interpolatePaths() {
  var points = [];
  for (var i = 0; i <= steps; i++) {
    points.push({x: pathPointsFrom[i].x + (pathPointsTo[i].x-pathPointsFrom[i].x)*interpolationPoint.percentage, y: pathPointsFrom[i].y +(pathPointsTo[i].y-pathPointsFrom[i].y)*interpolationPoint.percentage});
  }
  return points;
}

function getColorSegment(i) {
  var p = i/steps + offset;
  if (p > 1) p = p-1;
  var point = Math.floor(p*4);
  return colors[point];
}

var paths = [samplePath("circle-path"), samplePath("rect-path"), samplePath("triangle-path")];

function loop() {
  ctx.clearRect(0, 0, 200, 200);
  offset = offset + 0.009;
  pathPointsNow = interpolatePaths();
  if (offset >= 1) offset = 0;
  drawPathToCanvas();
  requestAnimationFrame(loop);  
}

function tweenPaths() {
  pathPointsFrom = paths[pathCount];
  if (pathCount+1 <= 2) pathPointsTo = paths[pathCount+1];
  else pathPointsTo = paths[0];
  
  TweenLite.to(interpolationPoint, 0.7, {percentage: 1, ease: Power2.easeInOut, delay: 0.4, onComplete: function() {
    interpolationPoint.percentage = 0;
    pathCount++;
    if (pathCount > 2) {
      pathCount = 0;
    }
    tweenPaths();
  }});
}

tweenPaths();
loop();



              
            
!
999px

Console