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 hidden>Coins not used: <span id="coinsNotUsedSpan"></span></div>
<div hidden>Coins in left bowl: <span id="coinsInLeftBowlSpan"></span></div>
<div hidden>Coins in right bowl: <span id="coinsInRightBowlSpan"></span></div>
<div hidden>Weight of coins in left bowl: <span id="weightOfCoinsInLeftBowlSpan"></span></div>
<div hidden>Weight of coins in right bowl: <span id="weightOfCoinsInRightBowlSpan"></span></div>

<div>
  <h3>You have <span id="weighsLeftSpan">3</span> weighs left</h3>
  <h3>Your guess:</h3>
  <select hidden>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
</select>
</div>

<svg width="580" height="469" xmlns="http://www.w3.org/2000/svg">
 <!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ -->

 <g>
  <title>background</title>
  <rect fill="#fff" id="canvas_background" height="471" width="582" y="-1" x="-1"/>
  <g display="none" id="canvasGrid">
   <rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%" id="svg_1"/>
  </g>
 </g>
 <g>
  <title>Layer 1</title>
  <g id="left_bowl">
   <path stroke="#000" id="svg_51" d="m42.00215,212.66238l79.2839,-108.66621l79.2839,108.66621l-158.5678,0z" fill-opacity="null" stroke-opacity="null" stroke-width="1.5" fill="none"/>
   <path stroke="#000" id="svg_52" d="m200.73877,214.0342l-0.08671,12.33213l0,0c-0.04789,6.81084 -35.64446,12.25827 -79.50715,12.16717c-43.8627,-0.09111 -79.38161,-5.68625 -79.33372,-12.49709l0.08671,-12.33213l158.84087,0.32992z" fill-opacity="null" stroke-opacity="null" stroke-width="1.5" fill="#000000"/>
  </g>
  <g id="right_bowl">
   <path stroke="#000" id="svg_27" d="m379.00755,212.6662l79.2839,-108.6662l79.2839,108.6662l-158.5678,0z" fill-opacity="null" stroke-opacity="null" stroke-width="1.5" fill="none"/>
   <path stroke="#000" id="svg_28" d="m537.74417,214.03802l-0.08671,12.33213l0,0c-0.0479,6.81085 -35.64446,12.25827 -79.50715,12.16717c-43.86271,-0.09111 -79.38162,-5.68624 -79.33372,-12.49709l0.08671,-12.33213l158.84087,0.32992z" fill-opacity="null" stroke-opacity="null" stroke-width="1.5" fill="#000000"/>
  </g>
  <path id="svg_25" d="m368.83333,323.34456c0,4.39361 -35.2949,7.95534 -78.83335,7.95534m78.83335,-7.95534l0,0c0,4.39361 -35.2949,7.95534 -78.83335,7.95534c-43.53844,0 -78.83332,-3.56172 -78.83332,-7.95534m0,0l0,0c0,-4.39361 35.29488,-7.95535 78.83332,-7.95535c43.53845,0 78.83335,3.56174 78.83335,7.95535l0,31.82139c0,4.39361 -35.2949,7.95533 -78.83335,7.95533c-43.53844,0 -78.83332,-3.56172 -78.83332,-7.95533l0,-31.82139z" stroke-opacity="null" stroke-width="1.5" fill="#BBBBBB" stroke="#000"/>
  <line stroke="#000" stroke-linecap="undefined" stroke-linejoin="undefined" id="svg_7" y2="323.509774" x2="290" y1="98.946043" x1="290" fill-opacity="null" stroke-opacity="null" stroke-width="8" fill="none"/>
  <text stroke="#000" transform="matrix(1,0,0,0.5154364705085754,0,118.00636204145849) " xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="24" id="svg_56" y="238.498738" x="315.664985" fill-opacity="null" stroke-opacity="null" stroke-width="0" fill="#000000"/>
  <g id="coins">
    <g id="coin1" class="draggable coin" transform="translate(159, 60)" number="1" weight="2" startX="159" startY="60">
      <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_4"/>
      <text y="5" text-anchor="middle" fill="black" id="svg_5">1</text>
    </g>
   <g id="coin2" class="draggable coin" transform="translate(182, 60)" number="2" weight="2" startX="182" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_6"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_9">2</text>
   </g>
   <g id="coin3" class="draggable coin" transform="translate(205, 60)" number="3" weight="2" startX="205" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_10"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_11">3</text>
   </g>
   <g id="coin4" class="draggable coin" transform="translate(228, 60)" number="4" weight="2" startX="228" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_12"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_13">4</text>
   </g>
   <g id="coin5" class="draggable coin" transform="translate(251, 60)" number="5" weight="2" startX="251" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_14"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_15">5</text>
   </g>
   <g id="coin6" class="draggable coin" transform="translate(274, 60)" number="6" weight="2" startX="274" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_16"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_17">6</text>
   </g>
   <g id="coin7" class="draggable coin" transform="translate(297, 60)" number="7" weight="2" startX="297" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_18"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_19">7</text>
   </g>
   <g id="coin8" class="draggable coin" transform="translate(320, 60)" number="8" weight="2" startX="320" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_20"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_21">8</text>
   </g>
   <g id="coin9" class="draggable coin" transform="translate(343, 60)" number="9" weight="2" startX="343" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_22"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_23">9</text>
   </g>
   <g id="coin10" class="draggable coin" transform="translate(367, 60)" number="10" weight="2" startX="367" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_24"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_26">10</text>
   </g>
   <g id="coin11" class="draggable coin" transform="translate(390, 60)" number="11" weight="2" startX="390" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_29"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_30">11</text>
   </g>
   <g id="coin12" class="draggable coin" transform="translate(413, 60)" number="12" weight="2" startX="413" startY="60">
    <circle r="10" fill="#FFFA8D" stroke="#000" id="svg_31"/>
    <text y="5" text-anchor="middle" fill="black" id="svg_32">12</text>
   </g>
  </g>
  <g id="weighButton">
   <rect rx="7" id="svg_64" height="24.49983" width="55" y="335.497682" x="262" stroke="#000" fill="#0bc64d"/>
   <text xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="16" id="weighText" y="352.99755" x="267" fill-opacity="null" stroke-opacity="null" stroke-width="0" stroke="#000" fill="#000000">Weigh</text>
  </g>
  <rect fill="none" x="43.250335" y="103.499858" width="156.999792" height="106.999858" id="left_drop_area"/>
  <rect fill="none" x="379.249889" y="103.499853" width="158.499785" height="107.999859" id="right_drop_area"/>
  <line stroke-linecap="undefined" stroke-linejoin="undefined" id="lever" y2="98.945313" x2="467.119312" y1="98.945313" x1="112.880692" fill-opacity="null" stroke-opacity="null" stroke-width="8" stroke="#000" fill="none"/>
 </g>
