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

              
                <div class="App">
  <h1>Step 3</h1>

  <div class="Example">
    <div>3. setTimeout with delta fix (100ms)</div>
    <div class="Example-position"></div>
    <div class="Box"></div>
  </div>
</div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  background: #fdfdfd;
  font-family: Helvetica, Arial, sans-serif;
}

.App {
  position: relative;
  max-width: 400px;
  margin: 0 auto;
  padding: 40px 20px;
}

h1 {
  font-size: 1.2em;
  font-weight: bold;
  margin-bottom: 5px;
}

a {
  color: #409ad7;
  text-decoration: none;
  border-bottom: 1px solid #ddd;
  margin-bottom: 20px;
  display: inline-block;
  
  &:hover {
    border-bottom-color: #409ad7;
  }
}

.Example {
  border: 1px solid #ddd;
  border-radius: 3px;
  background: #fff;
  padding: 20px;
  margin-bottom: 10px;
}

.Example-position {
  margin-top: 5px;
  color: #409ad7;
}

.Box {
  width: 30px;
  height: 30px;
  background: #34ba9c;
  margin-top: 20px;
  border-radius: 2px;
}
              
            
!

JS

              
                const FRAME_DURATION = 1000 / 60; // 60fps frame duration
// If available we are using native "performance" API instead of "Date"
// It it's name suggests it is more performant, read more on MDN:
// https://developer.mozilla.org/en-US/docs/Web/API/Performance
const getTime = typeof performance === 'function' ? performance.now : Date.now;
const MAX_POSITION = 150;

const box = document.querySelector('.Box');
// Initial position
let position = 0;
// Initial time
let lastUpdate = getTime();

function animate() {
  const now = getTime();
  // This is the main part
  // We are checking how much time has passed since the last update
  // and translating that to frames
  const delta = (now - lastUpdate) / FRAME_DURATION;

  // Updating scene logic
  // We want to move the box 1px per each 16.66ms (60fps)
  // so we are multipling 1px with the number of frames passed
  position += 1 * delta;
  
  // Reset position
  if (position > MAX_POSITION) {
    position -= MAX_POSITION;
  }

  // Render updated scene
  box.style.transform = `translateX(${ position }px)`;

  // Update last updated time
  lastUpdate = now;

  // Fake 10fps using "setTimeout"
  setTimeout(animate, 100);
}

animate();
              
            
!
999px

Console