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

              
                <link href='https://fonts.googleapis.com/css?family=Asap:400,700' rel='stylesheet' type='text/css'>
<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="400" viewBox="0 0 800 400">
    <foreignobject class="node" x="0" y="0" width="800" height="400">
      <div id="type-container" width="800" height="400"></div>
    </foreignobject>
  </svg>
</div>
<div id="waveform"></div>
<div id="form-wrapper">
  <button id="play-single">DO SINGLE WORDS</button>
  <button id="play-phrases">DO PHRASES</button>
<textarea id="copy" name="copy" cols="60" rows="10">
Now in those days a decree went out from Caesar Augustus to register all the empire for taxes. This was the first registration, taken when Quirinius was governor of Syria. Everyone went to his own town to be registered. So Joseph also went up from the town of Nazareth in Galilee to Judea, to the city of David called Bethlehem, because he was of the house and family line of David. He went to be registered with Mary, who was promised in marriage to him, and who was expecting a child. While they were there, the time came for her to deliver her child. And she gave birth to her firstborn son and wrapped him in strips of cloth and laid him in a manger, because there was no place for them in the inn. Now there were shepherds nearby living out in the field, keeping guard over their flock at night. An angel of the Lord appeared to them, and the glory of the Lord shone around them, and they were absolutely terrified. But the angel said to them, “Do not be afraid! Listen carefully, for I proclaim to you good news that brings great joy to all the people: Today your Savior is born in the city of David. He is Christ the Lord. He is Christ... the Lord!! This will be a sign for you: You will find a baby wrapped in strips of cloth and lying in a manger.” Suddenly a vast, heavenly army appeared with the angel, praising God and saying, “Glory to God in the highest, and on earth peace among people with whom he is pleased!”
</textarea>
</div>
              
            
!

CSS

              
                body
  background-color: #333
  color: white
  
#main
  width: 800px
  height: 400px
  overflow: hidden

#type-container
  position: relative
  overflow: hidden
  display: flex
  flex-flow: row nowrap
  justify-content: center
  align-items: center
  width: 800px
  height: 400px
  background-color: #000
  
  h3
    position: absolute
    font-family: 'Asap', sans-serif
    font-weight: 700
    text-align: center
    visibility: hidden
    font-size: 56px
    -webkit-margin-before: 0
    -webkit-margin-after: 0


// NOTE! This is not ideal for Firefox because scaling isn't perfectly centered. 
// I am only interested in Chrome as this was designed to go into an Electron app
// I am also aware that there are issues in Edge and Safari. If you decide to use this
// you should be aware of these issues.
@-moz-document url-prefix()
  #type-container
    h3
      left: 0
      top: 0
      width: 800px
      height: 400px

h2 
  color: #9af600
  margin-left: 24px
  margin-bottom: 0px
li 
  font-family: 'Asap', sans-serif
  color: #999
  margin-bottom: 10px

textarea 
  display: block
  background: #003806
  color: burlywood
  font-size: 14px
  border: 2px solid burlywood
  padding: 10px
  width: 786px
  height: 200px

#waveform
  width: 800px
  
#form-wrapper
  position: absolute
  top: 540px
  left: 0

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

              
            
!

JS

              
                var container = $("#type-container"),
    _sentenceEndExp = /(\.|\?|!)$/g, //regular expression to sense punctuation that indicates the end of a sentence so that we can adjust timing accordingly
    sentences = "",
    wavesurfer = WaveSurfer.create({
      container: '#waveform',
      waveColor: 'violet',
      progressColor: 'purple'
    }),
    audioSettings = {currentVolume: 1, fadeTime: 5},
    letterSettings = {fontSize: '52px', maxPhraseLettersPerScreen: 110, pauseBetweenSentences: 0.6};

function machineGun(text) {
  var tl = new TimelineMax({delay:0.6, onStart: playAudio, onStartParams: [0], onComplete: fadeAudio}),
      words = text.split(" "),
      wordCount,
      time = 0,
      word, element, duration, isSentenceEnd, i;
  
  wordCount = words.length;
  $('#type-container h3').css('font-size',letterSettings.fontSize);
  for(i = 0; i < wordCount; i++){
    word = words[i];
    isSentenceEnd = _sentenceEndExp.test(word);
    element = $("<h3>" + word + "</h3>").appendTo(container);
    duration = Math.max(0.5, word.length * 0.08); //longer words take longer to read, so adjust timing. Minimum of 0.5 seconds.
    if (isSentenceEnd) {
      duration += 0.6; //if it's the last word in a sentence, drag out the timing a bit for a dramatic pause.
    }
    //set opacity and scale to 0 initially. We set z to 0.01 just to kick in 3D rendering in the browser which makes things render a bit more smoothly.
    TweenLite.set(element, {autoAlpha:0, scale:0, z:0.01});
    //the SlowMo ease is like an easeOutIn but it's configurable in terms of strength and how long the slope is linear. See https://www.greensock.com/v12/#slowmo and https://api.greensock.com/js/com/greensock/easing/SlowMo.html
    tl.to(element, duration, {scale:1.2,  ease:SlowMo.ease.config(0.25, 0.9)}, time)
      //notice the 3rd parameter of the SlowMo config is true in the following tween - that causes it to yoyo, meaning opacity (autoAlpha) will go up to 1 during the tween, and then back down to 0 at the end. 
		 	.to(element, duration, {autoAlpha:1, ease:SlowMo.ease.config(0.25, 0.9, true)}, time);
    time += duration - 0.05;
    if (isSentenceEnd) {
      time += letterSettings.pauseBetweenSentences; //at the end of a sentence, add a pause for dramatic effect.
    }
  }
}