</svg>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Signika+Negative:300,400&display=swap');
body { 
  font-family: "Signika Negative", sans-serif; 
  font-weight: 300;
}

#weighButton {
  cursor: pointer;
}
              
            
!

JS

              
                function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}

var weights = [1, 3];
var randomNumber = getRndInteger(1, 12);
var randomWeight = weights[Math.floor(Math.random() * weights.length)];
gsap.set("#coin" + randomNumber, {attr:{weight:randomWeight}});

var coins = gsap.utils.toArray(".coin").map(function(element) {

  var x = gsap.getProperty(element, "x");
  var y = gsap.getProperty(element, "y");
  
  element._startX = x;
  element._startY = y;
  
  return element;
  
});

var leftBowlSelectors = [];
var rightBowlSelectors = [];
var bowlTilt = null;
var weighs = 0;

$( "#weighButton" ).click(function() {
  
  weighs++;
  
  $( "#weighsLeftSpan" ).text(3 - weighs);
  
  var coinsNotUsed = [];
  var coinsInLeftBowl = [];
  var coinsInRightBowl = [];
  
  var weightOfCoinsInLeftBowl = 0;
  var weightOfCoinsInRightBowl = 0;
  
  $(coins).each(function() {
    
    if (leftCoords.some(e => e.coin == this.id)) {
      coinsInLeftBowl.push(this.id);
    } else if (rightCoords.some(e => e.coin == this.id)) {
      coinsInRightBowl.push(this.id);
    } else {
      coinsNotUsed.push(this.id);
    }
    
  });
  
  $(coinsInLeftBowl).each(function() {
            
    weightOfCoinsInLeftBowl += parseInt($("#" + this).attr("weight"));
    
  });
  
  $(coinsInRightBowl).each(function() {
            
    weightOfCoinsInRightBowl += parseInt($("#" + this).attr("weight"));
    
  });
  
  $( "#weightOfCoinsInLeftBowlSpan" ).text("The coins in the left bowl weigh " + weightOfCoinsInLeftBowl);
  $( "#weightOfCoinsInRightBowlSpan" ).text("The coins in the right bowl weigh " + weightOfCoinsInRightBowl);
  
  $( "#coinsNotUsedSpan" ).text(coinsNotUsed);
  $( "#coinsInLeftBowlSpan" ).text(coinsInLeftBowl);
  $( "#coinsInRightBowlSpan" ).text(coinsInRightBowl);
  
  if (weightOfCoinsInLeftBowl > weightOfCoinsInRightBowl) {
    leftBowlIsHeavier(coinsInLeftBowl, coinsInRightBowl);
  } else if (weightOfCoinsInRightBowl > weightOfCoinsInLeftBowl) {
    rightBowlIsHeavier(coinsInLeftBowl, coinsInRightBowl);           
  } else {
    bowlsAreOfEqualWeight(coinsInLeftBowl, coinsInRightBowl);
  }
  
});

