123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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>
            
          
!
            
              * {
  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;
}
            
          
!
            
              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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console