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

              
                <h2>Drag and throw green square</h2>

<div id="container">
  <div id="box">
    <div id="coords">x: o<br>y: 0</div>
    <div id="velocity">velocity (x): 0<br>velocity (y): 0</div>
    <div id="end">endX: 0<br>endY: 0</div>
  </div>
</div>
              
            
!

CSS

              
                body{
  background-color:#555;
  margin:20px;
  color:white;
}

h2 {
  font-size:20px;
}
#container{
  background-color:black;
  width:1000px;
  height:400px;
}

#box {
  position:absolute;
  padding:6px;
  width:125px;
  background-color:green;
}
              
            
!

JS

              
                var coords = document.getElementById("coords"),  
    velocity = document.getElementById("velocity"),
    end = document.getElementById("end"),
    tracker = ThrowPropsPlugin.track(document.getElementById("box"), "x,y"),
    draggable;

Draggable.create("#box", {
  type:"x,y",
  throwProps:true,
  edgeResistance:0.5,
  bounds:"#container",
  onDragStart: function() {
    TweenLite.ticker.addEventListener("tick", updateVelocity);
  },
  
  //"this" in any of the callbacks refers to the Draggable instance.
  onDragEnd: function() {
    end.innerHTML = "endX: " + Math.round(this.endX) + "<br>endY: " + Math.round(this.endY);
  },

  //onThrowComplete is used by the ThrowProps tween. We'll stop updating the velocity when the tween is done.
  onThrowComplete: function() {
    TweenLite.ticker.removeEventListener("tick", updateVelocity);
  }
});

//get the Draggable instance so that we can check its x and y properties in the updateVelocity() function.
draggable = Draggable.get("#box");

//we call this function each time the core engine updates its values (typically synced with the screen refresh), reporting the current position and velocity
function updateVelocity() {
  coords.innerHTML = "x: " + Math.round(draggable.x) + "<br>y: " + Math.round(draggable.y);
  velocity.innerHTML = "velocity (x): " + Math.round(tracker.getVelocity("x")) + "<br>velocity (y): " + Math.round(tracker.getVelocity("y"));
}
              
            
!
999px

Console