var leftCoords = [{position: 1, x: 77, y: 202, coin: null}, {position: 2, x: 99, y: 202, coin: null}, {position: 3, x: 121, y: 202, coin: null}, {position: 4, x: 143, y: 202, coin: null}, {position: 5, x: 165, y: 202, coin: null}, {position: 6, x: 88, y: 182, coin: null}, {position: 7, x: 110, y: 182, coin: null}, {position: 8, x: 132, y: 182, coin: null}, {position: 9, x: 154, y: 182, coin: null}, {position: 10, x: 99, y: 162, coin: null}, {position: 11, x: 121, y: 162, coin: null}, {position: 12, x: 143, y: 162, coin: null}];

var rightCoords = [{position: 1, x: 417, y: 202, coin: null}, {position: 2, x: 439, y: 202, coin: null}, {position: 3, x: 461, y: 202, coin: null}, {position: 4, x: 483, y: 202, coin: null}, {position: 5, x: 505, y: 202, coin: null}, {position: 6, x: 427, y: 182, coin: null}, {position: 7, x: 449, y: 182, coin: null}, {position: 8, x: 471, y: 182, coin: null}, {position: 9, x: 493, y: 182, coin: null}, {position: 10, x: 439, y: 162, coin: null}, {position: 11, x: 461, y: 162, coin: null}, {position: 12, x: 483, y: 162, coin: null}];

