Pen Settings

HTML

CSS

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

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

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.

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 id="app">
  <div class="cards">
    <sort-card
      v-for="(card, index) in store.state.cards"
      :key="index" :value="card.value" :sort-index="card.sortIndex" :is-active="card.isActive" :is-locked="card.isLocked">
    </sort-card>
  </div>
  <div class="control-panel">
    <h1>Bubble Sort Visualizer</h1>
    <button aria-label="Reset" v-if="store.state.done" @click="reset">
      <i class="fa fa-refresh" aria-hidden="true"></i>
    </button>
  </div>
</div>

<script type="text/x-template" id="sort-card-template">
	<div class="card-wrapper" :data-sort-index="sortIndex">
  	<div class="card" :class="cardClassObject">
    	<div class="value">{{value}}</div>
  	</div>
  </div>
</script>

              
            
!

CSS

              
                $card-count: 16;
$card-height-base: 30px;
$card-height-increment: 10px;
$transition-time: 200ms;
$easing: cubic-bezier(0.175, 0.885, 0.320, 1.275);

@function get-card-height($order) {
  @return $card-height-base + $order * $card-height-increment;
}

@import url('https://fonts.googleapis.com/css?family=Titillium+Web:700');
@import url('https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');

*, *::before, *::after {
  box-sizing: border-box;
}

html {
  font: 700 16px/1 'Titillium Web', sans-serif;
}

body {
  margin: 40px 0;
  color: #fff;
  background-color: #000;
}

#app {
  width: 640px;
  margin: 0 auto;
}

.cards {
  position: relative;
  height: get-card-height($card-count);
}

.card-wrapper {
  position: absolute;
  bottom: 0;
  width: 100% / $card-count;
  transition: transform $transition-time $easing;

  // set increasing heights
  @for $i from 1 through $card-count {
    &:nth-child(#{$i}) {
      height: get-card-height($i);
    }
  }

  // set increasing horizontal position based on sortIndex
  @for $i from 0 through $card-count - 1 {
    &[data-sort-index='#{$i}'] {
      transform: translateX($i * 100%);
    }
  }
}

.card {
  position: relative;
  height: 100%;
  margin: 0 5px;
  border: 1px solid #ff3179;
  background-color: #000;
  box-shadow: 0 0 25px #c2255c;
}

.card-active {
  filter: hue-rotate(200deg);
}

.card-locked {
  filter: hue-rotate(280deg);
}

.value {
  position: absolute;
  bottom: 5px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 1.25rem;
}

.control-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 30px 5px 0;
  padding-top: 20px;
  border-top: 1px solid #fff;
}

h1 {
  margin: 0;
  font-size: 2.5rem;
}

button {
  appearance: none;
  background: none;
  border: none;
  color: #ff3179;
  font-size: 1.5rem;
  cursor: pointer;
}

@media only screen and (min-width: 880px) {
  #app { width: 800px; }
  .value { font-size: 1.5rem; }
}

@media only screen and (min-width: 1084px) {
  #app { width: 1024px; }
  .value { font-size: 1.75rem; }
}

              
            
!

JS

              
                const CARD_COUNT = 16; // match $card-count
const EVENT_DELAY = 200; // match $transition-time

const store = new Vuex.Store({
  state: {
    values: [], // the actual list of numbers being sorted
    cards: [], // visual representations (moved around via CSS)
    done: true
  },
  
  mutations: {
    reset (state, payload) {
      state.values = payload.values;

      // cards are added to the DOM in order of ascending value
      // though sortIndex dictates horizontal position
      state.cards = [];
      for (let i = 1; i <= state.values.length; i++) {
        state.cards.push({
          value: i,
          sortIndex: state.values.indexOf(i),
          isActive: false,
          isLocked: false
        });
      }
      
      state.done = false;
    },
    
    swap (state, payload) {
      let a = payload.indexes[0];
      let b = payload.indexes[1];
      let temp = state.values[a];
      state.values[a] = state.values[b];
      state.values[b] = temp;

      // tell each card what its new order is
      state.cards.forEach((card) => {
        card.sortIndex = state.values.indexOf(card.value);
      });
    },
    
    activate (state, payload) {
      payload.indexes.forEach((index) => {
        let card = state.cards.find((card) => { return card.sortIndex === index; });
        card.isActive = true;
      });
    },
    
    deactivate (state, payload) {
      payload.indexes.forEach((index) => {
        let card = state.cards.find((card) => { return card.sortIndex === index; });
        card.isActive = false;
      });
    },

    lock (state, payload) {
      payload.indexes.forEach((index) => {
        let card = state.cards.find((card) => { return card.sortIndex === index; });
        card.isLocked = true;
      });
    },
    
    done (state) {
      state.done = true;
    }
  }
});

Vue.component('sort-card', {
  template: '#sort-card-template',
  props: ['value', 'sortIndex', 'isActive', 'isLocked'],
  computed: {
    cardClassObject() {
      return {
        'card-active': this.isActive,
        'card-locked': this.isLocked
      }
    }
  }
});

new Vue({
  el: '#app',
  store,
  
  created() {
    this.reset();
  },
  
  methods: {
    reset() {
      // random array of values 0 to n - 1
      let pool = Array.from(Array(CARD_COUNT).keys());
      
      // random order of values from pool
      let values = [];
      while (pool.length > 0) {
        let index = Math.floor(Math.random() * pool.length);
        values.push(pool[index] + 1); // add 1 to make range 1 to n
        pool.splice(index, 1); // remove from pool of candidates
      }
      store.commit({ type: 'reset', values: values });

      // sort executes immediately, but events are replayed slowly
      let sequence = this.bubbleSort(values.slice());
      sequence.forEach((event, index) => {
        setTimeout(() => { store.commit(event); }, index * EVENT_DELAY);
      });
    },
    
    bubbleSort(values) {
      let sequence = [];
      let swapped;
      let indexLastUnsorted = values.length - 1;

      do {
        swapped = false;
        for (var i = 0; i < indexLastUnsorted; i++) {
          sequence.push({ type: 'activate', indexes: [i, i + 1] });
          if (values[i] > values[i + 1]) {
            let temp = values[i];
            values[i] = values[i + 1];
            values[i + 1] = temp;
            swapped = true;
            sequence.push({ type: 'swap', indexes: [i, i + 1] });
          }
          sequence.push({ type: 'deactivate', indexes: [i, i + 1] });
        }
        sequence.push({ type: 'lock', indexes: [indexLastUnsorted] });
        indexLastUnsorted--;
      } while (swapped);
      
      let skipped = Array.from(Array(indexLastUnsorted + 1).keys())
      sequence.push({ type: 'lock', indexes: skipped });
      sequence.push({ type: 'done' });

      return sequence;
    }
  }
});

              
            
!
999px

Console