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 id="app">
  <slot-machine></slot-machine>
</div>
              
            
!

CSS

              
                @use postcss-cssnext;
:root {
  --tileSize: 90px;
}
html, body {
  margin: 0;
  font-family: sans-serif;
  cursor: default;
}
#app {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.SlotMachine {
  border-radius: 5px;
  width: 270px;
}
.SlotMachine-reels {
  display: flex;
  position: relative;
}
.SlotMachine-shadow {
  border-radius: 4px 4px 0 0;
  pointer-events: none;
  z-index: 99;
  position: absolute;
  width: 100%;
  height: 100%;
  box-shadow: 
    inset 0 25px 30px -5px gray(0,.1),
    inset 0 5px 10px -2px gray(0,.2),
    inset 0 -25px 30px -5px gray(0,.1),
    inset 0 -5px 10px -2px gray(0,.2);
}
.SlotMachine-payline {
  position: absolute;
  top: calc(var(--tileSize) * 1 * 1.1666);
  height: 1px;
  width: 100%;
  background: gray(0, 0.3);
}


.SlotMachine-stats {
  display: flex;
  background: #000;
  justify-content: flex-end;
  padding: 10px 0;
}
.SlotMachine-coin {
  width: 6px;
  height: 38px;
  background: gray(20);
  border: 3px solid gray(40);
  border-radius: 6px;
  margin: 12px 10px 0 10px;
}
.SlotMachine-coin:hover {
  background: gray(140);
}
.SlotMachine-stat {
  display: flex;
  flex-direction: column;
  margin-right: 10px;
  flex-grow: 1;
}
.SlotMachine-statTitle {
  font-size: 12px;
  color: gray(60);
}
.SlotMachine-statValue {
  padding: 5px 10px;
  background: rgba(255,0,0,0.15);
  border-radius: 2px;
  border: 1px solid gray(0);
  font-size: 25px;
  text-align: right;
  color: rgba(255, 0, 0, 0.8);
  text-shadow: 0 0 4px rgba(255, 0, 0, 0.5);
  white-space: nowrap;
}
.SlotMachine-statSub {
  font-size: 9px;
  color: gray(50);
  text-align: right;
}



.SlotMachine-actions {
  padding: 20px 10px;
  display: flex;
  justify-content: flex-end;
  background: gray(20);
  border-radius: 0 0 4px 4px;
}
.SlotMachine-button {
  font-size: 17px;
  font-weight: bold;
  padding: 14px 12px;
  margin-left: 20px;
  border-radius: 5px;
  box-shadow: 0 1px 2px 2px gray(0);
}
.SlotMachine-button:focus {
  outline: none;
}
.SlotMachine-button:active {
  box-shadow: 0 0 2px 1px gray(0);
}

.SlotMachine-button.is-spin {
  background: rgba(0, 255, 0, 0.4);
  border: 1px solid rgba(0, 255, 0, 0.4);
}
.SlotMachine-button.is-spin:hover {
  background: rgba(0, 255, 0, 0.43);
}
.SlotMachine-button.is-spin:active {
  background: rgba(0, 255, 0, 0.46);
}

.SlotMachine-button.is-win {
  background: rgba(255, 0, 0, 0.4);
  border: 1px solid rgba(255, 0, 0, 0.5);
}
.SlotMachine-button.is-win.has-win {
  background: rgba(255, 0, 0, 0.99);
}
.SlotMachine-button.is-win:hover {
  background: rgba(255, 0, 0, 0.65);
}
.SlotMachine-button.is-win:active {
  background: rgba(255, 0, 0, 0.7);
}






.Reel {
  width: var(--tileSize);
  height: calc(var(--tileSize) * 2.333);
  overflow: hidden;
}
.Reel.is-locked {
/*   opacity: 0.8; */
  background: rgba(0,0,0,0.1);
}
.Reel:not(:last-child) {
  border-right: 1px solid gray(0, 0.1);
}
.Reel-inner {
  position: relative;
  top: calc(var(--tileSize) * -1 * 1.333);
  display: flex;
  flex-direction: column;
}
.Reel.move .Reel-inner{
  transition: margin-top 0.04s linear;
  margin-top: var(--tileSize);
}
.Reel-image {
  width: var(--tileSize);
  width: var(--tileSize);
}

