123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!-- A demo of the SVG Plugin -->
<!-- SVG Morph -->
<div id="wrapper">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 600" >
    <path id="path1" fill="orangered" d="" />
    <path id="path2" style="visibility:hidden" d=""/>
  </svg>
</div>
<h1>SVG Path Morph Utility for KUTE.js</h1>
<p>Helps you find the best tween options for your SVG morphs such as <i class="text-pink">morphPrecision</i> and <i class="text-blue">morphIndex</i> starting with version 1.6.0.<br>Change values below then click the rectangle to replay, or <a href="https://thednp.github.io/kute.js/svg.html">click here</a> to learn more :)</p>
<form>
  <input data-toggle="popover" data-trigger="focus" data-title="morphPrecision" data-content="The <strong>morphPrecision</strong> value determines the accuracy of the morph, the higher value the less accuracy. This usually should be set first." id="precision" type="text" class="form-input" placeholder="morphPrecision Value" value="15" />
  <input data-toggle="popover" data-trigger="focus" data-title="morphIndex" data-content="The <strong>morphIndex</strong> value sets the point where both shapes start being drawn" id="index" type="text" class="form-input" placeholder="morphIndex Value"  value="127" />
  <textarea data-toggle="popover" data-trigger="focus" data-title="Start shape" data-content="Animation will start from this shape" id="input1" class="form-input" placeholder="Paste d attribute value for starting shape">M38.01,5.653h526.531c17.905,0,32.422,14.516,32.422,32.422v526.531 c0,17.905-14.517,32.422-32.422,32.422H38.01c-17.906,0-32.422-14.517-32.422-32.422V38.075C5.588,20.169,20.104,5.653,38.01,5.653z</textarea>
  <textarea data-toggle="popover" data-trigger="focus" data-title="End shape" data-content="The animation will finish with this shape" id="input2" class="form-input" placeholder="Paste d attribute value for end shape">M301.113,12.011l99.25,179.996l201.864,38.778L461.706,380.808 l25.508,203.958l-186.101-87.287L115.01,584.766l25.507-203.958L0,230.785l201.86-38.778L301.113,12.011z</textarea>
  <input data-toggle="popover" data-trigger="focus" data-title="viewBox" data-content="The <strong>viewBox</strong> attribute value sets the scale for your SVG" id="viewbox" type="text" class="form-input" placeholder="viewBox Value" value="0 0 600 600" />
  <div style="width: 100%;">
    <label data-toggle="popover" data-title="reverseFirstPath" data-content="Option to reverse the starting shape">
      <input id="reverse1" type="checkbox" name="reverse1" value="reverse1"> <i class="text-orange">reverseFirstPath</i>
    </label>
    <label data-toggle="popover" data-title="reverseSecondPath" data-content="Option to reverse the ending shape">
      <input id="reverse2" type="checkbox" name="reverse2" value="reverse2"> <i class="text-lime">reverseSecondPath</i>
    </label>
    <p class="log"></p>
    <p><small>The small circles identify the start of both shapes. Also this demo always uses the latest nightly build!!</small></p>
  </div>
</form>
            
          
!
            
              /* some style */