function buildChunks(text, maxLength) {
  if (maxLength === undefined) {
    return text.split(" ");
  }
  var words = text.split(" "),
      wordCount = words.length,
      chunk = [],
      chunks = [], i;
  for (i = 0; i < wordCount; i++){
    chunk.push(words[i]);
    if (_sentenceEndExp.test(words[i]) || i === wordCount - 1 || chunk.join(" ").length + words[i+1].length > maxLength) {
      chunks.push(chunk.join(" "));
      chunk = [];
    }
  }
  return chunks;
}

function machineGunCombos(chunks, maxLength) {
  //in case "chunks" isn't an array, we'll build chunks automatically
  if (!(chunks instanceof Array)) {
    chunks = buildChunks(chunks, maxLength);
  }
  
  var tl = new TimelineMax({delay:0.6, onStart: playAudio, onStartParams: [0], onComplete: fadeAudio}),
      time = 0,
      chunk, element, duration, isSentenceEnd, i;
  
  for (i = 0; i < chunks.length; i++) {
    chunk = chunks[i];
    isSentenceEnd = _sentenceEndExp.test(chunk) || (i === chunks.length - 1);
    element = $("<h3>" + chunk + "</h3>").appendTo(container);
      duration = Math.max(0.5, chunk.length * 0.08); //longer words take longer to read, so adjust timing. Minimum of 0.5 seconds.
      if (isSentenceEnd) {
        duration += 0.6; //if it's the last word in a sentence, drag out the timing a bit for a dramatic pause.
      }
      //set opacity and scale to 0 initially. We set z to 0.01 just to kick in 3D rendering in the browser which makes things render a bit more smoothly.
    TweenLite.set(element, {autoAlpha:0, scale:0, z:0.01});
    //the SlowMo ease is like an easeOutIn but it's configurable in terms of strength and how long the slope is linear. See https://www.greensock.com/v12/#slowmo and https://api.greensock.com/js/com/greensock/easing/SlowMo.html
    tl.to(element, duration, {scale:1.2,  ease:SlowMo.ease.config(0.25, 0.9)}, time)
      //notice the 3rd parameter of the SlowMo config is true in the following tween - that causes it to yoyo, meaning opacity (autoAlpha) will go up to 1 during the tween, and then back down to 0 at the end. 
		  .to(element, duration, {autoAlpha:1, ease:SlowMo.ease.config(0.25, 0.9, true)}, time);
    time += duration - 0.05;
    if (isSentenceEnd) {
      time += 0.6; //at the end of a sentence, add a pause for dramatic effect.
    }
  }
  $('#type-container h3').css('font-size',letterSettings.fontSize);
}

function clearContent() {
  var containerContents = document.querySelectorAll('#type-container h3');
  for ( let content of containerContents ) {
    container[0].removeChild(content);
  }
}

function playAudio(location) {
  wavesurfer.seekTo(location);
  wavesurfer.play();
}

function stopAudio() {
  wavesurfer.stop();
}

function fadeAudio() {
  TweenLite.to(audioSettings, audioSettings.fadeTime, {
    currentVolume: 0,
    ease: Sine.easeOut,
    onUpdate: function(){
      wavesurfer.setVolume(audioSettings.currentVolume);
    },
    onComplete: function(){
      stopAudio();
      wavesurfer.setVolume(1);
      audioSettings.currentVolume = 1;
    }
  });
}

document.getElementById("play-single").onclick = function() {
  clearContent();
  sentences = $('#copy').val();
  machineGun(sentences);
}

document.getElementById("play-phrases").onclick = function() {
  clearContent();
  sentences = $('#copy').val();
  machineGunCombos(sentences, letterSettings.maxPhraseLettersPerScreen);
}

$(function(){
  console.clear();
  wavesurfer.load('https://s3-us-west-2.amazonaws.com/s.cdpn.io/81395/inspire-loop-2.mp3');
  wavesurfer.on('finish', function(){
    playAudio(0);
  });
});

/* learn more about the GreenSock Animation Platfrom (GSAP) for JS

https://www.greensock.com/gsap-js/

watch a quick video on how TimelineLite allows you to sequence animations like a pro
https://www.greensock.com/sequence-video/

*/

//Check out this enhanced version that intelligently groups words together: https://codepen.io/GreenSock/pen/sxdfe
              
            
!
999px

Console