.Legend {
  display: flex;
  flex-direction: column;
  margin-top: 16px;
}
.Legend-item {
  width: 100%;
  display: flex;
}

.Legend-itemTitle {
  font-size: 10px;
  padding-top: 3px;
  width: 42px;
  color: gray(150);
}

.Legend-itemImage {
  width: 20px;
}

.Legend-tip {
  margin-top: 8px;
  font-size: 10px;
  color: gray(150);
}

.Legend-credit {
  margin-top: 8px;
  font-size: 10px;
  font-style: italic;
  color: gray(150);
}
              
            
!

JS

              
                Vue.component('slot-reel', {
  props: ['value', 'canlock'],
  data: function() {
    return {
      momentum: null,
      audio: {
        bar: new Audio('https://freesound.org/data/previews/0/707_207-lq.mp3'),
        barWin: new Audio('https://freesound.org/data/previews/337/337049_3232293-lq.mp3'),
        spin: new Audio('https://freesound.org/data/previews/120/120373_824230-lq.mp3'),
        spinEnd: new Audio('https://freesound.org/data/previews/145/145441_2615119-lq.mp3'),
        lock: new Audio('https://freesound.org/data/previews/56/56246_91374-lq.mp3')
      },
      reelTileCount: 15,
      reelTileData: null,
      reelSourceData: [
        {
          name: 'Lemon',
          value: 2,
          image: 'https://cdn4.iconfinder.com/data/icons/slot-machines/512/Lemon-512.png'
        },
        {
          name: 'Melon',
          value: 4,
          image: 'https://cdn4.iconfinder.com/data/icons/slot-machines/512/Watermelon-512.png'
        },
        {
          name: 'Grapes',
          value: 8,
          image: 'https://cdn4.iconfinder.com/data/icons/slot-machines/512/Grapes-512.png'
        },
        {
          name: 'Cherry',
          value: 16,
          image: 'https://cdn4.iconfinder.com/data/icons/slot-machines/512/Cherry-512.png'
        },
        {
          name: 'Bar',
          value: 72,
          image: 'https://cdn4.iconfinder.com/data/icons/casino-games/512/bar-512.png'
        }
      ],
      reelIndex: 2,
      tile1Index: 0,
      tile2Index: 1,
      tile3Index: 2,
      tile4Index: 3,
      tile5Index: 4,
      locked: false
    }
  },
  beforeMount: function () {
    // Build up the reelTileData array with random tiles  
    let frs = []
    let count = this.reelTileCount
    this.audio.spin.volume = 0.3
    this.audio.spinEnd.volume = 0.8
    this.audio.lock.volume = 0.2
    this.audio.spin.currentTime = 0.3
    // Method 1, random until count is reached
    // while (count > 0) {
    //   const fruitIndex = Math.floor(Math.random() * this.reelSourceData.length)
    //   const fruitObject = this.reelSourceData[fruitIndex]
    //   frs.push(fruitObject)
    //   count--
    // }
    
    // Method 2, sort on value, use index to determine entry count, shuffle
    let reelSourceData = this.reelSourceData.slice(0)
    reelSourceData.sort((a, b) => b.value - a.value)
    reelSourceData.forEach((sd, i) => {
      let times = i + 1 + i // 0+1+0=1, 3+2+3=8
      // if (sd.name === 'Bar') times += 40 // TEST
      while (times > 0) {
        frs.push(sd)
        times--
      }
    })
    
    function shuffle(array) {
      var currentIndex = array.length, temporaryValue, randomIndex;

      // While there remain elements to shuffle...
      while (0 !== currentIndex) {

        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
      }

      return array;
    }
    
    this.reelTileData = shuffle(frs)
  },
  mounted: function() {
    this.$el.addEventListener("transitionend", this.animateEnd)
  },
  computed: {
  },
  methods: {
    run: function() {
      console.log('run')
      if (!this.locked) {
        const min = 8
        const max = 28
        const momentum = Math.floor(Math.random()*(max-min+1)+min);
        this.momentum = momentum
        this.audio.spin.play()
        this.animate()
      } else {
        this.locked = false
        this.$emit('stopped', this.reelTileData[this.reelIndex], true)
      }
    },
    animate: function() {
      this.$el.classList.add('move')
    },
    animateEnd: function() {
      this.$el.classList.remove('move')
      this.reIndex()
      this.momentum = this.momentum - 1
      if (this.momentum > 0) {
        setTimeout(this.animate, 10)
      } else {
        const reelTileData = this.reelTileData[this.reelIndex]
        this.$emit('stopped', reelTileData)
        if (reelTileData.name === 'Bar') {
          this.audio.barWin.play()
        } else {
        this.audio.spinEnd.play()          
        }
        this.audio.spin.pause()
        this.audio.spin.currentTime = 0.3
      }
    },
    reIndex: function() {
      const end = this.reelTileData.length - 1 //this.reelTileCount - 1
      const index = this.reelIndex === 0 ? end : this.reelIndex - 1
      // const index = this.index === end ? 0 : this.index + 1
      this.reelIndex = index
      this.tile1Index = index === 1 ? end : index === 0 ? end - 1 : index - 2
      this.tile2Index = index === 0 ? end : index - 1
      this.tile3Index = index
      this.tile4Index = index === end ? 0 : index + 1
      this.tile5Index = index === end - 1 ? 0 : index === end ? 1 : index + 2
    },
    lock: function() {
      if (this.canlock) {
        this.locked = !this.locked
        this.audio.lock.play()
      }
    }
  },
  
  template: `<div class="Reel" v-bind:class="{'is-locked':locked}" v-on:mousedown="lock()">
    <div class="Reel-inner">
      <img class="Reel-image" :src="reelTileData[tile1Index].image" />
      <img class="Reel-image" :src="reelTileData[tile2Index].image" />
      <img class="Reel-image" :src="reelTileData[tile3Index].image" />
      <img class="Reel-image" :src="reelTileData[tile4Index].image" />
      <img class="Reel-image" :src="reelTileData[tile5Index].image" />
    </div>
  </div>`
})



