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 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

              
                <div id="main">
  <svg id="stage" xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="800" height="600" viewBox="0 0 800 600">
    <foreignobject class="node" x="0" y="0" width="100%" height="100%">
      <div id="type-container" width="100%" height="100%"></div>
    </foreignobject>
  </svg>
</div>

<div id="form-wrapper">
  <button id="reset">RESET ANIMATION</button>
  <button id="play">PLAY</button>
<textarea id="copy" name="copy" id="" cols="40" rows="33">
No one
knows~
where the
wind
blows~

Unless
you're
standing
in a
windstorm!~
</textarea>
</div>

              
            
!

CSS

              
                body
  margin: 0
  overflow: scroll
  font-family: 'alpha_echoregular', sans-serif
  background: #350000
@font-face
  font-family: 'alpha_echoregular'
  src: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.eot')
  src: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.eot?#iefix') format("embedded-opentype"), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.woff') format("woff"), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.ttf') format("truetype"), url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/alpha_echo-webfont.svg#alpha_echoregular') format("svg")
  font-weight: normal
  font-style: normal

#main, #form-wrapper
  margin: 20px 0 0 20px
  padding: 0
  float: left
  display: block
  position: relative

#main
  padding-right: 0
#form-wrapper
  padding-left: 0
  
#stage
  background: black url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/grunge-paper-bg.jpg) no-repeat center center
  background-size: cover

#type-container
  width: 800px
  height: 600px
  display: flex
  flex-flow: row nowrap
  justify-content: space-between
  align-items: flex-start
  font-size: 34px
  // color: #5d0707
  color: #000000
  font-family: 'alpha_echoregular', sans-serif
  // opacity: 0
  // position: relative
  // line-height: 1.0
  .wrapper
    display: flex
    flex-flow: column nowrap
    align-items: flex-start
  .line
    width: auto
    text-transform: uppercase
    // line-height: 1.0em
  .word
    display: inline-block
    line-height: 1.0em
    vertical-align: bottom
    opacity: 0

#type-container > div 
  transform-origin: right center 0
  white-space: pre
  margin: 0

textarea 
  display: block
  background: #2f0000
  color: antiquewhite
  font-size: 14px
  border: 4px solid burlywood
  height: 553px

button 
  background: green
  box-shadow: none
  color: white
  border: none
  padding: 6px 20px
  border-radius: 5px
  margin: 0 0 10px 0
  
              
            
!

JS

              
                // create global vars
var masterTL = new TimelineMax({paused: true}),
    stage = $('#stage'),
    copy = $('#copy'),
    typoContainer = document.getElementById('type-container'),
    phraseGroups = [],
    rotateArr = [],
    coordMap = [],
    spanMap = [],
    linePadding = 0.2, // 10% on either side of the current line is default
    stageDims = {WD: parseInt(stage.width()), HT: parseInt(stage.height())},
    speed = 0.5,
    extenderMultiplier = 0.15;

rotateArr[0] = [0,"-90_ccw","-90_ccw"]; // needs to be all ccw or all cw
rotateArr[1] = [0,"90_cw","90_cw"]; // needs to be all ccw or all cw
rotateArr[2] = [0,"90_cw"]; // needs to be all ccw or all cw
rotateArr[3] = ["90_cw",0]; // needs to be all ccw or all cw
// TweenMax.set('#type-container',{opacity: 0});

function createWordMap() {
  document.getElementById('type-container').innerHTML = "";
  phraseGroups = breakIntoGroups();
  // wrapper-counter, line-counter, word-counter,  
  var wCnt = 0, lCnt = 0, wdCnt = 0; // all id's will start at 1 so increment each before
  
  // cycle through groupings
  for ( let group of phraseGroups ) {
    wCnt++; // starts at 1
    lCnt = 0;
    
    var wrapper = document.createElement('div');
    wrapper.setAttribute('class','wrapper');
    wrapper.setAttribute('id',`wrap-${wCnt}`);
    typoContainer.appendChild(wrapper);

    // cycle through lines in this group
    var lines = breakIntoLines(group);
    for( let line of lines ) {
      lCnt++;
      if(line == "" || line == " ") return;
      wdCnt = 0;
    
      var thisLine = document.createElement('div');
      thisLine.setAttribute('class', 'line');
      thisLine.setAttribute('id', `line-${lCnt}`);
      wrapper.appendChild(thisLine);
      
      // cycle through the words in this line
      var words = breakIntoWords(line);
      for( let word of words ) {
        
        // extra time called for at end of this word?
        var className = word.indexOf('~') !== -1 ? 'word extended' : 'word';
        
        wdCnt++;
        if(word == "" || word == " ") break;

        if( wdCnt < words.length ) {
          word += " ";
        }

        var span = document.createElement('span');
        span.setAttribute('class',className);
        span.setAttribute('id',`word-${wdCnt}`);
        span.innerHTML = word;
        thisLine.appendChild(span);
      }
    }
  }
  buildCoordMap();
}

