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

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

              
                <button class="draw">Draw</button><button class="reset">Reset</button>
<div id="stage" width="800" height="600" style="width: 800px; height: 600px;">
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="800" height="600" x="0" y="0" id="stage-master" style="background: rgba(0, 0, 0, 0);">
    
    <g id="gp-1" class="svg-wrapper" ref="1" x="0" y="0" height="256" width="256" data-svg-origin="204.5 204.5" style="transform-origin: 0px 0px 0px; transform: matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1); z-index: 1004;">
        <svg xmlns="http://www.w3.org/2000/svg" id="svg-1" viewBox="0 0 256 256" class="svg inline" role="img" ref="1" height="411" width="411" x="0" y="0" data-svg-origin="128 128" style="transform-origin: 0px 0px 0px; transform: matrix(1.60546, 0, 0, 1.60546, -77.5, -77.5);">
          <polyline fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" points="  198.5,198.7 198.5,243.2 58.5,243.2 58.5,199.7 "></polyline>
          <rect x="100.5" y="113.2" fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" width="55" height="130"></rect>
          <polygon fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" points="  217.5,159.2 211.5,159.2 211.5,188.1 198.6,199.6 184.5,188.1 184.5,159.2 177.5,159.2 177.5,130.2 217.5,130.2 "></polygon>
          <polygon fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" points="  77.5,159.2 71.5,159.2 71.5,188.1 58.6,199.6 44.5,188.1 44.5,159.2 37.5,159.2 37.5,130.2 77.5,130.2 "></polygon>
          <path fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="  M121.5,169.2V145c0-3.6,3.4-6.4,7-6.4l0,0c3.6,0,7,2.9,7,6.4v24.2H121.5z"></path>
          <path fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="  M57.7,91c0,0,7.6,16.3,10.6,21.3c7.9,13.2,16.9,17.9,16.9,17.9H57.7H30.2c0,0,7.8-3.5,16.9-17.9C50.2,107.3,57.7,91,57.7,91z"></path>
          <path fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="  M128.3,58.5c0,0,10.6,22.8,14.8,29.8c11.1,18.4,23.6,24.8,23.6,24.8h-38.4H89.9c0,0,10.9-4.7,23.6-24.8  C117.9,81.4,128.3,58.5,128.3,58.5z"></path>
          <path fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="  M198.3,91c0,0,7.6,16.3,10.6,21.3c7.9,13.2,16.9,17.9,16.9,17.9h-27.5h-27.5c0,0,7.8-3.5,16.9-17.9C190.9,107.3,198.3,91,198.3,91z"></path>
          <line fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="44" y1="159.2" x2="72" y2="159.2"></line>
          <line fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="184" y1="159.2" x2="211" y2="159.2"></line>
          <path fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="  M117.5,241.7v-23.5c0-6.1,5.1-11,11.2-11h0.3c6.1,0,11.5,4.8,11.5,11v23.5"></path>
          <path fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="  M50.5,152.2V145c0-3.6,3.4-6.4,7-6.4l0,0c3.6,0,7,2.9,7,6.4v7.2H50.5z M191.5,152.2V145c0-3.6,3.4-6.4,7-6.4l0,0c3.6,0,7,2.9,7,6.4  v7.2H191.5z M128.5,58.7v-5 M133.1,46.7c0,2.4-1.9,4.3-4.3,4.3s-4.3-1.9-4.3-4.3c0-2.4,1.9-4.3,4.3-4.3  C131.2,42.4,133.1,44.3,133.1,46.7z"></path>
          <path fill="none" stroke="#000000" stroke-width="5" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="  M115,88.2h26 M72,167.2h12h9h6 M156,167.2h7h8h12 M57.5,90.7v-17 M76.5,70.8l-19,9.2V61.6L76.5,70.8z M198.5,90.7v-17 M217.5,70.8  l-19,9.2V61.6L217.5,70.8z M128.5,39.7v-12 M151.3,16.1l-22.8,12.4V8.8L151.3,16.1z M129.5,207.7v34"></path>
        </svg>
    </g>

    <g id="hand" class="svg-wrapper" ref="3" x="0" y="0" height="256" width="256" data-svg-origin="63.5 63.5" style="transform-origin: 0px 0px 0px; transform: matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1); z-index: 1015;">
      <svg xmlns="http://www.w3.org/2000/svg" class="svg inline scale-it" viewBox="0 0 256 256" id="svg-3" role="img" ref="3" height="129" width="129" x="0" y="0" data-svg-origin="128 128" style="transform-origin: 0px 0px 0px; transform: matrix(0.5039, 0, 0, 0.5039, 63.5, 63.5);">
        <title>Cartoon Pencil</title>
        <path fill="#383234" d="M9.2 251L5 246.8l15.4-48.5 37 36.9"></path>
        <path fill="#110F10" d="M9.2 251L5 246.8l41.8-22.1 10.6 10.5"></path>
        <path fill="#F9A665" d="M57.4 235.2l-37-36.9 9.7-38.2 65.5 65.5"></path>
        <path fill="#BF7C4F" d="M56.9 236.9l-10.1-12.2 38.2-9.6 10.6 10.5"></path>
        <path fill="#CA65A7" stroke="#E57AB0" stroke-miterlimit="10" d="M224.6 94.2l-64-63.9 24.3-25.1 65.5 65.5z"></path>
        <path fill="#9E5186" d="M224.6 94.2L215.4 83 239 59.4l11.4 11.3"></path>
        <path fill="#FEC345" d="M94.4 224.5l-64.3-64.4L160.6 30.3l64 63.9"></path>
        <path fill="#D18828" d="M95.6 225.6l-10.1-12.3L214 83.7l10.6 10.5"></path>
        <g fill="none" stroke="#000" stroke-width="5" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round">
          <path d="M184.9 5.2l65.5 65.5L95.6 225.6 9.2 251 5 246.8l25.1-86.7zM30.5 160.6l63.9 63.9M20.4 198.3l37 36.9M224.6 94.2l-64-63.9"></path>
          <path d="M57.4 132.8c-1.6 3.9.9 9.9 4.6 13.6 7.9 7.9 18.6 1.3 24.4 7.1 7.2 7.2-1.8 18.5 6.4 26.8 7.9 7.9 18.6 1.3 24.4 7.1 3.7 3.7 4.9 8.8 3.3 12.7M165.2 132.7l39.3-39.3M145.3 152.8L156 142M129.8 168.2l5.5-5.4"></path>
        </g>
      </svg>
    </g>

  </svg>
