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.

Details

Privacy

Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO

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.

Template

Make Template?

Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.

Template URL

Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:

Screenshot

Screenshot or Custom Thumbnail

Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.

Upgrade to PRO

HTML

              
                <div class="demo">
  <div class="box red" id="redBox"></div>
  <div class="box blue" id="blueBox"></div>
</div>
<div class="nav">
  <button id="restart">restart</button> 
</div>
<div id="slider"></div>
<div>
              
            
!

CSS

              
                body {
  font-family:sans-serif;
}

.demo {
  width:600px;
  height:400px;
  background-color:black;
}

.box {
  width:50px;
  height:50px;
  position:absolute;
  visibility:hidden;
}

.red {
  background-color:red;
}

.blue {
  background-color:blue;
}

.nav {
  width:600px;
  background-color:#ccc;
  text-align:right;
}

button {
  padding:10px;
  margin:10px;
}

#slider{
    width: 600px;
    height:15px;
    margin:15px 0px 8px 6px;   
}
              
            
!

JS

              
                // preparation

var keyFramesRed = [
  {x:100,y:100,t:2}, // t equals time (not duration)
  {x:400,y:200,t:3},
  {x:300,y:300,t:4},
  {x:200,y:200,t:5},
  {x:500,y:100,t:6}
];
  
var keyFramesBlue = [
  {x:400,y:300,t:3},
  {x:200,y:200,t:5},
  {x: 50,y:300,t:7}
];

var curviness = 0.5; // 0 -> none, 1 -> max
var pointsPerSec = 10;
var pointLength = 1 / pointsPerSec; // should be globalStepSize (see function 'getCurvePoints' below)
var curvePointsRed = getCurvePoints(keyFramesRed, pointsPerSec, curviness);
var curvePointsBlue = getCurvePoints(keyFramesBlue, pointsPerSec, curviness);

console.log(curvePointsBlue);

// setting up the timeline(s)

TweenLite.defaultEase = Linear.easeNone;

var tlRed = new TimelineLite();
for (var i = 0; i < curvePointsRed.length; i++) {
  tlRed.add(TweenLite.to("#redBox", pointLength, curvePointsRed[i]));
}

var tlBlue = new TimelineLite();
for (var i = 0; i < curvePointsBlue.length; i++) {
  tlBlue.add(TweenLite.to("#blueBox", pointLength, curvePointsBlue[i]));
}

var tlMain = new TimelineLite({onUpdate:updateSlider});
tlMain.add(tlRed, keyFramesRed[0].t);
tlMain.add(TweenLite.to("#redBox", 0.0001, curvePointsRed[0]), keyFramesRed[0].t - 0.001);
tlMain.add(TweenLite.to("#redBox", 0.0001, {visibility:"visible"}), keyFramesRed[0].t);
tlMain.add(TweenLite.to("#redBox", 0.0001, {visibility:"hidden"}), keyFramesRed[keyFramesRed.length-1].t);

tlMain.add(tlBlue, keyFramesBlue[0].t);
tlMain.add(TweenLite.to("#blueBox", 0.0001, curvePointsBlue[0]), keyFramesRed[0].t - 0.001);
tlMain.add(TweenLite.to("#blueBox", 0.0001, {visibility:"visible"}), keyFramesBlue[0].t);
tlMain.add(TweenLite.to("#blueBox", 0.0001, {visibility:"hidden"}), keyFramesBlue[keyFramesBlue.length-1].t);

var tlMainDuration = 8;
tlMain.set({},{}, "+=" + (tlMainDuration - Math.max(keyFramesRed[keyFramesRed.length-1].t, keyFramesBlue[keyFramesBlue.length-1].t)))

// user interface

function updateSlider() {
  $("#slider").slider("value", tlMain.progress() *100);
}

$( "#slider" ).slider({
  range: false,
  min: 0,
  max: 100,
  step:.1,
  slide: function ( event, ui ) {
    tlMain.progress( ui.value/100 ).pause();
  }
}); 

$("#restart").on("click", function() {
  tlMain.restart();
})

// calculation

function getCurvePoints(keyframes, pointsPerSec, curviness) {
  var points = [];
  var lastIdx = Math.max(keyframes.length-1, 0);
  var totalDuration = keyframes[lastIdx].t - keyframes[0].t;
  var totalDivisions = Math.floor(totalDuration * pointsPerSec); // equals number of points
  var globalStepSize = totalDuration / totalDivisions; // almost '1 / pointsPerSec' but not quite,
                                                       // this makes sure that the last step ends
                                                       // exaclty on the last keyframe
  var offsetGlobal = 0; // stepsize minus remainder from previous curve segment
  
  for(var i = 0; i < lastIdx; i++) {
    var current   = keyframes[i];
    var next      = keyframes[i+1];
    var previous  = i > 0 ? keyframes[i-1] : current;
    var afterNext = i < lastIdx-1 ? keyframes[i+2] : next;
    
    var cpStart = {
      x:current.x + (next.x - previous.x) * 0.5 * curviness, // make sure that path passes through keyframes
      y:current.y + (next.y - previous.y) * 0.5 * curviness  // by creating additional control points for qubic bezier segment
    };
    var cpEnd = {
      x:next.x - (afterNext.x - current.x) * 0.5 * curviness, // control points are some kind of 'tangent' made up from 
      y:next.y - (afterNext.y - current.y) * 0.5 * curviness  // direction vector of neighboring keyframes
    };
    
    var segmentDuration = next.t - current.t;
    var segmentDivisions = Math.ceil(segmentDuration / globalStepSize);
    var localStepSize = globalStepSize / segmentDuration;
    var offsetLocal = offsetGlobal / segmentDuration;
    console.log(localStepSize + " " + segmentDivisions + " " + offsetLocal);
    for (var t = 0; t < segmentDivisions; t++) {
      points.push({
        x:bezier(current.x, cpStart.x, cpEnd.x, next.x, offsetLocal + t * localStepSize),
        y:bezier(current.y, cpStart.y, cpEnd.y, next.y, offsetLocal + t * localStepSize)
      });
    }

    offsetGlobal = (offsetGlobal + (segmentDivisions * globalStepSize - segmentDuration)) % globalStepSize;
  }
  return points;
}

function bezier(a, b, c, d, t) {
  return  (1 - t) * (1 - t) * (1 - t)              * a
    + 3 * (1 - t) * (1 - t)           * t          * b
    + 3 * (1 - t) *                    (t * t)     * c
    +                                  (t * t * t) * d;
}
              
            
!
999px
We'd ask you for an infinite loop joke, but we'd never hear the end of it.

Console