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

Auto Save

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

              
                <article>
  <header>
    <h1>Half of U.S. young adults want to &#8216;fast&#8212;forward&#8217; by 10 years, out of fear.</h1>
    <p>By Ben Norman, 10/12/2016</p>
  </header>
  <main>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pulvinar sit amet turpis sit amet gravida. Duis sit amet pulvinar turpis. Proin tempor ex in enim faucibus pellentesque nec at neque. Sed eget justo malesuada nisl laoreet rutrum. Praesent suscipit euismod nunc vel imperdiet. Mauris ac odio metus.</p>
    <p>Etiam tempor ornare mattis. Pellentesque fringilla fringilla eros quis dignissim. Morbi tristique ultrices diam eget hendrerit.</p>
    <p>Nullam non magna pharetra eros iaculis euismod. Ut at libero ac leo mattis mollis. Praesent sodales maximus orci ac fringilla. Morbi dolor augue, condimentum cursus neque ut, vulputate molestie metus. Curabitur vestibulum quam eget faucibus sollicitudin. Aliquam a ligula id ante cursus semper imperdiet in tortor.</p>
    <p>Curabitur molestie lorem a nunc posuere cursus. Curabitur posuere venenatis ligula, vel dignissim turpis vehicula in. Sed at pretium est.</p>
    <figure>
      <img src="https://leibrug.pl/img/posts/enhance-your-media-with-canvas-over-technique/canvas-over-concerned-man.jpg">
      <canvas id="canvas"></canvas>
      <figcaption>American young adults face many doubts at their early&#8212;twenties.</figcaption>
    </figure>
    <p>Vestibulum scelerisque at diam nec commodo. Mauris sodales leo ac tincidunt blandit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Pellentesque diam odio, pretium ornare justo a, euismod tempor sem. Nunc nec sagittis nibh, ac bibendum odio. Nulla facilisi.</p>
    <p>Vestibulum commodo viverra lacus, nec ultrices lacus accumsan eget. In egestas porttitor diam a varius. Duis porta vel sem non imperdiet. Fusce vel risus fermentum, tempor est molestie, egestas nunc. Nulla ac urna sit amet tellus porta interdum. Aliquam lacinia felis vitae finibus sollicitudin.</p>
  </main>
  <footer>
    <a href="https://www.flickr.com/photos/lentzstudios/4361222379/">"nothing like a profile"</a> by <a href="https://www.flickr.com/photos/lentzstudios/">Teeejayy</a> / <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA</a>
  </footer>
</article>
              
            
!

CSS

              
                body,
figure {
  margin: 0;
}
article {
  max-width: 60em;
  margin: 0 auto;
  padding: 2em;
}
main,
footer {
  font-family: serif;
  font-size: 18px;
}
main {
  line-height: 1.5;
  -moz-columns: 2;
  columns: 2;
  -moz-column-gap: 2em;
  column-gap: 2em;
}
h1 {
  font-size: 36px;
  margin: 0 0 .25em;
}
header p {
  font-size: 24px;
}
p,
figcaption {
  margin: 0 0 1em;
}
figure {
  position: relative;
}
img {
  width: 100%;
  height: auto;
}
canvas {
  position: absolute;
  top: 0;
  left: 0;
}
footer {
  border-top: 1px #000 solid;
  margin-top: 1em;
  padding-top: 2em;
}
              
            
!

JS

              
                // Config
var cfg = {
  textStyle: '14px sans-serif',
  textColor: '#fff',
  lineSpacing: 30,
  positionX: 30,
  positionY: 40,
  moveDuration: 500,
  moveRepeat: 1000
};

// Doubts list
var doubts = [
  'Rent or own a house?',
  'How will I pay my college loan?',
  'Should I move for the better job?',
  'Will she support me?',
  'How can I help my parents?'
];

// Refs
var canvas = document.getElementById('canvas');
var image = canvas.previousElementSibling;
var ctx = canvas.getContext('2d');

// Gradient mask
var maskHeight = cfg.positionY + doubts.length * cfg.lineSpacing + parseInt(cfg.textStyle);
var mask = ctx.createLinearGradient(0, 0, 0, maskHeight);
mask.addColorStop(0.15, '#000');
mask.addColorStop(0.45, 'transparent');
mask.addColorStop(0.55, 'transparent');
mask.addColorStop(0.85, '#000');

function drawMask() {
  ctx.globalCompositeOperation = 'destination-out';
  ctx.fillStyle = mask;
  ctx.fillRect(0, 0, canvas.width, maskHeight);

  // Reset
  ctx.globalCompositeOperation = 'source-over';
  ctx.fillStyle = cfg.textColor;
}

// Loop
var lastMoveTimeStamp = 0;

function draw(timeStamp) {
  var delta = timeStamp - lastMoveTimeStamp;

  // First run
  if (lastMoveTimeStamp === 0) {
    lastMoveTimeStamp = timeStamp;
    doubts.push(doubts[0]);
  }

  // Calculate text offset in this frame
  var textOffsetY = 0;
  if (delta >= cfg.moveRepeat) {
    textOffsetY = -Math.min((delta - cfg.moveRepeat) / cfg.moveDuration * cfg.lineSpacing, cfg.lineSpacing);
  }

  // Draw all
  ctx.clearRect(0, 0, canvas.width, maskHeight);
  for (var i = 0; i < doubts.length; i++) {
    ctx.fillText(doubts[i], cfg.positionX, i * cfg.lineSpacing + cfg.positionY + textOffsetY);
  }
  drawMask();

  // End of move
  if (delta >= cfg.moveRepeat + cfg.moveDuration) {
    lastMoveTimeStamp = timeStamp;
    doubts.shift();
    doubts.push(doubts[0]);
  }

  window.requestAnimationFrame(draw);
}

// Init
image.addEventListener('load', function() {
  canvas.width = image.width;
  canvas.height = image.height;
  ctx.font = cfg.textStyle;
  ctx.fillStyle = cfg.textColor;
  window.requestAnimationFrame(draw);
}, false);
              
            
!
999px

Console