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

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 class="box">
  <div class="ball"></div>
</div>

<div class="controls">  
  <div class="table-container">
    <table class="table table-condensed table-bordered">
      <thead>
        <tr>
          <th class="cell-1"></th>
          <th class="cell-2">TWEEN</th>
          <th class="cell-3">MOD</th>
        </tr>
      </thead>      
      <tbody>
        <tr>
          <th class="cell-1">X</th>
          <td id="tween-x">0</td>
          <td id="mod-x">0</td>
        </tr>
        <tr>
          <th class="cell-1">Y</th>
          <td id="tween-y">0</td>
          <td id="mod-y">0</td>
        </tr>
      </tbody>      
    </table>   
  </div>
  
  <div class="button-container">
    <button id="play" class="btn btn-primary">Play</button>
  </div> 
</div>
              
            
!

CSS

              
                html, body {
  height: 100%;
}

body {
  overflow: hidden;
}

* {
  box-sizing: border-box;
}

.box {  
  width: 70%;
  height: 60%;
  min-width: 300px;
  min-height: 300px;  
  top: 50%;
  left: 50%;
  border: 1px solid #333;
  position: absolute;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.ball {
  position: absolute;
  height: 100px;
  width: 100px;
  top: 0;
  left: 0;
  border-radius: 50%;
  background: red;
  border: 2px solid #222;
}

.controls {
  position: absolute;
  top: 0;
  left: 0;
  // background: white;
}

.table-container {
  width: 300px;
}

.button-container {  
  padding: 5px;
}

button {
  width: 90px;
  margin-left: 5px;
}

.table {
  margin-bottom: 5px;
}

.cell-1 {
  width: 25px;
  text-align: center;
}

.cell-2,
.cell-3 {
  width: 100px;
}


              
            
!

JS

              
                
var $tweenX = $("#tween-x");
var $tweenY = $("#tween-y");

var $modX = $("#mod-x");
var $modY = $("#mod-y");

var ball = $(".ball");
var box  = $(".box");

var minX = 0;
var minY = 0;
var size = ball.outerWidth();
var maxX = box.outerWidth()  - size;
var maxY = box.outerHeight() - size;

var startX = maxX / 2 - size / 2;
var startY = 100;

TweenLite.set(ball, { x: startX, y: startY });

$("#play").click(() => {
     
  TweenMax.to(ball, random(4, 8), {
    x: random(4000, 8000) * randomSign(), 
    y: random(4000, 8000) * randomSign(),    
    modifiers: { x: modX, y: modY }
  });  
});

function modX(x) {
  $tweenX.text(x.toFixed(1));
    
  if (x > maxX || x < minX) {
    
    var delta = ((x % maxX) + maxX) % maxX;
    var start = x > maxX ? 1 : 0;
    var ratio = x / maxX + start;
    var even  = !(ratio & 1);
    
    x = even ? maxX - delta : minX + delta;
  } 
  
  $modX.text(x.toFixed(1));
  return x;
}

function modY(y) {
  $tweenY.text(y.toFixed(1));
    
  if (y > maxY || y < minY) {
    
    var delta = ((y % maxY) + maxY) % maxY;
    var start = y > maxY ? 1 : 0;
    var ratio = y / maxY + start;
    var even  = !(ratio & 1);
    
    y = even ? maxY - delta : minY + delta;
  }   
  
  $modY.text(y.toFixed(1));
  return y;
}

$(window).resize(() => {
  maxX = box.outerWidth()  - size;
  maxY = box.outerHeight() - size;  
});

function randomSign() {
  return Math.random() < 0.5 ? -1 : 1;
}

function random(min, max) {
  if (max == null) { max = min; min = 0; }
  return Math.random() * (max - min) + min;
}

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






              
            
!
999px

Console