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

              
                <script id="worker" type="text/javascript-worker">
  self.addEventListener('message', async event => {
    const imageURL = event.data

    const response = await fetch(imageURL)
    const blob = await response.blob()

    // Send the image data to the UI thread!
    self.postMessage({
      imageURL: imageURL,
      blob: blob,
    })
  })
</script>

<body>
  <img data-src="https://source.unsplash.com/random?index=1">
  <img data-src="https://source.unsplash.com/random?index=2">
  <img data-src="https://source.unsplash.com/random?index=3">
  <img data-src="https://source.unsplash.com/random?index=4">
  <img data-src="https://source.unsplash.com/random?index=5">
  <img data-src="https://source.unsplash.com/random?index=6">
</body>

              
            
!

CSS

              
                body {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  max-height: 100vh;
  max-width: 100vw;
}

img {
  max-width: 100%;
}
              
            
!

JS

              
                /*
 * main.js
 */

// This is a workaround because Codepen was having some issues loading the
// JavaScript file from a separate pen. 🤷
const workerAsText = document.querySelector('#worker').textContent
const workerAsBlob = new Blob([workerAsText], { type: 'text/javascript' })
const ImageLoaderWorker = new Worker(URL.createObjectURL(workerAsBlob))

// This is how we would *normally* load the web worker
// const ImageLoaderWorker = new Worker('https://s.codepen.io/trezy/pen/dEBYXV.js')

const imgElements = document.querySelectorAll('img[data-src]')

ImageLoaderWorker.addEventListener('message', event => {
  // Grab the message data from the event
  const imageData = event.data

  console.log('We got a message back!', imageData)

  const imageElement = document.querySelector(`img[data-src='${imageData.imageURL}']`)

  const objectURL = URL.createObjectURL(imageData.blob)
  
  imageElement.setAttribute('src', objectURL)
  imageElement.removeAttribute('data-src')
})

imgElements.forEach(imageElement => {
  const imageURL = imageElement.getAttribute('data-src')
  ImageLoaderWorker.postMessage(imageURL)
})

              
            
!
999px

Console