</div>
              
            
!

CSS

              
                #stage-master
  width: 800px
  height: 600px

              
            
!

JS

              
                
// Polyfill for getTransformToElement
SVGElement.prototype.getTransformToElement = SVGElement.prototype.getTransformToElement || function(toElement) {
  return toElement.getScreenCTM().inverse().multiply(this.getScreenCTM());  
};

var svgID = '#svg-1',
    hand = '#hand',
    pathArr,
    multiplier = 0.2;

// look for compound paths in the original SVG
var paths = document.querySelectorAll(`${svgID} path`);

TweenLite.set("#gp-1", {
  rotation: 45
});

TweenLite.set(hand, {
  rotation: 270
});

var dg = Draggable.create(hand, {
  throwProps:false,
  onPress: function(e) {
    var coords = {x: this.pointerX, y: this.pointerY };
    console.log(coords);
  }
});

var newPaths = breakApartPaths(paths,$(svgID)[0]);

for( path of paths ) {
  $(svgID)[0].removeChild(path);
}
for( path of newPaths ) {
  $(svgID)[0].appendChild(path);
}

MorphSVGPlugin.convertToPath(`${svgID} circle, ${svgID} rect, ${svgID} ellipse, ${svgID} line, ${svgID} polygon, ${svgID} polyline`);
pathArr = document.querySelectorAll(`${svgID} path`);

TweenMax.set(pathArr,{drawSVG: "0"});
TweenMax.to('#gp-1', 0.5, {x: 200, y: 25});

TweenLite.defaultEase = Linear.easeNone;

function reset() {
  TweenMax.set(pathArr,{drawSVG: "0"});
  TweenMax.set(hand, {x: 0, y: 0});
}

function drawIt() {
  
  var gpWD = parseInt($('#gp-1').attr('width'));
  var gpHT = parseInt($('#gp-1').attr('height'));
  var svgWD = parseInt($('#svg-1').attr('width'));
  var svgHT = parseInt($('#svg-1').attr('height'));
  var scaleX = svgWD/gpWD;
  var scaleY = svgHT/gpHT;
  
  var tl = new TimelineMax();
  
  for ( let path of pathArr ) {
    
    var root = $("#stage-master")[0];
    var t = path.getTransformToElement(root);    
    
    var TL = new TimelineMax(),
        time = (parseInt(path.getTotalLength())/100)*multiplier,
        data = path.getAttribute('d'),
        bzPath = MorphSVGPlugin.pathDataToBezier(data, {
          matrix: [t.a, t.b, t.c, t.d, t.e, t.f]
        });   
    
    TL.fromTo(hand,0.2,{x: hand.x, y: hand.y, scaleX: 1.1, scaleY: 1.1},{x: bzPath[0].x, y: bzPath[0].y, scaleX: 1, scaleY: 1, ease: Power4.easeIn}, 0)
      .fromTo(path, time, {drawSVG: "0"}, {drawSVG: "100%"}, multiplier)
      .to(hand, time, {bezier:{values:bzPath, type:"cubic"}}, multiplier);
    tl.add(TL);
  }

  return tl;
}

function breakApartPaths(paths, svg) {
  
  var sepPaths = [];
   
  for (let path of paths ) {
    
    let beziers = MorphSVGPlugin.pathDataToRawBezier(path);
            
    if (beziers.length > 1) {
            
      for (let bezier of beziers) {       
        
        let data = ["M", bezier.shift(), bezier.shift(), "C", ...bezier].join(" ");
                
        let newPath = path.cloneNode(true);
        newPath.setAttribute("d", data);
        sepPaths.push(newPath);
      }     
    } else {
      sepPaths.push(path);
    }
  }
  
  return sepPaths;
}

function scaleBezier(scaleX,scaleY,bzPath) {
  var newPath = [];
  for ( let coords of bzPath ) {
    var X = coords.x*scaleX+$('#gp-1')[0]._gsTransform.x;
    var Y = coords.y*scaleY+$('#gp-1')[0]._gsTransform.y;
    newPath.push({x: X, y: Y});
  }
  return newPath;
}

function _breakApartPaths(paths, svg) {
  var sepPaths = [];
  for ( let path of paths ) {
    var pdata = path.getAttribute('d');
    pdata = pdata.trim();
    var subArr = pdata.split('M');
    for ( let M of subArr ) {
      if(M != "") {
        var newPath = document.createElementNS("http://www.w3.org/2000/svg", 'path');
        for ( var a = 0; a < path.attributes.length; a++ ) {
          var N = path.attributes[a].name;
          var V = path.attributes[a].value;
          if(N != 'd') {
            newPath.setAttributeNS('',N,V);
          }
        }
        newPath.setAttributeNS('','d',`M${M}`);
        sepPaths.push(newPath);
      }
    }
  }
  return sepPaths;
}

$('button.draw').on("click", function(){
  reset();
  var TL = drawIt();
  TL.play();
})

$('button.reset').on("click", function(){
  reset();
});
              
            
!
999px

Console