function buildCoordMap() {
  
  // loop through wrappers
  $('.wrapper').each(function(){
    // reset the obj vars each loop
    var wrapObj = {}, lineArr = []; // for storing all wrapper/lines/spans data
    // drop the data into the wrapObj
    wrapObj = positionData($(this),'wrap');
    
    // loop through .line elements (lines)
    $(this).find('.line').each(function(){
      
      var line = {};
      var lineData = positionData($(this),'line');
      line.data = lineData;
      
      var words = [];
      // loop through the .word span elements
      $(this).find('.word').each(function(){
        
        // push this word span's data into spanArr
        var wordData = positionData($(this),'word');
        words.push(wordData);
      });
      line.words = words;
      lineArr.push(line);
    });
    coordMap.push({wrapObj,lineArr});
  });
  buildTweens();
}

function buildTweens() {
  var xpad = 10;
  var ypad = 2;
  
  var tl = new TimelineMax({paused: true});
  var labelCounter = 0;
  for ( let wrap of coordMap ) {
    // each item will contain: { ht: float, item: jQueryObj, wd: float, x: int, y: int}
    // lineArr[].data{}, lineArr[].words[], wrapObj{}
    var longestLine = 0, // reset
        lastLine,
        time = 0;
    // loop through each line
    for ( let line of wrap.lineArr ) {
      // will contain line.data{} and line.words[]
      // first zoom the viewbox to the first line
      longestLine = line.data.wd > line.wd ? line.data.wd : line.wd;
      lastLine = line;
      var vbox = `${line.data.x-xpad} ${line.data.y-ypad} ${line.data.wd+xpad} ${line.data.ht+ypad}`;
      // tl.to(stage,0.5,{attr:{viewBox: vbox}});
      
      // loop through each word and set viewbox zooms
      for ( let word of line.words ) {

        var vboxTween, wordTween, letters, pause = 0;
        letters = $(word.item).html().length;
        if( $(word.item).hasClass('extended') ) {
          pause = letters*extenderMultiplier;
        }
        console.log(pause);
        var wvbox = `${word.x-xpad} ${line.data.y-ypad} ${line.data.wd+xpad} ${line.data.ht+ypad}`;
        masterTL.to(word.item, 0.01, {opacity: 1})
                .to(stage,speed,{attr:{viewBox: wvbox}});
        if(pause>0) {
          masterTL.to(stage,pause,{attr:{viewBox: `${word.x-(xpad*2)} ${line.data.y-(ypad*2)} ${line.data.wd+(xpad*4)} ${line.data.ht+(ypad*4)}`}});
        }
      }
    }
    console.log(wrap.wrapObj.item);
    // masterTL.to(wrap.wrapObj.item,0.5,{opacity: 0});
  }
  // masterTL.add(tl,0);
}

function hideWrapperWords(wrapper) {
  // wrapper is jQuery obj
  TweenMax.set(wrapper.find('.word'),{opacity: 0});
}

$(function(){
  createWordMap();
});

function breakIntoLines(group) {
  return group.split(/\n/);
}

function breakIntoGroups() {
  return copy.val().trim().split(/\n\n/);
}

function breakIntoWords(line) {
  return $(line.trim().split(" "));
}

function positionData(el,type) {
  // el is a jQuery obj
  var id = el.attr('id'), selector;
  switch(type) {
    case "wrap":
      selector = `#${typoContainer.id} #${id}`;
      break;
    case "line":
      selector = `#${typoContainer.id} #${el[0].parentElement.id} #${id}`;
      break;
    case "word":
      var parentLine = el[0].parentElement.id;
      var parentWrap = el[0].parentElement.parentElement.id;
      selector = `#${typoContainer.id} #${parentWrap} #${parentLine} #${id}`;
      break;
  }
  var pos = el.position();
  var WD = el.width();
  var HT = el.height();
  return { item: selector, x: Math.floor(pos.left), y: Math.floor(pos.top), wd: parseInt(WD), ht: parseInt(HT) };
}

document.getElementById("reset").onclick = function() {
  coordMap = [];
  TweenMax.set(stage,{attr:{viewBox: '0 0 800 600'}});
  TweenMax.set('.wrapper',{opacity: 1});
  masterTL.seek(0);
  masterTL.clear();
  createWordMap();
}

document.getElementById("play").onclick = function() {
  var kids = masterTL.getChildren(false, true, true, 0);
  for ( let kid of kids ) {
    kid.paused(false);
  }
  masterTL.play();
}

function randomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min;
}
              
            
!
999px

Console