cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              .wrapper
  
  .candle__container
    .candle
      //.candle__glow
      //.candle__flame--outer 
      //.candle__flame--inner 
    .candle
    .candle
    .candle
    .candle
    .candle
    .candle
    
  a.btn.relight__btn(href="#") 🔥Relight  
  
  .instructions
    a.btn.instructions__toggle(href="#") Instructions
    p.instructions__content This is a javascript implementation of <a href="http://www.popularmechanics.com/science/math/a26818/riddle-of-the-week-30-seven-candles/?src=nl&mag=pop&list=nl_pnl_news&date=061017">Popular Mechanics Riddle of the Week #30</a>.
    p.instructions__content You're in a room (belonging to Cthulu 🐙, as it were) with seven candles in a circle, on a table, in the middle of the room. The goal is to blow out all candles in the least number of moves.
    p.instructions__content When you blow out one candle in the circle, the two adjacent candles are extinguished as well. If you blow on a candle that has already been extinguished, it will relight. And if there is an extinguished candle next to the one you blow on, it also will relight.

    p.instructions__content To put it another way: When you blow on a candle, it—as well as the candles on either side—will change from lit to unlit, or vice versa.
            
          
!
            
              $candle-diameter: 70px
$candle-diameter--shadow: $candle-diameter * 1.2
$flame-diameter--inner: $candle-diameter * .7
$flame-diameter--outer: $candle-diameter * .9
$flame-diameter--glow: $candle-diameter * 4
$circle-diameter: 400px
$flame--inner: rgba(231, 099, 000, .57)
$flame--outer: rgba(255, 185, 023, .80)
$candle--shadow: rgba(000, 000, 000, .20)
$flame--glow: rgba(255, 244, 187, .12)

/// Keyframe all the things
@keyframes flame-orbit--inner
  0%
    transform: rotate(0deg) translateX(-$candle-diameter/10) rotate(0deg)
  100%
    transform: rotate(360deg) translateX(-$candle-diameter/10) rotate(-360deg)
 
@keyframes flame-orbit--outer
  0%
    transform: rotate(0deg) translateX(-$candle-diameter/7) rotate(0deg)
  100%
    transform: rotate(360deg) translateX(-$candle-diameter/7) rotate(-360deg)
  
@keyframes flame-orbit--glow
  0%
    width: $flame-diameter--glow
    transform: rotate(0deg) translateX($candle-diameter/7) rotate(0deg)
  100%
    width: $flame-diameter--glow
    transform: rotate(360deg) translateX($candle-diameter/7) rotate(-360deg)
  
@keyframes flame-orbit--shadow
  0%
    transform: rotate(0deg) translateX($candle-diameter/3) rotate(0deg)
  100%
    transform: rotate(360deg) translateX($candle-diameter/3) rotate(-360deg)
  
