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

              
                <figure class="gallery">
  <div class="gallery__frame displaytext loading">
    loading mobilenet neural network
  </div>
  <figcaption class="gallery__caption">
    <div class="gallery__caption__inner"></div>
  </figcaption>
  <div class="controls">
    <div class="controls__buttons">
      <button class="controls__btn" data-topic="azure">Azure</button> +
      <button class="controls__btn" data-topic="puppy">Puppies</button> +
      <button class="controls__btn" data-topic="happy">I'm Fine</button>
    </div>
    <form id="search" class="controls__search" action="#">
      <label class="visually-hidden" for="search__field">Custom topic:</label>
      <input class="search__field" id="search__field" placeholder="Custom topic" name="topic">
      <button class="controls__btn controls__btn--search">Search</button>
    </form>
  </div>
</figure>

              
            
!

CSS

              
                @keyframes fade-up {
  to {
    opacity: 1;
  }
}

@keyframes slide-up {
  to {
    transform: translateY(-100%);
  }
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.visually-hidden {
  position: absolute;
  clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
  clip: rect(1px, 1px, 1px, 1px);
  padding: 0;
  border: 0;
  height: 1px;
  width: 1px;
  overflow: hidden;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}

.displaytext {
  font-variant: small-caps;
  text-transform: lowercase;
  letter-spacing: 2px;
}

.gallery {
  display: grid;
  grid-template-areas:
    "frame"
    "controls";
  grid-template-rows: 1fr auto;

  overflow: hidden;
  height: 100vh;
  margin: 0;
  background: #000;
  color: #ccc;
}

.gallery__frame {
  grid-area: frame;

  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  overflow: hidden;
}

.gallery__frame.loading:after {
  transform: translateZ(0);
  animation: spin 1.1s infinite linear;

  content: "";
  width: 5em;
  height: 5em;
  margin: 60px auto;
  border: 0.35em solid #333;
  border-left: 0.35em solid #999;
  border-radius: 50%;
}

.gallery__image {
  opacity: 0;
  animation: 1s fade-up forwards;

  height: 100%;
}

.gallery__caption {
  grid-area: frame;

  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
}

.gallery__caption__inner {
  transform: translateY(0);
  animation: 0.5s slide-up forwards;

  position: absolute;
  left: 0;
  top: 100%;
  width: 100%;
  background: #000000aa;
  color: #eee;
}

.gallery__caption__labels {
  display: grid;
  grid-template-columns: auto 1fr;

  max-width: 960px;
  margin: auto;
  padding: 10px;
}

.gallery__caption__labels dt {
  text-align: right;
}

.gallery__caption__labels dd {
  margin: 0 0 0 10px;
}

.controls {
  grid-template-area: controls;

  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-content: center;

  width: 100%;
  max-width: 960px;
  margin: auto;
  padding: 10px;
  line-height: 28px;
  background: #222;
}

.controls__buttons {
  text-align: center;
}

.controls__btn {
  transition: 0.25s background-color, 0.25s color;

  padding: 0 5px;
  border: 1px solid currentColor;
  border-radius: 3px;
  font: inherit;
  cursor: pointer;
  background-color: #666;
  color: #000;
}

.controls__btn:hover,
.controls__btn:focus,
.controls__btn.active {
  background-color: #000;
  color: #666;
}

.controls__search {
  display: flex;
  justify-content: center;
  align-items: center;

  margin-top: 10px;
}

.search__field {
  padding: 0 10px;
  border: 1px solid #666;
  border-right: none;
  border-top-left-radius: 3px;
  border-bottom-left-radius: 3px;
  font: inherit;
  line-height: inherit;
  outline: none;
  background: #111;
  color: #fff;
}

.search__field:focus {
  border-color: #fff;
}

.controls__btn--search {
  border: 1px solid #666;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

.controls__btn--search:focus {
  border-color: #666;
}

@media (min-width: 600px) {
  .controls {
    flex-direction: row;
  }

  .controls__buttons {
    text-align: left;
  }

  .controls__search {
    margin-top: 0;
  }
}
              
            
!

JS

              
                class GalleryUI {
  constructor(props) {
    this.props = props;

    this.frame = document.querySelector(".gallery__frame");
    this.caption = document.querySelector(".gallery__caption");
    this.btns = [...document.querySelectorAll("[data-topic]")];
    this.search = document.querySelector("#search");

    this.loadImage = this.loadImage.bind(this);
    this._onSearch = this._onSearch.bind(this);
    this._onBtnClick = this._onBtnClick.bind(this);

    this.search.addEventListener("submit", this._onSearch);
    this.btns.forEach(btn => btn.addEventListener("click", this._onBtnClick));
  }

  // Event handlers
  _onSearch(e) {
    e.preventDefault();
    this.props.onTopicSelect(e.target.topic.value);
  }

  _onBtnClick(e) {
    this.props.onTopicSelect(e.target.dataset.topic);
  }

  // Internal methods
  _updateBtns(topic) {
    this.btns.forEach(btn => {
      btn.dataset.topic === topic
        ? btn.classList.add("active")
      : btn.classList.remove("active");
    });
  }

  _updateSearch(topic) {
    if (this.search.topic.value != topic) {
      this.search.topic.value = "";
    }
  }

  _createImg(blob) {
    const img = document.createElement("img");
    img.className = "gallery__image";
    img.src = URL.createObjectURL(blob);
    
    // Need to wait for onload event
    return new Promise(resolve => {
      img.onload = () => resolve(img)
    })
  }

  _makeLabels(predictions) {
    const getLabel = prediction => {
      const percent = (prediction.probability * 100).toFixed(2);
      const dt = `<dt>${percent}%</dt>`;
      const dd = `<dd>${prediction.className}</dd>`;

      return `${dt} ${dd}`;
    };

    const labels = document.createElement("dl");
    labels.className = "gallery__caption__labels";
    labels.innerHTML = predictions.map(getLabel).join("");

    const inner = document.createElement("div");
    inner.className = "gallery__caption__inner";
    inner.appendChild(labels);

    return inner;
  }

  // Public API
  reset(topic) {
    this.frame.innerHTML = `loading ${topic} image from Unsplash`;
    this.frame.classList.add("loading");
    this.caption.innerHTML = "";
    this._updateBtns(topic);
    this._updateSearch(topic);
  }

  async loadImage(blob) {
    this.img = await this._createImg(blob);

    this.frame.classList.remove("loading");
    this.frame.innerHTML = "";
    this.frame.appendChild(this.img);

    return this.img;
  }

  showPredictions(predictions) {
    this.img.alt = predictions.map(p => p.className).join(", ");
    this.caption.appendChild(this._makeLabels(predictions));
  }

  autoLoad(index = 0) {
    this.btns[index].click();
  }
}

class Gallery {
  constructor({ model, endpoint, initialTopic }) {
    this.model = model;
    this.getEndpoint = endpoint;

    this.state = { loading: false };
    this.ui = new GalleryUI({
      onTopicSelect: this.onTopicSelect.bind(this)
    });

    this.onTopicSelect(initialTopic);
  }

  async onTopicSelect(topic) {
    if (this.state.loading) return;

    this.state.loading = true;
    this.ui.reset(topic && topic.length ? topic : "random");

    // 1) `fetch` a topical image from {endpoint} and extract the blobbed reponse
    // 2) Generate a new <img src={blob}> element
    // 3) Pass the <img> to the classifier, get predictions back
    const res = await fetch(this.getEndpoint(topic));
    const blob = await res.blob();
    const img = await this.ui.loadImage(blob);
    const predictions = await this.model.classify(img);

    this.ui.showPredictions(predictions);
    this.state.loading = false;
  }
}

document.addEventListener("DOMContentLoaded", async () => {
  const btn = document.querySelector(".controls__btn")

  new Gallery({
    model: await window.mobilenet.load(),
    endpoint: topic => `https://source.unsplash.com/random/800x800/?${topic}`,
    initialTopic: btn.dataset.topic
  });
});
              
            
!
999px

Console