svg {overflow: visible}
body {background: #333; color: #ccc; font-family: Helvetica, Arial, sans-serif; overflow: hidden; line-height: 20px; font-size: 13px}
#wrapper {width: 200px; float: left; margin: 0 20px 0 10px}
a {color: lime}
form {width: 60%; display: inline-block}
.form-input {
  background: #222; color: #ccc;
  border: 2px solid #444; border-radius: 5px;
  padding: 10px 15px; width: 38%; width: calc(40% - 24px); margin: 0 10px 10px 0; display: inline-block
}
textarea.form-input { width: 80% }
input[type="checkbox"] {vertical-align: middle}

/* some colors */
b, strong, h1 { color: #fff }
.text-olive {color: #9C27B0; } 
.text-indigo { color: #673AB7; } 
.text-green { color: #4CAF50; } 
.text-red { color: #e91b1f; } 
.text-yellow { color: #ffd626; } 
.text-blue { color: #2196F3; }
.text-pink { color: #E91E63; } 
.text-orange { color: #FF5722; }
.text-lime { color: #CDDC39; }
.text-teal { color: #009688; }

/* popover */
.fade {
  opacity: 0;
  -webkit-transition: opacity 0.15s linear;
  -o-transition: opacity 0.15s linear;
  transition: opacity 0.15s linear;
}
.fade.in {
  opacity: 1;
}
.popover {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1060;
  display: none;
  max-width: 276px;
  padding: 1px;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-style: normal;
  font-weight: normal;
  letter-spacing: normal;
  line-break: auto;
  line-height: 1.42857143;
  text-align: left;
  text-align: start;
  text-decoration: none;
  text-shadow: none;
  text-transform: none;
  white-space: normal;
  word-break: normal;
  word-spacing: normal;
  word-wrap: normal;
  font-size: 14px;
  background-color: #ffffff;
  -webkit-background-clip: padding-box;
          background-clip: padding-box;
  border: 1px solid #cccccc;
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 6px;
  color: #333
}
.popover b,
.popover strong {color: #111}
.popover.top {
  margin-top: -10px;
}
.popover.right {
  margin-left: 10px;
}
.popover.bottom {
  margin-top: 10px;
}
.popover.left {
  margin-left: -10px;
}
.popover-title {
  margin: 0;
  padding: 8px 14px;
  font-size: 14px;
  background-color: #f7f7f7;
  border-bottom: 1px solid #ebebeb;
  border-radius: 5px 5px 0 0;
}
.popover-content {
  padding: 9px 14px;
}
.popover > .arrow,
.popover > .arrow:after {
  position: absolute;
  display: block;
  width: 0;
  height: 0;
  border-color: transparent;
  border-style: solid;
}
.popover > .arrow {
  border-width: 11px;
}
.popover > .arrow:after {
  border-width: 10px;
  content: "";
}
.popover.top > .arrow {
  left: 50%;
  margin-left: -11px;
  border-bottom-width: 0;
  border-top-color: #999999;
  border-top-color: rgba(0, 0, 0, 0.25);
  bottom: -11px;
}
.popover.top > .arrow:after {
  content: " ";
  bottom: 1px;
  margin-left: -10px;
  border-bottom-width: 0;
  border-top-color: #ffffff;
}
.popover.right > .arrow {
  top: 50%;
  left: -11px;
  margin-top: -11px;
  border-left-width: 0;
  border-right-color: #999999;
  border-right-color: rgba(0, 0, 0, 0.25);
}
.popover.right > .arrow:after {
  content: " ";
  left: 1px;
  bottom: -10px;
  border-left-width: 0;
  border-right-color: #ffffff;
}
.popover.bottom > .arrow {
  left: 50%;
  margin-left: -11px;
  border-top-width: 0;
  border-bottom-color: #999999;
  border-bottom-color: rgba(0, 0, 0, 0.25);
  top: -11px;
}
.popover.bottom > .arrow:after {
  content: " ";
  top: 1px;
  margin-left: -10px;
  border-top-width: 0;
  border-bottom-color: #ffffff;
}
.popover.left > .arrow {
  top: 50%;
  right: -11px;
  margin-top: -11px;
  border-right-width: 0;
  border-left-color: #999999;
  border-left-color: rgba(0, 0, 0, 0.25);
}
.popover.left > .arrow:after {
  content: " ";
  right: 1px;
  border-right-width: 0;
  border-left-color: #ffffff;
  bottom: -10px;
}
            
          
!
            
              /* KUTE.js SVG Morph utilities */
  var element = document.getElementById('path1'),
    svg = element.ownerSVGElement,
    input1 = document.getElementById('input1'),
    input2 = document.getElementById('input2'),
    // define the tween object, but only as undefined    
    tween,
    viewbox = document.getElementById('viewbox'),
    reverse1 = document.getElementById('reverse1'),
    reverse2 = document.getElementById('reverse2'),
    index = document.getElementById('index'),
    precision = document.getElementById('precision'),
    idx, log = document.querySelector('.log'),
    ns = 'http://www.w3.org/2000/svg',
    pathReg = /(m[^(h|v|l)]*|[vhl][^(v|h|l|z)]*)/gmi,
    clone = function(a) {
      var copy;
      if (a instanceof Array) {
        copy = [];
        for (var i = 0, len = a.length; i < len; i++) {
          copy[i] = clone(a[i]);
        }
        return copy;
      }
      return a;
    },
    getOnePath = function(p){ return p.split(/z/i).shift() + 'z'; },
    createPath = function (p){ // createPath
      var c = document.createElementNS(ns,'path'), d = typeof p === 'object' ? p.getAttribute('d') : p; 
      c.setAttribute('d',d); return c;
    },
    getSegments = function(s,e,r){ // getSegments returns an array of points based on a sample size morphPrecision
      var s1 = [], e1 = [], le1 = s.getTotalLength(), le2 = e.getTotalLength(), ml = Math.max(le1,le2),
        d = r, ar = ml / r, j = 0, sl = ar*r; // sl = sample length

      while ( (j += r) < sl ) { // populate the points arrays based on morphPrecision as sample size
        s1.push( [s.getPointAtLength(j).x, s.getPointAtLength(j).y]);
        e1.push( [e.getPointAtLength(j).x, e.getPointAtLength(j).y]);
      }
      return [s1,e1];
    },
    getClosestPoint = function(p,t,s){
      var x, y, a = [], l = s.length, dx, nx, pr;
      for (i=0; i<l; i++){
        x = Math.abs(s[i][0] - t.x);
        y = Math.abs(s[i][1] - t.y);
        a.push( Math.sqrt( x * x + y * y ) );
      }
      dx = a.indexOf(Math.min.apply(null,a));
      pr = !!s[dx-1] ? dx-1 : l-1;
      nx = !!s[dx+1] ? dx+1 : 0;
      return Math.abs(s[pr][0] - t.x) < p && Math.abs(s[pr][1] - t.y) < p ? s[pr]
      : Math.abs(s[nx][0] - t.x) < p && Math.abs(s[nx][1] - t.y) < p ? s[nx] 
      : Math.abs(s[dx][0] - t.x) < p && Math.abs(s[dx][1] - t.y) < p ? s[dx] 
      : [t.x,t.y];
    },
    pathToAbsolute = function(p) {
      var np = p.match(pathReg), wp = [], l = np.length, s, c, r, x = 0, y = 0;
      for (var i = 0; i<l; i++){
        np[i] = np[i]; c = np[i][0]; r = new RegExp(c+'[^\\d|\\-]*','i'); 
        np[i] = np[i].replace(/(^|[^,])\s*-/g, '$1,-').replace(/(\s+\,|\s|\,)/g,',').replace(r,'').split(',');
        np[i][0] = parseFloat(np[i][0]);
        np[i][1] = parseFloat(np[i][1]);
        if (i === 0) { x+=np[i][0]; y +=np[i][1]; }
        else {
          x = np[i-1][0]; 
          y = np[i-1][1]; 
          if (/l/i.test(c)) {
            np[i][0] = c === 'l' ? np[i][0] + x : np[i][0];
            np[i][1] = c === 'l' ? np[i][1] + y : np[i][1];  
          } else if (/h/i.test(c)) {
            np[i][0] = c === 'h' ? np[i][0] + x : np[i][0];
            np[i][1] = y;  
          } else if (/v/i.test(c)) {
            np[i][0] = x;
            np[i][1] = c === 'v' ? np[i][0] + y : np[i][0];
          }
        }
      }
      return np;
    },
    getBestIndex = function(s,e){ // getBestIndex for shape rotation
      var s1 = clone(s), e1 = clone(e), d = [], i, l = e.length, t, ax, ay;
      for (i=0; i<l; i++){
        t = e1.splice(i,l-i); e1 = t.concat(e1);
        ax = Math.abs(s1[i][0] - e1[i][0]);
        ay = Math.abs(s1[i][1] - e1[i][1]);
        d.push( Math.sqrt( ax * ax + ay * ay ) );
        e1 = []; e1 = clone(e); t = null;
      }
      return d.indexOf(Math.min.apply(null,d));
    },
    startingPoints = [],
    showStartingPoints = function(s,e,isp){ // showPoints helper function to visualize the points on the path
      var c, a = arguments, cl, p, l;
      for (var i=0; i<2; i++){
        p = a[i]; l = p.length; cl = i=== 0 ? { f: 'orangered', o: 'orange' } : { f: 'Lime', o: 'LimeGreen' };
        for (var j=0; j<l; j++) {
          c = document.createElementNS(ns,'circle');
          c.setAttribute('cx',p[j][0]); c.setAttribute('cy',p[j][1]);
          c.setAttribute('r', j===0 ? 20 : 10 ); c.setAttribute('fill', j===0 ? cl.f : cl.o);
          if (isp) { svg.appendChild(c); } else if (!isp && j===0 ) { svg.appendChild(c);}
          startingPoints.push(c);
        }
      }
      c = null;
    },
    removeStartingPoints = function(){
      for (var i=0,l = startingPoints.length; i<l; i++ ) { startingPoints[i].parentNode === svg && svg.removeChild(startingPoints[i]); } startingPoints = [];
    },
    showInfo = function(s1,e1){
      var isPolygon = !/[CSQTA]/i.test(input1.value) && !/[CSQTA]/i.test(input2.value),
          mpi = isPolygon ? 'the polygon with the most points.<br>' : (parseInt(precision.value) === 15 ? 'the default' : 'your') +' <i class="text-pink">morphPrecision</i> value of <b>'+precision.value+'</b>.<br>',
          theLog = '<b>KUTE.js Path Morph Log</b><br>The morph used <b>' + s1.length + '</b> points to draw both paths based on ' + mpi
        + (index.value ? 'You\'ve configured the <i class="text-blue">morphIndex</i> to ' + index.value + ' while the recommended is <b>' + idx + '</b>. You can then increase/decrease this value till you get the perfect morph animation.<br>' : 'You may also consider a <i class="text-blue">morphIndex</i> for your morph. Currently the best index seems to be around <b>' + idx + '</b> value.<br>')
        + (!reverse1.checked && !reverse2.checked ? 'If the current animation is still not satisfactory after using a <i class="text-blue">morphIndex</i> value, consider reversing one of the paths.<br>Maybe the paths do not intersect or they really have different draw directions.' : 'You\'ve chosen that the first path to have ' + ( reverse1.checked  ? '<i class="text-orange">reversed</i> draw direction, ' : 'unchanged draw direction, ') + 'while second path is to be ' + (reverse2.checked ? '<i class="text-lime">reversed</i>.<br>' : 'unchanged.<br>') )
        + (reverse1.checked && reverse2.checked ? '<b>Remember</b>: when both shapes are reversed the recommended <i class="text-blue">morphIndex</i> is actually <b>' + (s1.length - idx)+'</b>.' : '' );
      
      showStartingPoints(s1,e1,isPolygon);
      
      log.innerHTML = theLog;      
    },
    pathCross = function(s,e,svg){ // pathCross
      var s1, e1, arr, arL, sm, lg, smp, lgp, nsm = [], sml, cl = [], len, tl, cs,
          isPolygon = !/[CSQTA]/i.test(input1.value) && !/[CSQTA]/i.test(input2.value);

      if (!isPolygon) {
        s = createPath(s); e = createPath(e);  
        arr = getSegments(s,e,parseInt(precision.value)); 
        s1 = arr[0]; e1 = arr[1]; arL = e1.length;
      } else {
        s = pathToAbsolute(s); e = pathToAbsolute(e);

        if ( s.length !== e.length ){
          arL = Math.max(s.length,e.length);
          if ( arL === e.length) { sm = s; lg = e; } else { sm = e; lg = s; }
          sml = sm.length;

          smp = createPath('M'+sm.join('L')+'z'); len = smp.getTotalLength() / arL;
          for (var i=0; i<arL; i++){
            tl = smp.getPointAtLength(len*i);
            cs = getClosestPoint(len,tl,sm);
            nsm.push( [ cs[0], cs[1] ] );
          }

          if (arL === e.length) { e1 = lg; s1 = nsm; } else { s1 = lg; e1 = nsm; }
        } else {
          s1 = s; e1 = e;
        }
      }

      // determine index for best/minimum distance between points
      idx = getBestIndex(s1,e1);

      // shift second array to for smallest tween distance
      if (index.value) {
        var e11 = e1.splice(parseInt(index.value),arL-parseInt(index.value));
        e1 = e11.concat(e1);
      }
      
      showInfo(s1,e1);

      s = e = null;
    };

// get the inputs
function init(){
  svg.setAttribute('viewBox',viewbox.value);
  removeStartingPoints();
  element.setAttribute('d',getOnePath(input1.value));
  // showInfo
  paths = pathCross(getOnePath(input1.value),getOnePath(input2.value),element.ownerSVGElement);
  // write a simple tween object
  tween = KUTE.fromTo('#path1',  // target shape
   { path: input1.value, attr : { fill: 'orangered' } }, // from shape
   { path: input2.value, attr : { fill: 'lime' } }, // to shape
   { // options
    easing: 'easingCubicOut', 
    yoyo: true, repeat: 1, duration: 5000,
    morphIndex: parseInt(index.value),
    morphPrecision: parseInt(precision.value),
    reverseFirstPath: reverse1.checked || false,
    reverseSecondPath: reverse2.checked || false
  });
}
input1.value && input2.value && init();

// trigger it whenever you want
document.getElementById('wrapper').onclick = function() {
  input1.value && input2.value && init();
  tween.start();
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console