cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <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>
            
          
!
            
              body {
  background: #fafafa;
}

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

canvas {
  position: absolute;
  display: block;
  margin: -100px -100px;
  top: 50%;
  left: 50%;
}
            
          
!
            
              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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console