Draggable.create(".draggable", {
  
  onDragEnd: function() {
    
    let draggedCoin = this.target;
    let draggedCoinId = this.target.getAttribute("id");
    var thisCoinsCoords;
    var nextFreeCoords;
    
    if (this.hitTest("#left_drop_area")) {
       
      // Check if this coin already was in the left bowl
      if (leftCoords.some(e => e.coin === draggedCoinId)) {
                
        // Remove coin from these coordinates
        thisCoinsCoords = leftCoords.find(x => x.coin == draggedCoinId);
        thisCoinsCoords.coin = null;
        
      // Check if this coin was dragged from the right bowl...  
      } else if (rightCoords.some(e => e.coin === draggedCoinId)) {
        
        // Remove coin from these coordinates
        thisCoinsCoords = rightCoords.find(x => x.coin == draggedCoinId);
        thisCoinsCoords.coin = null;

      }
      
      // Find first free coordinates in leftCoords
      nextFreeCoords = leftCoords.find(x => x.coin == null);
           
      // Occupy these coordinates
      nextFreeCoords.coin = draggedCoinId;
      
      if (bowlTilt == "left") {
        gsap.to(this.target, {duration: 0.5, x: nextFreeCoords.x, y: nextFreeCoords.y +30});     
      } else if (bowlTilt == "right") {
        gsap.to(this.target, {duration: 0.5, x: nextFreeCoords.x, y: nextFreeCoords.y -30});           
      } else {
        gsap.to(this.target, {duration: 0.5, x: nextFreeCoords.x, y: nextFreeCoords.y});  
      }
      
    } else if (this.hitTest("#right_drop_area")) {
      
      // Check if this coin already was in the right bowl
      if (rightCoords.some(e => e.coin === draggedCoinId)) {
                
        // Remove coin from these coordinates
        thisCoinsCoords = rightCoords.find(x => x.coin == draggedCoinId);
        thisCoinsCoords.coin = null;
        
      // Check if this coin was dragged from the left bowl...  
      } else if (leftCoords.some(e => e.coin === draggedCoinId)) {
        
        // Remove coin from these coordinates
        thisCoinsCoords = leftCoords.find(x => x.coin == draggedCoinId);
        thisCoinsCoords.coin = null;
        
      }
      
      // Find first free coordinates in rightCoords
      nextFreeCoords = rightCoords.find(x => x.coin == null);
           
      // Occupy these coordinates
      nextFreeCoords.coin = draggedCoinId;
      
      if (bowlTilt == "left") {
        gsap.to(this.target, {duration: 0.5, x: nextFreeCoords.x, y: nextFreeCoords.y -30});     
      } else if (bowlTilt == "right") {
        gsap.to(this.target, {duration: 0.5, x: nextFreeCoords.x, y: nextFreeCoords.y +30});           
      } else {
        gsap.to(this.target, {duration: 0.5, x: nextFreeCoords.x, y: nextFreeCoords.y});  
      }
      
    } else {
      
      // Check if the coin was dragged from the left bowl
      if (leftCoords.some(e => e.coin === draggedCoinId)) {
        
        // Remove coin from these coordinates
        thisCoinsCoords = leftCoords.find(x => x.coin == draggedCoinId);
        thisCoinsCoords.coin = null;
      
      // Check if the coin was dragged from the right bowl
      } else if (rightCoords.some(e => e.coin === draggedCoinId)) {
        
        // Remove coin from these coordinates
        thisCoinsCoords = rightCoords.find(x => x.coin == draggedCoinId);
        thisCoinsCoords.coin = null;
      
      }
      
      // Move coin to it's initial position
      gsap.to(this.target, {
        x: this.target._startX,
        y: this.target._startY,
      });
      
    }
  }
});

function leftBowlIsHeavier(coinsInLeftBowl, coinsInRightBowl){
  
  leftBowlSelectors = [];
  
  rightBowlSelectors = [];
  
  $(coinsInLeftBowl).each(function() {
    
    leftBowlSelectors.push("#" + this);
    
  });
  
  $(coinsInRightBowl).each(function() {
    
    rightBowlSelectors.push("#" + this);
    
  });
  
  gsap.set('#lever', {transformOrigin: "center"});
  
  gsap.to('#lever', {duration: 2, rotation: -10});
  
  gsap.to('#left_bowl', {duration: 2, y: 30});
  
  gsap.to('#right_bowl', {duration: 2, y: -30});
  
  if (bowlTilt == "right") {
    gsap.to(leftBowlSelectors, {duration: 2, y: "+=60"});
    
    gsap.to(rightBowlSelectors, {duration: 2, y: "-=60"});      
  } else if (bowlTilt == null) {
    gsap.to(leftBowlSelectors, {duration: 2, y: "+=30"});
    
    gsap.to(rightBowlSelectors, {duration: 2, y: "-=30"});          
  }
  
  bowlTilt = "left";
  
}


