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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <fieldset><legend>Options</legend>
  <textarea id="fonttext">Hello World!</textarea>

  <select id="fontselect">
    <option id="loading">Loading Fonts...</option>
  </select>
</fieldset>
<button>DRAW</button>
<div id="main" >
  <svg xmlns="http://www.w3.org/2000/svg" width="640" height="400"></svg>
</div>
              
            
!

CSS

              
                body {
  font-family: sans-serif;
}

fieldset {
  position: absolute;
  top: 20px;
  left: 720px;
  background-color: #fff;
}

fieldset input, fieldset select {
  margin-bottom: 1em;
  display: block;
}
#main {
  position: relative;
  display: block;
  padding-top: 20px;
  width: 640px;
}

#main svg {
  position: relative;
}

button {
  font-size: 16px;
  color: white;
  background: green;
  border: none;
  padding: 4px 15px;
  border-radius: 4px;
}
              
            
!

JS

              
                // Load the font JSON data
$.getJSON('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/hersheytext.min.json', function(fonts){

  // Font data is loaded!
  $('#loading').remove();

  // Populate font choices
  for (var id in fonts) {
    $('<option>').text(fonts[id].name).val(id).appendTo('#fontselect');
  }

  $('#fontselect').change(function(){
    $('#textexample').remove(); // Kill the old one (if any)

    // Render some text into the SVG area with it
    renderText($('#fonttext').val(), {
      font: fonts[$(this).val()],
      pos: {x: 0, y: 0},
      scale: 2,
      charWidth: 8,
      wrapWidth: 70,
      centerWidth: 200,
      target: '#main svg',
      id: 'textexample'
    });

    // REQUIRED: Refresh DOM to reinstate node status with XML namespaces
    // TODO: There must be a better way to do this! :P
    $('#main').html($('#main').html());
  }).attr('size', 10).change(); // Trigger initial run

  // Re-render on keypress
  $('input').on('input', function(e){
    $('#fontselect').change();
  });
});


/**
* Render a string of text in a Hershey engraving font to a given SVG element
*
* @param {string} s
*   Text string to be rendered
* @param {object} options
*   Object of named options:
*    font {obj}: [Required] Font object containing path elements for font
*    id {string}: [Required] ID to give the final g(roup) SVG DOM object
*    pos {object}: [Required] {X, Y} object of where to place the final object within the SVG
*    charWidth {int}: [Optional] Base width given to each character
*    charHeight {int}: [Optional] Base height given to each character (when wrapping)
*    scale {int}: [Optional] Scale to multiply size of everything by
*    wrapWidth {int}: [Optional] Max line size at which to wrap to the next line
*    centerWidth {int}: [Optional] Width to center multiline text inside of
*    centerHeight {int}: [Optional] Height to center text inside of vertically
*
* @returns {boolean}
*   Operates directly on the given target given in options, returns false if
*   required option missing or other failure.
*/
function renderText(s, options) {
  try {
    var font = options.font.chars;
    options.charWidth = options.charWidth ? options.charWidth : 10;
    options.charHeight = options.charHeight ? options.charHeight : 28;

    var offset = {left: 0, top: 0};
    options.scale = options.scale ? options.scale : 1;

    // Create central group
    var $group = $('<g>').attr({
      id: options.id,
      style: 'stroke:#000000; fill:none;',
      transform:
        'scale(' + options.scale + ') ' +
        'translate(' + options.pos.x + ',' + options.pos.y + ')'
    });
    $(options.target).prepend($group);

    // Initial Line container
    var lineCount = 0;
    var $groupLine = $('<g>').attr('id', options.id + '-line-' + lineCount);
    $group.prepend($groupLine);

    // Move through each word
    var words = s.split(' ');
    for(var w in words) {
      var word = words[w];

      // Move through each letter
      for(var i in word) {
        var index = word.charCodeAt(i) - 33;

        // Only print in range chars
        var charOffset = options.charWidth;
        if (font[index]){
          charOffset = font[index].o;

          // Add the char to the DOM
          $groupLine.prepend(
            $('<path>').attr({
              d: font[index].d,
              style: 'stroke: red; fill:none;',
              fill: 'none',
              transform: 'translate(' + offset.left + ', ' + offset.top + ')'
            })
          );
        }

        // Add space between
        offset.left+= charOffset + options.charWidth;
      }

      // Wrap words to width
      if (options.wrapWidth) {
         if (offset.left > options.wrapWidth) {
           if (options.centerWidth) {
             var c = (options.centerWidth / 2) - (offset.left / 2);
             $groupLine.attr('transform', 'translate(' + c + ',0)');
           }

           offset.left = 0;
           offset.top+= options.charHeight;

           // New Line container
           lineCount++;
           $groupLine = $('<g>').attr('id', options.id + '-line-' + lineCount);
           $group.prepend($groupLine);
         } else {
           offset.left+= options.charWidth*2; // Add regular space
         }
       } else {
         offset.left+= options.charWidth*2; // Add regular space
       }
     }

    if (options.centerWidth) {
      var c = (options.centerWidth / 2) - (offset.left / 2);
      $groupLine.attr('transform', 'translate(' + c + ',0)');
    }

    if (options.centerHeight) {
      var c = (options.centerHeight / 2) - ((options.charHeight*(lineCount+1)) / 2) + options.pos.y;
      $group.attr({
        transform:
        'scale(' + options.scale + ') ' +
        'translate(' + options.pos.x + ',' + c + ')'
      });
    }
  } catch(e) {
    console.error(e);
    return false; // Error!
  }

  return true; // We should be all good!
}

function drawText() {
  
  let paths = Array.from(document.querySelectorAll(`#main svg path`)),
      tl = new TimelineMax();
  
  paths = paths.reverse();
  
  tl.set(paths,{drawSVG: "0"});
  tl.staggerFromTo(paths, 1, {drawSVG: "0% 0%"}, {drawSVG: "100% 0%"},0.7,0);
  
}

$('button').on('click', drawText);
              
            
!
999px

Console