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

<div>
  <button type="button" id="check">Check</button>
</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" number="1" weight="2">
    <circle cx="159" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_4"/>
    <text x="159" y="65" text-anchor="middle" fill="black" id="svg_5">1</text>
   </g>
   <g id="coin2" class="draggable coin" number="2" weight="2">
    <circle cx="182" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_6"/>
    <text x="182" y="65" text-anchor="middle" fill="black" id="svg_9">2</text>
   </g>
   <g id="coin3" class="draggable coin" number="3" weight="2">
    <circle cx="205" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_10"/>
    <text x="205" y="65" text-anchor="middle" fill="black" id="svg_11">3</text>
   </g>
   <g id="coin4" class="draggable coin" number="4" weight="2">
    <circle cx="228" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_12"/>
    <text x="228" y="65" text-anchor="middle" fill="black" id="svg_13">4</text>
   </g>
   <g id="coin5" class="draggable coin" number="5" weight="2">
    <circle cx="251" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_14"/>
    <text x="251" y="65" text-anchor="middle" fill="black" id="svg_15">5</text>
   </g>
   <g id="coin6" class="draggable coin" number="6" weight="2">
    <circle cx="274" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_16"/>
    <text x="274" y="65" text-anchor="middle" fill="black" id="svg_17">6</text>
   </g>
   <g id="coin7" class="draggable coin" number="7" weight="2">
    <circle cx="297" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_18"/>
    <text x="297" y="65" text-anchor="middle" fill="black" id="svg_19">7</text>
   </g>
   <g id="coin8" class="draggable coin" number="8" weight="2">
    <circle cx="320" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_20"/>
    <text x="320" y="65" text-anchor="middle" fill="black" id="svg_21">8</text>
   </g>
   <g id="coin9" class="draggable coin" number="9" weight="2">
    <circle cx="343" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_22"/>
    <text x="343" y="65" text-anchor="middle" fill="black" id="svg_23">9</text>
   </g>
   <g id="coin10" class="draggable coin" number="10" weight="2">
    <circle cx="367" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_24"/>
    <text x="367" y="65" text-anchor="middle" fill="black" id="svg_26">10</text>
   </g>
   <g id="coin11" class="draggable coin" number="11" weight="2">
    <circle cx="390" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_29"/>
    <text x="390" y="65" text-anchor="middle" fill="black" id="svg_30">11</text>
   </g>
   <g id="coin12" class="draggable coin" number="12" weight="2">
    <circle cx="413" cy="60" r="10" fill="#FFFA8D" stroke="#000" id="svg_31"/>
    <text x="413" y="65" 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="50.499649" y="335.497682" x="264.750179" stroke="#000" fill="#0bc64d"/>
   <text xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="16" id="svg_65" y="352.99755" x="267.468921" 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;
}
              
            
!

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 = ["coin1", "coin2", "coin3", "coin4", "coin5", "coin6", "coin7", "coin8", "coin9", "coin10", "coin11", "coin12"];


$( "#check" ).click(function() {
  
  var coinsNotUsed = [];
  var coinsInLeftBowl = [];
  var coinsInRightBowl = [];
  
  var weightOfCoinsInLeftBowl = 0;
  var weightOfCoinsInRightBowl = 0;
  
  $(coins).each(function() {
    
    if (leftCoords.some(e => e.coin == this)) {
      coinsInLeftBowl.push(this);
    } else if (rightCoords.some(e => e.coin == this)) {
      coinsInRightBowl.push(this);
    } else {
      coinsNotUsed.push(this);
    }
    
  });
  
  $(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();           
  } else {
    bowlsAreOfEqualWeight();
  }
  
});

var leftCoords = [{position: 1, x: 67, y: 192, coin: null}, {position: 2, x: 89, y: 192, coin: null}, {position: 3, x: 111, y: 192, coin: null}, {position: 4, x: 133, y: 192, coin: null}, {position: 5, x: 155, y: 192, coin: null}, {position: 6, x: 78, y: 172, coin: null}, {position: 7, x: 100, y: 172, coin: null}, {position: 8, x: 122, y: 172, coin: null}, {position: 9, x: 144, y: 172, coin: null}, {position: 10, x: 89, y: 152, coin: null}, {position: 11, x: 111, y: 152, coin: null}, {position: 12, x: 133, y: 152, coin: null}];

var rightCoords = [{position: 1, x: 407, y: 192, coin: null}, {position: 2, x: 429, y: 192, coin: null}, {position: 3, x: 451, y: 192, coin: null}, {position: 4, x: 473, y: 192, coin: null}, {position: 5, x: 495, y: 192, coin: null}, {position: 6, x: 417, y: 172, coin: null}, {position: 7, x: 439, y: 172, coin: null}, {position: 8, x: 461, y: 172, coin: null}, {position: 9, x: 483, y: 172, coin: null}, {position: 10, x: 429, y: 152, coin: null}, {position: 11, x: 451, y: 152, coin: null}, {position: 12, x: 473, y: 152, coin: null}];

Draggable.create(".draggable", {
  
  onDragEnd: function() {
    
    let draggedCoin = this.target;
    let draggedCoinId = this.target.getAttribute("id");
    var draggedCoinPosition = draggedCoin.getBBox();
    var thisCoinsCoords;
    var nextFreeCoords;
    var targetX;
    var targetY;
    
    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;
                
        // Move coin to it's initial position
        gsap.to(this.target, 0.5, {x: 0, y: 0, ease: Bounce.easeOut});
        
      // 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;
      
      targetX = nextFreeCoords.x - draggedCoinPosition.x;
      
      targetY = nextFreeCoords.y - draggedCoinPosition.y;
      
      gsap.to(this.target, {duration: 0.5, x: targetX, y: targetY});
      
    } 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;
                
        // Move coin to it's initial position
        gsap.to(this.target, 0.5, {x: 0, y: 0, ease: Bounce.easeOut});
        
      // 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;
      
      targetX = nextFreeCoords.x - draggedCoinPosition.x;
      
      targetY = nextFreeCoords.y - draggedCoinPosition.y;
      
      gsap.to(this.target, {duration: 0.5, x: targetX, y: targetY});
      
    } 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;
                
        // Move coin to it's initial position
        gsap.to(this.target, 0.5, {x: 0, y: 0, ease: Bounce.easeOut});
      
      // 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, 0.5, {x: 0, y: 0, ease: Bounce.easeOut});
      
      } else {
        // Just put the coin back to it's initial position
      gsap.to(this.target, 0.5, {x: 0, y: 0, ease: Bounce.easeOut});
      }
    }
  }
});

function leftBowlIsHeavier(coinsInLeftBowl, coinsInRightBowl){
  
  var leftBowlSelectors = [];
  
  var 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(leftBowlSelectors, {duration: 2, y: 30});
    
  gsap.to('#right_bowl', {duration: 2, y: -30});
  
  gsap.to(rightBowlSelectors, {duration: 2, y: -30});
  
}


function rightBowlIsHeavier(){
  
  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});

}

function bowlsAreOfEqualWeight(){

  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});

}


/*
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