Vue.component('slot-machine', {
  data: function() {
    return {
      spend: 6,
      credits: 6,
      win: 0,
      resultData: false,
      canlock: true,
      waslocked: false,
      audio: {
        barWin: new Audio('https://freesound.org/data/previews/337/337049_3232293-lq.mp3'),
        win: new Audio('https://freesound.org/data/previews/387/387232_1474204-lq.mp3'),
        insertCoin: new Audio('https://freesound.org/data/previews/276/276091_5123851-lq.mp3'),
        bigwin: new Audio('https://freesound.org/data/previews/270/270319_5123851-lq.mp3')
      }
    }
  },
  beforeMount: function () {},
  mounted: function() {
    window.addEventListener('keydown', this.keydown)
  },
  computed: {
  },
  methods: {
    keydown: function(e) {
      console.log(e.which)
      const key = {
        one: 49,
        two: 50,
        three: 51,
        space: 32
      }
      if (e.which === key.one) {
        this.$refs.reel1.lock()
        e.preventDefault()
      } else if (e.which === key.two) {
        this.$refs.reel2.lock()
        e.preventDefault()
      } else if (e.which === key.three) {
        this.$refs.reel3.lock()
        e.preventDefault()
      } else if (e.which === key.space) {
        this.spin()
        e.preventDefault()
      }
    },
    spin: function() {
      if (this.credits > 0 && !this.resultData) {
        this.resultData = []
        this.credits = this.credits - 0.5
        this.$refs.reel1.run()
        this.$refs.reel2.run()
        this.$refs.reel3.run()
      }
    },
    insertCoin: function() {
      this.audio.insertCoin.currentTime = 0
      this.audio.insertCoin.play()
      this.credits += .5
      this.spend += .5
    },
    takeWin: function() {
      if (this.win > 0) {
        this.credits += this.win
        this.win = 0
      }
    },
    reelStopped(resultData, wasLocked) {
      if (wasLocked) this.waslocked = wasLocked
      this.resultData.push(resultData)
      if (this.resultData.length === 3) {
        this.checkWin(this.resultData)
        if (this.waslocked) {
          this.waslocked = false
          this.canlock = false
        } else {
          this.canlock = true
        }
      }
    },
    checkWin: function() {
      if (this.resultData.length === 3) { // ;-)
        const v1 = this.resultData[0]
        const v2 = this.resultData[1]
        const v3 = this.resultData[2]
        if(v1.name === v2.name && v2.name === v3.name) {
          if (v1.value >= 8) {
            this.audio.bigwin.play()
          } else {
            this.audio.win.play()
          }
          this.win += v1.value
          this.waslocked = true // prevent lock after an unlocked win
        } else {

          const bar1 = v1.name === 'Bar'
          const bar2 = v2.name === 'Bar'
          const bar3 = v3.name === 'Bar'
          if (bar1 && bar2 || bar1 && bar3 || bar2 && bar3) {
            this.audio.bigwin.play()
            this.win += 18
            this.waslocked = true // prevent lock after an unlocked win
          } else if (bar1 || bar2 || bar3) {
            // this.audio.win.play()
            // this.audio.barWin.play()
            this.win += 3
            this.waslocked = true // prevent lock after an unlocked win
          } else {
            // Lose : (
          }
        }
      }
      this.resultData = false
    },
  },
  
  template: `<div class="SlotMachine">
    <div class="SlotMachine-reels">
      <div class="SlotMachine-shadow"></div>
      <div class="SlotMachine-payline"></div>
      <slot-reel ref="reel1" :canlock="canlock" v-on:stopped="reelStopped"></slot-reel>
      <slot-reel ref="reel2" :canlock="canlock" v-on:stopped="reelStopped"></slot-reel>
      <slot-reel ref="reel3" :canlock="canlock" v-on:stopped="reelStopped"></slot-reel>
    </div>
    <div class="SlotMachine-stats">
      <div class="SlotMachine-coin" v-on:mousedown="insertCoin()"></div>
      <div class="SlotMachine-stat is-credit">
        <div class="SlotMachine-statTitle">Credits</div>
        <div class="SlotMachine-statValue">€ {{credits.toFixed(2)}}</div>
        <div class="SlotMachine-statSub">spend € {{spend.toFixed(2)}}</div>
      </div>
      <div class="SlotMachine-stat is-win">
        <div class="SlotMachine-statTitle">Won</div>
        <div class="SlotMachine-statValue">€ {{win.toFixed(2)}}</div>
      </div>
    </div>
    <div class="SlotMachine-actions">
      <button class="SlotMachine-button is-spin" v-on:mousedown="spin()">Play</button>
      <div class="SlotMachine-button is-win" v-bind:class="{'has-win':win}" v-on:mousedown="takeWin()">Take Win</div>
    </div>
    <div class="Legend">
      <div class="Legend-item">
        <div class="Legend-itemTitle">€ 2.00</div>
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Lemon-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Lemon-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Lemon-512.png" />
      </div>
      <div class="Legend-item">
        <div class="Legend-itemTitle">€ 4.00</div>
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Watermelon-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Watermelon-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Watermelon-512.png" />
      </div>
      <div class="Legend-item">
        <div class="Legend-itemTitle">€ 8.00</div>
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Grapes-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Grapes-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Grapes-512.png" />
      </div>
      <div class="Legend-item">
        <div class="Legend-itemTitle">€ 16.00</div>
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Cherry-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Cherry-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/slot-machines/512/Cherry-512.png" />
      </div>
      <div class="Legend-item">
        <div class="Legend-itemTitle">€ 3.00</div>
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/casino-games/512/bar-512.png" />
      </div>
      <div class="Legend-item">
        <div class="Legend-itemTitle">€ 18.00</div>
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/casino-games/512/bar-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/casino-games/512/bar-512.png" />
      </div>      
      <div class="Legend-item">
        <div class="Legend-itemTitle">€ 72.00</div>
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/casino-games/512/bar-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/casino-games/512/bar-512.png" />
        <img class="Legend-itemImage" src="https://cdn4.iconfinder.com/data/icons/casino-games/512/bar-512.png" />
      </div>
    </div>
    <div class="Legend-tip">Add coins to add credit. Press space to Play. Click reel or press 1, 2 or 3 to lock. Brought to you by Timo & Lex Hofmeijer</div>
  </div>`
})



var app = new Vue({
  el: '#app'
})
              
            
!
999px

Console