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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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="cointainer">

  <!-- This will be removed and used for the template -->
  <div class="template" style="visibility:hidden;">
    <img class="coinHeads" src="http://www.joshworth.com/dev/78coins/img/cf/coin-heads.svg" />
    <img class="coinEdge" src="http://www.joshworth.com/dev/78coins/img/cf/coin-edge.svg" />
    <img class="coinLine" src="http://www.joshworth.com/dev/78coins/img/cf/coin-line.svg" />
    <img class="coinTails" src="http://www.joshworth.com/dev/78coins/img/cf/coin-tails.svg" />
  </div>

</div>
<div id="flipBtn">
  <button onclick="callIt()">Try it</button>
  <p id="notes1"></p>
  <p id="notes2"></p>
</div>
              
            
!

CSS

              
                @charset "UTF-8";


body {
	background-color:#000;
}

.coinHeads, .coinEdge, .coinLine, .coinTails, #flipBtn {
	display:block;
	position:absolute;
	left: 0px;
}

.coinBox {
  display:block;
	position:absolute;
	height: 100px;
	width: 100px;
  border: 0px solid white;
}

.coinHeads {
	height:100px;
	width:100px;
	z-index:30;
	top: 0px;
}

.coinEdge {
	height:100px;
	width:100px;
	z-index:20;
	top: 0px;
}

#cointainer{
  background-color:black;
  width:100%;
  height:80vh;
  overflow:visible;
  position:relative;
}
.coinLine {
	top: 50px;
	height:0px;
	width:100px;
	z-index:10;
}

.coinTails {
	top: 0px;
	height:100px;
	width:100px;
	z-index:40;
	opacity:0;
}

#flipBtn {
	top: 300px;
	left: 50px;
}
              
            
!

JS

              
                //why won't the last coin animate??

var container = $("#cointainer"); //canvas area
var $coinBox = $("#coinBox");
var coinTl = new TimelineMax(); // master timeline
var jumpTime = (Math.random() * .75) + .8;

// The HTML for the coins to use
var template = $(".template").remove();

var count = 5;
var coins = createCoins(count);

// Returns an array of coin objects
function createCoins(count) {

  var coins = [];
  
  for (var i = 0; i < count; i++) {

    // Copy the template
    var clone = template.clone().attr("class", "coinBox");
    container.append(clone);

    // Finds the images inside the clone
    var img = clone.find("img");
    
    var coin = {
      index : i,
      box   : clone,
      heads : img[0],
      edge  : img[1],
      line  : img[2],
      tails : img[3],
      flip  : function() {
        var side = Math.random() > 0.5 ? headFlip(this) : tailFlip(this);
        return getFlippage(this.box, side);
      }
    };

    coins.push(coin);

    TweenLite.set(clone, { x: i * 100, y: 100, autoAlpha: 1 });
  } 
  
  return coins;  
}

// Animation setup
function getFlippage(coin, whatSide) {

  //define jumping
  jumpTime = (Math.random() * .75) + .8;
  var jumpTl = new TimelineMax();
  var jumpUp = new TweenMax(coin, jumpTime / 2, {
    y: -100,
    rotation: 10,
    ease: Power1.easeOut
  });
  var jumpDown = new TweenMax(coin, jumpTime / 2, {
    y: 100,
    rotation: -10,
    ease: Bounce.easeOut
  });

  jumpTl
    .add(jumpUp.play())
    .add(jumpDown.play())
    .to(coin, .1, {
    rotation: 0
  }, "-=.2");

  // timeline for each
  var coinFlipTl = new TimelineMax();
  coinFlipTl
    .add(whatSide, 0) //picks whether to land on heads or tails
    .add(jumpTl, 0);
  return coinFlipTl;
};

// gets called when you click the button
function callIt() {
  //container.html('');
  coinTl.clear();
  buildTimeline();
  //coinTl.seek(0).invalidate().restart(); //don't seem to need this.
};

//makes a new animation for a coin that's heads
function headFlip(coin) {
  
  var flipTime = 1 / 4;
  var headsTl = new TimelineMax({ repeat: 1 });
  
  headsTl
    .fromTo(coin.heads, flipTime, {
    scaleY: 1,
    y: 0
  }, {
    scaleY: 0,
    y: 0
  }, "headEnd")
    .fromTo(coin.heads, flipTime, {
    scaleY: 1,
    y: 0
  }, {
    scaleY: 0,
    y: 8
  }, "headEnd")
    .fromTo(coin.line, flipTime, {
    height: 0,
    y: 0
  }, {
    height: 8,
    y: 0
  }, "headEnd")

    .to(coin.heads, 0, {
    y: 0
  }, "tailStart")
    .fromTo(coin.tails, flipTime, {
    opacity: 1,
    scaleY: 0,
    y: 8
  }, {
    opacity: 1,
    scaleY: 1,
    y: 0
  }, "tailStart")
    .to(coin.edge, flipTime, {
    scaleY: 1,
    y: 0
  }, "tailStart")
    .to(coin.line, flipTime, {
    height: 0,
    y: 0
  }, "tailStart")

    .to(coin.tails, flipTime, {
    scaleY: 0
  }, "tailEnd")
    .fromTo(coin.edge, flipTime, {
    scaleY: 1,
    y: 0
  }, {
    scaleY: 0,
    y: 8
  }, "tailEnd")
    .to(coin.line, flipTime, {
    height: 8,
    y: 0
  }, "tailEnd")
    .fromTo(coin.heads, flipTime, {
    scaleY: 0,
    y: 8
  }, {
    scaleY: 1,
    y: 0
  }, "headStart")
    .fromTo(coin.edge, flipTime, {
    scaleY: 0,
    y: 0
  }, {
    scaleY: 1,
    y: 0
  }, "headStart")
    .to(coin.line, flipTime, {
    height: 0,
    y: 0
  }, "headStart");
  
  return headsTl;
};

//makes a new animation for a coin that's tails
function tailFlip(coin) {

  var flipTime = 1 / 3;  
  var tailsTl = new TimelineMax({ repeat: 1 });
  
  tailsTl
    .fromTo(coin.heads, flipTime, {
    scaleY: 1,
    y: 0
  }, {
    scaleY: 0,
    y: 0
  }, "headEnd2")
    .fromTo(coin.edge, flipTime, {
    scaleY: 1,
    y: 0
  }, {
    scaleY: 0,
    y: 8
  }, "headEnd2")
    .fromTo(coin.line, flipTime, {
    height: 0,
    y: 0
  }, {
    height: 8,
    y: 0
  }, "headEnd2")

    .to(coin.edge, 0, {
    y: 0
  }, "tailStart2")
    .fromTo(coin.tails, flipTime, {
    opacity: 1,
    scaleY: 0,
    y: 8
  }, {
    opacity: 1,
    scaleY: 1,
    y: 0
  }, "tailStart2")
    .to(coin.edge, flipTime, {
    scaleY: 1,
    y: 0
  }, "tailStart2")
    .to(coin.line, flipTime, {
    height: 0,
    y: 0
  }, "tailStart2");

  return tailsTl;
};

function buildTimeline() {

  // forEach is an array method
  coins.forEach(function(coin) {    
    coinTl.add(coin.flip(), 0);
  });
};

buildTimeline();

              
            
!
999px

Console