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

              
                <h1>Little Game</h1>
<p>Click on a circle to rotate it; connect the pipes and watch everything move.</p>
<div class="center">
  <ul id="board"></ul>
  <div><span>Points: </span><span id="point"></span></div>
  <a id="fast" href="#">Fast mode</a>
  <a id="normal" href="#">Normal mode</a>
  <div class="right">
    <a href="https://twitter.com/share" id="tweet">Tweet Score</a>
  </div>
  <footer>
   <p>Everything is rendered in HTML + CSS, no Canvas used. </p> 
  </footer>
</div>

              
            
!

CSS

              
                
ul{
  display:block;
  width:500px;
  height:500px;
  list-style:none;
  
  transform:translateZ(0);
}

li{
  position:relative;
  
  display:block;
  float:left;
  
  background:rgba(60,60,60,1);
  
  width:50px;
  height:50px;
  
  border-radius:100%;
    
  overflow:hidden;
}

li:before{
  position:absolute;
  content:'';
  
  width:50px;
  height:50px;
  
  top:22px;
  left:22px;
  border:5px solid white;
  width:inherit;
  height:inherit;
  border-radius:40%;
  
  box-sizing:border-box;
}

[data-state='1'], [data-state='2'], [data-state='3'], [data-state='4'], li{
    animation:turn .5s forwards;
}
[data-state='2']{
    animation-name:turn1;
}
[data-state='3']{
    animation-name:turn2;
}
[data-state='4']{
    animation-name:turn3;
}
li{
    animation-name:turn3;
}

@keyframes turn{
  0%{
    transform:rotate(0deg);
  }
  100%{
    transform:rotate(90deg);
  }
}
@keyframes turn1{
  0%{
    transform:rotate(90deg);
  }
  100%{
    transform:rotate(180deg);
  }
}
@keyframes turn2{
  0%{
    transform:rotate(180deg);
  }
  100%{
    transform:rotate(270deg);
  }
}
@keyframes turn3{
  0%{
    transform:rotate(270deg);
  }
  100%{
    transform:rotate(360deg);
  }
}

/* Boring */
body{
  font-family:"Helvetica Neue", sans-serif;
}
h1, p{
  text-align:center;
}
h1{
  font-size:2em;
}
.center{
  margin:0 auto;
  width:600px;
}
.right{
  float:right;
}

footer{
  font-size:.8em;
}
              
            
!

JS

              
                var game = {
  points : 0,
  delay  : 510,
  init : function(node, point, tweet, w, h){
    this.map = new Array(w*h);
    
    this.node = node;
    this.point = point;
    this.tweet = tweet;
    
    this.w = w;
    this.h = h;
    
    
    this.node.addEventListener('click', function(e){
      var element = e.srcElement || e.target;
      game.points = 0;
      game.turn(element.dataPos, true);
    }, true); 
       
    this._initNode(); 
  },
    
  turn : function(pos, start){   
    var item = this.map[pos];
    
    if (start === true){
       game.chain(pos, item)
     }else{
      setTimeout(function(){
        game.chain(pos, item);
      }, game.delay); // This have to be in sync with the turn animation .5s
     }
  },
  
  chain : function(pos, item){
    if (item.animationRunning) return; // Only do thing when it's not currently animating. 
                                       // Fixing bugs where strange things can happen
    
    var state = item.state, 
        nextItem, 
        nextPos, 
        allowed;
    
    state = ( item.state + 1 ) % 4;
    
    if (state === 0 || state === 1){
      this.startNext(pos+game.w, [2, 3])
    }
    if (state === 0 || state === 3){
      this.startNext(pos+1, [1,2])
    }
    if (state === 1 || state === 2){
      this.startNext(pos-1, [0,3])
    }
    if (state === 2 || state === 3){
      this.startNext(pos-game.w, [0,1])
    }
    
    item.state = state;
    item.animationRunning = true;
    item.li.setAttribute('data-state', state);
    
    // Just want to make this working. 
    // here i should use the animationend event! 
    setTimeout(function(){item.animationRunning = false;}, 500)
      
    game.addPoints();
  },
  
  startNext : function(nextPos, allowed){
    var nextItem = game.map[nextPos];    
    if (nextItem !== undefined && allowed.indexOf(nextItem.state) !== -1){
      game.turn(nextPos);
    }
  },
  
  addPoints : function(){ 
    game.points++;
    game.tweet.setAttribute('href', "https://twitter.com/intent/tweet?&text="+encodeURIComponent("I scored '"+game.points+"' in this Little Game https://cdpn.io/JjBhk"))
    game.point.innerHTML = game.points;
  },
  
  _initNode : function(){
    var frag = document.createDocumentFragment(),
        b = 0, map = this.map;
    for (var i=0;i<map.length;i++){
      var state = Math.floor(Math.random()*4),
          li   = document.createElement('li'); 
      
          li.setAttribute('data-state', state);
          li.dataPos = i;
      
      map[i] = {state : state, li : li};
      frag.appendChild(li);
    }
    this.node.innerHTML = "";
    this.node.appendChild(frag);
  }
}

var board = document.getElementById('board'),
    point =  document.getElementById('point'),
    tweet =  document.getElementById('tweet');
game.init(board, point, tweet, 10, 10);



var fast =  document.getElementById('fast');
fast.addEventListener('click', function(e){
  game.delay = 0;
  e.preventDefault();
});
var normal = document.getElementById('normal');
normal.addEventListener('click', function(e){
  game.delay = 510;
  e.preventDefault();
});
              
            
!
999px

Console