function rightBowlIsHeavier(coinsInLeftBowl, coinsInRightBowl){
   
  leftBowlSelectors = [];
  
  rightBowlSelectors = [];
  
  $(coinsInLeftBowl).each(function() {
    
    leftBowlSelectors.push("#" + this);
    
  });
  
  $(coinsInRightBowl).each(function() {
    
    rightBowlSelectors.push("#" + this);
    
  });    
  
  gsap.set('#lever', {transformOrigin: "center"});
  
  gsap.to('#lever', {duration: 2, rotation: 10});
  
  gsap.to('#right_bowl', {duration: 2, y: 30});
  
  gsap.to('#left_bowl', {duration: 2, y: -30}); 
  
  if (bowlTilt == "left") {
    gsap.to(rightBowlSelectors, {duration: 2, y: "+=60"});
    
    gsap.to(leftBowlSelectors, {duration: 2, y: "-=60"});    
  } else if (bowlTilt == null) {
    gsap.to(rightBowlSelectors, {duration: 2, y: "+=30"});
    
    gsap.to(leftBowlSelectors, {duration: 2, y: "-=30"});               
  }
  
  bowlTilt = "right";

}

function bowlsAreOfEqualWeight(coinsInLeftBowl, coinsInRightBowl) {
  
  leftBowlSelectors = [];
  
  rightBowlSelectors = [];
  
  $(coinsInLeftBowl).each(function() {
    
    leftBowlSelectors.push("#" + this);
    
  });
  
  $(coinsInRightBowl).each(function() {
    
    rightBowlSelectors.push("#" + this);
    
  });
  
  if (bowlTilt == "left") {
      gsap.to(rightBowlSelectors, {duration: 2, y: "+=30"});

      gsap.to(leftBowlSelectors, {duration: 2, y: "-=30"});
  }
  
  if (bowlTilt == "right") {
      gsap.to(leftBowlSelectors, {duration: 2, y: "+=30"});

      gsap.to(rightBowlSelectors, {duration: 2, y: "-=30"});
  }

  gsap.set('#lever', {transformOrigin: "center"});
  
  gsap.to('#lever', {duration: 2, rotation: 0});
  
  gsap.to('#right_bowl', {duration: 2, y: 0});
  
  gsap.to('#left_bowl', {duration: 2, y: 0});
  
  bowlTilt = null;

}


/*
function checkIfCoinsShouldMove(emptyPosition, array) {
  
  var occupiedPositions = [];
  var coinOnOccupiedPosition;
  var coinToMove;
  
  $(array).each(function() {
    
    if (this.coin !== null) {
      occupiedPositions.push(this.position);
    }
    
  });
  
  switch (true) {

    case ((emptyPosition === 1) && (occupiedPositions.includes(6)) && (occupiedPositions.includes(10))):
      // Mynt på position 6 ska flyttas till position 1 och mynt på position 10 ska flyttas till position 6
      coinOnOccupiedPosition = leftCoords.find(x => x.position == 6);
      coinToMove = "#" + coinOnOccupiedPosition.coin;
      gsap.to(coinToMove, {duration: 0.5, x: 10, y: 10});
      coinOnOccupiedPosition.position = 1;
      
      coinOnOccupiedPosition = leftCoords.find(x => x.position == 10);
      coinToMove = "#" + coinOnOccupiedPosition.coin;
      gsap.to(coinToMove, {duration: 0.5, x: 10, y: 10});
      coinOnOccupiedPosition.position = 6;
      break;
    case ((emptyPosition === 1) && (occupiedPositions.includes(6))):
      // Mynt på position 6 ska flyttas till position 1
      coinOnOccupiedPosition = leftCoords.find(x => x.position == 6);
      coinToMove = "#" + coinOnOccupiedPosition.coin;
      gsap.to(coinToMove, {duration: 0.5, x: 10, y: 10});
      coinOnOccupiedPosition.position = 1;
      break;
    case ((emptyPosition === 5) && (occupiedPositions.includes(9)) && (occupiedPositions.includes(12))):
      // Mynt på position 9 ska flyttas till position 5 och mynt på position 12 ska flyttas till position 9
      break;
    case ((emptyPosition === 5) && (occupiedPositions.includes(9))):
      // Mynt på position 9 ska flyttas till position 5
      break;
    case ((emptyPosition === 6) && (occupiedPositions.includes(10))):
      // Mynt på position 10 ska flyttas till position 6
      break;
    case ((emptyPosition === 9) && (occupiedPositions.includes(12))):
      // Mynt på position 12 ska flyttas till position 9
      break;
    default:
      // Do nothing
  }
  
  console.log(emptyPosition, occupiedPositions);

}
*/
              
            
!
999px

Console