/// Mixin to place items on a circle
/// @author Hugo Giraudel
/// @author Ana Tudor
/// @param {Integer} $item-count - Number of items on the circle
/// @param {Length} $circle-size - Large circle size
/// @param {Length} $item-size - Single item size
=on-circle($item-count, $circle-size, $item-size) 
  position: relative
  width:  $circle-size
  height: $circle-size
  padding: 0
  border-radius: 50% 
  list-style: none       
  
  > * 
    display: block
    position: absolute
    top:  50% 
    left: 50%
    width:  $item-size
    height: $item-size
    margin: -($item-size / 2)
  
    $angle: (360 / $item-count)
    $rot: 0

    @for $i from 1 through $item-count 
      &:nth-of-type(#{$i}) 
        transform: rotate($rot * 1deg) translate($circle-size / 2) rotate($rot * -1deg)
        
      $rot: $rot + $angle

body, html 
  padding: 0
  margin: 0
  
.wrapper
  width: 100vw
  height: 100vh
  margin: 0
  padding: 50px
  background: #2f4f4f
  display: flex
  justify-content: center
  align-items: center
  box-sizing: border-box
  overflow: hidden
  z-index: 1
  
.candle
  //width: 70px
  //height: 70px
  border-radius: 50%
  background: #fffaf0
  
  &__shadow
    width: $candle-diameter--shadow
    height: $candle-diameter--shadow
    opacity: 0
    z-index: -1
    position: absolute
    top: -1 * (($candle-diameter--shadow/2) - ($candle-diameter/2))
    left: -1 * (($candle-diameter--shadow/2) - ($candle-diameter/2))
    background: $candle--shadow
    border-radius: 50%
    animation: flame-orbit--shadow 16s cubic-bezier(.36,.07,.19,.97) infinite
    animation-delay: inherit
    transition: opacity 2s cubic-bezier(.36,.07,.19,.97)
  
  &__glow
    width: $flame-diameter--glow
    height: $flame-diameter--glow
    opacity: 0
    z-index: 6
    position: absolute
    top: -1 * (($flame-diameter--glow/2) - ($candle-diameter/2))
    left: -1 * (($flame-diameter--glow/2) - ($candle-diameter/2))
    background: $flame--glow
    border-radius: 50%
    animation: flame-orbit--glow 16s cubic-bezier(.36,.07,.19,.97) infinite
    animation-delay: inherit
    transition: opacity 2s cubic-bezier(.36,.07,.19,.97)
    
  &__flame
    
    &--inner
      width: $flame-diameter--inner
      height: $flame-diameter--inner
      opacity: 0
      position: absolute
      top: -1 * (($flame-diameter--inner/2) - ($candle-diameter/2))
      left: -1 * (($flame-diameter--inner/2) - ($candle-diameter/2))
      background: $flame--inner
      border-radius: 50%
      transition: opacity 2s cubic-bezier(.36,.07,.19,.97)
      animation: flame-orbit--inner 16s cubic-bezier(.36,.07,.19,.97) infinite
      animation-delay: inherit
      
    &--outer
      width: $flame-diameter--outer
      height: $flame-diameter--outer
      opacity: 0
      position: absolute
      top: -1 * (($flame-diameter--outer/2) - ($candle-diameter/2))
      left: -1 * (($flame-diameter--outer/2) - ($candle-diameter/2))
      background: $flame--outer 
      border-radius: 50%
      transition: opacity 2s cubic-bezier(.36,.07,.19,.97)
      animation: flame-orbit--outer 16s cubic-bezier(.36,.07,.19,.97) infinite
      animation-delay: inherit
    
  &--is-lit &__glow
    width: $flame-diameter--glow
    height: $flame-diameter--glow
    opacity: 1
  
  &--is-lit &__shadow
    width: $candle-diameter--shadow
    height: $candle-diameter--shadow
    opacity: 1
    
  &--is-lit &__flame
    
    &--inner
      width: $flame-diameter--inner
      height: $flame-diameter--inner
      opacity: 1
      
    &--outer    
      width: $flame-diameter--outer
      height: $flame-diameter--outer
      opacity: 1
        
  &__container
    +on-circle(7, $circle-diameter, $candle-diameter)  
  
.instructions
  position: absolute
  top: calc(100vh - 44px)
  bottom: initial
  width: 100vw
  padding: 50px
  padding-top: 10px
  background: rgba(0,0,0,.4)
  box-sizing: border-box
  color: white
  
  &--is-showing
    top: initial
    bottom: 0

.btn
  min-height: 44px
  min-width: 44px
  padding: auto
  
.relight__btn
  text-decoration: none
  position: absolute
  font-family: sans-serif
  text-transform: uppercase
  color: $flame--outer
  
  &:hover
    opacity: .5
  
.instructions__toggle
  text-decoration: none
  color: inherit
  font-family: sans-serif
  font-weight: 100
  font-size: 1.5em  
.instructions__content
  font-family: sans-serif
  font-weight: 100
  font-size: 1.2em
  
  a 
    color: inherit
    &:visited
      color: inherit
    &:hover
      color: blue
            
          
!
            
              const candles = document.querySelectorAll('.candle');
const instructions = document.querySelector('.instructions');
const relightButton = document.querySelector('.relight__btn');

const toggleIsLit = ( e ) => {
  e.currentTarget.classList.toggle('candle--is-lit');
  try{
    e.currentTarget.previousElementSibling.classList.toggle('candle--is-lit');
  } catch(err) {
    console.log('PrevSibling is null, targeting last child.')
    e.currentTarget.parentElement.lastElementChild.classList.toggle('candle--is-lit');
  }
  try{
    e.currentTarget.nextElementSibling.classList.toggle('candle--is-lit');
  } catch(err) {
    console.log('nextSibling is null, targeting first child.')
    e.currentTarget.parentElement.firstElementChild.classList.toggle('candle--is-lit');
  }
}

const lightCandle = ( candle ) => {
  const glow = document.createElement('div');
  const shadow = document.createElement('div');
  const inner = document.createElement('div');
  const outer = document.createElement('div');
  let deg = Math.random()*-25;  
  candle.style.animationDelay = `${deg}s`;
  candle.classList.add('candle--is-lit');
  glow.classList.add('candle__glow');
  shadow.classList.add('candle__shadow');
  outer.classList.add('candle__flame--outer');
  inner.classList.add('candle__flame--inner');
  candle.appendChild(glow);
  candle.appendChild(shadow);
  candle.appendChild(outer);
  candle.appendChild(inner);
}

const instructionsIsShowing = () => instructions.classList.toggle('instructions--is-showing');

const init = () => {
  candles.forEach( (candle) => {
  lightCandle(candle);
  candle.addEventListener('click', toggleIsLit, false);
});
}

const reset = () => {
  candles.forEach( (candle) => candle.classList.add('candle--is-lit'));
}

instructions.addEventListener('click', instructionsIsShowing);

relightButton.addEventListener('click', reset);

init();
            
          
!
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.
Loading ..................

Console