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" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0" y="0" width="800" height="600" viewBox="0 0 800 600">
    <g id="canvas" font-family="alpha_echoregular" fill="#ffffff"></g>
  </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

              
                @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

body
  font-family: 'alpha_echoregular', sans-serif
  background: black

#main
  margin: 0
  width: 800px
  height: 600px
  position: absolute
  left: 0
  top: 0

#form-wrapper
  position: absolute
  left: 820px
  top: 20px
#stage
  background: #a66bbe

button 
  background: green
  box-shadow: none
  color: white
  border: none
  padding: 6px 20px
  border-radius: 5px
  margin: 0 0 10px 0
  
tspan
  line-height: 1.0em
  alignment-baseline: hanging

textarea 
  display: block
  background: #2c0a3a
  color: antiquewhite
  font-size: 14px
  border: 4px solid burlywood
  height: 350px
              
            
!

JS

              
                function setType() {
  var stage = document.getElementById('stage'),
      canvas = document.getElementById('canvas'),
      fontRange = [18,50],
      colors = ["#008000", "#0000ff", "#4b0082", "#9400d3"],
      txtCnt = 0,
      wordArr = [],
      pattern = [ // thisPosition and matchPosition are based on their respective x/y coords (tl = topLeft, br = bottomRight, etc)
        {thisPosition: 'tl', matchPosition: null, rotation: null, origin: 'left top', textAlign: 'start', offsetX: 0, offsetY: 0, multiplier: 1}, // this one is pretty much a dummy until later maybe
        {thisPosition: 'tl', matchPosition: 'br', rotation: 90, origin: 'left top', textAlign: 'start', offsetX: 'lastLineEnd', offsetY: 0, multiplier: -1}, // lastLineEnd = prev.lastLine.width
        {thisPosition: 'tl', matchPosition: 'br', rotation: null, origin: 'left top', textAlign: 'start', offsetX: 0, offsetY: 'FirstLineLastWordTop', multiplier: 1}, // lastWordTop = this.y - prev.firstLine.lastWord.y (should be a negative number already)
        {thisPosition: 'tl', matchPosition: 'br', rotation: null, origin: 'left top', textAlign: 'end', offsetX: 'LastLineLastWordStart', offsetY: 0, multiplier: 1}, // lastWordStart = (prev.x + prev.lastLine.width) - prev.lastLine.lastWord.width
        {thisPosition: 'bl', matchPosition: 'tr', rotation: 90, origin: 'left bottom', textAlign: 'start', offsetX: 0, offsetY: 0, multiplier: 1},
        {thisPosition: 'tl', matchPosition: 'br', rotation: -90, origin: 'left top', textAlign: 'start', offsetX: 0, offsetY: 0, multiplier: 1},
        {thisPosition: 'tl', matchPosition: 'br', rotation: null, origin: 'left top', textAlign: 'start', offsetX: 0, offsetY: 'LastLineLastWordEnd', multiplier: -1}, // prev.lastLine.height
      ];

  function createWordMatrix() {

    var sections = breakIntoGroups();
    var grpCnt = 0;

    for ( let section of sections ) {

      var group = document.createElementNS('http://www.w3.org/2000/svg', 'g');
      // var fontSize = 24;
      var fontSize = randomInt(fontRange[0],fontRange[1]);
      group.setAttribute('font-size',fontSize);
      group.setAttribute('id',`group-${grpCnt}`);
      group.setAttribute('class', 'text-group');
      canvas.appendChild(group);
      grpCnt++;

      var lines = breakIntoLines(section);
      var dims = null; // for knowing the dimensions and location of the previous text element
      for ( let line of lines ) {

        line = line.trim();

        var color = randomInt(0,colors.length-1);
        var T = document.createElementNS('http://www.w3.org/2000/svg', 'text');
        T.setAttribute('class',`text-${txtCnt}`);
        T.setAttribute('font-size',fontSize);
        T.setAttribute('fill', colors[color]);
        group.appendChild(T);

        if( dims == null ) {
          dims = {x: 0, y: fontSize/2};
        }

        var words = line.split(" ");
        var wordCnt = 0;
        for ( word of words ) {
          word = wordCnt < words.length-1 ? word + " " : word;
          var TS = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
          TS.setAttribute('alignment-baseline','hanging');
          TS.innerHTML = word;
          T.appendChild(TS);
          wordCnt++;
        }


        var pos = $(T).position();
        var HT = parseInt($(T).height());
        var WD = parseInt($(T).width());

        dims = { el: T, x: 0, y: pos.top + HT, ht: HT, wd: WD };
        wordArr.push(dims);
        txtCnt++;
      }
    }
  }

  function spaceLines() {

    $('#canvas g').each(function(){
      var G = $(this),
          cnt = 0,
          fntSize = G.attr('font-size');

      G.find('text').each(function(){
        if(cnt > 0 ) {
          var ts = $(this).find('tspan');
          ts.attr('y',fntSize*cnt);
        }
        cnt++;
      });
    });
  }

  function positionGroups() {

    var gNum = 1, // group number
        pNum = 1, // pattern array iterate
        defaultFontSize = canvas.getAttribute('font-size');

    // set the first group at x: 10, y; 10
    TweenMax.set('#canvas g#group-0',{x: 10, y: 10});

    $('#canvas g#group-0').nextAll().each(function(){

      var curr = $(this),
          prev = $(this).prev(),
          S = pattern[pNum], // settings from pattern[pNum];
          pBox = prev[0].getBoundingClientRect(),
          cBox = curr[0].getBoundingClientRect(),
          pGS = prev[0]._gsTransform,
          topPad = (curr[0].getAttribute('font-size')/4) >= 0 ? (curr[0].getAttribute('font-size')/3) : 0,
          id = curr.attr('id');

      var lines = document.querySelectorAll(`g#${id} text`);
      for ( let line of lines ) {
        line.setAttribute('text-anchor', S.textAlign);
        var lineBox = line.getBoundingClientRect();
      }

      switch(S.matchPosition) {
        case 'br':
            console.log(canvas._gsTransform);
            // check for rotation
            if( S.rotation == null ) {
              TweenMax.set(curr,{x: pBox.right, y: pBox.bottom});
            } else {
              TweenMax.set(curr,{x: pBox.right, y: pBox.bottom+topPad, rotation: S.rotation, transformOrigin: S.origin});
            }
          break;
        case 'tr':
            // check for rotation
            if( S.rotation == null ) {
              TweenMax.set(curr,{x: pBox.right, y: pBox.top});
            } else {
              TweenMax.set(curr,{x: pBox.right, y: pBox.top, rotation: S.rotation, transformOrigin: S.origin});
            }
          break;
      }
      pNum++;
    });
  }

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

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

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

  document.getElementById("reset").onclick = function() {
    wordArr = [];
    txtCnt = 0;
    tl.seek(0).pause().clear();
    TweenMax.set(canvas,{scale: 1});
    var groupElements = document.querySelectorAll('g.text-group');
    for ( let group of groupElements ) {
      canvas.removeChild(group);
    }
    init();
  }

  document.getElementById("play").onclick = function() {
    tl.play();
  }

  function randomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
  }
  
  function init() {
    console.clear();
    var dfd = $.Deferred();
    dfd
      .done(createWordMatrix,spaceLines,positionGroups)
      .done(function(){
      console.log('all done');
    });
    dfd.resolve();
  }
  init();
}

setType();

              
            
!
999px

Console