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

              
                <p id="display"></p>
<button id="resetButton">リセット</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.js"></script>
              
            
!

CSS

              
                
              
            
!

JS

              
                // cannonjsのhelloWorld (https://github.com/schteppe/cannon.js)

var lastTime
var fixedTimeStep = 1.0 / 60.0; // seconds
var maxSubSteps = 3;
var world, sphereBody, groundBody
var loopFlag = false
var display = document.getElementById("display"),
    resetButton = document.getElementById("resetButton")

// シミレーション初期化のボタン設置
resetButton.addEventListener("click", function(){
  if(loopFlag) return
  initWorld() 
  simloop()
});

window.onload = function () {
  initWorld()
  simloop()
}

function initWorld() {
  // Setup our world
  world = new CANNON.World();
  world.gravity.set(0, 0, -9.82); // m/s²

  // Create a sphere
  var radius = 1; // m
  sphereBody = new CANNON.Body({
    mass: 5, // kg
    position: new CANNON.Vec3(0, 0, 10), // m
    shape: new CANNON.Sphere(radius)
  });
  world.addBody(sphereBody);

  // Create a plane
  groundBody = new CANNON.Body({
    mass: 0 // mass == 0 makes the body static
  });
  var groundShape = new CANNON.Plane();
  groundBody.addShape(groundShape);
  world.addBody(groundBody);
}

function simloop(time) {
  loopFlag = true
  var prePositionZ = sphereBody.position.z
  if (lastTime !== undefined) {
    var dt = (time - lastTime) / 1000;
    world.step(fixedTimeStep, dt, maxSubSteps);
  }
  console.log("Sphere z position: " + sphereBody.position.z);
  // コンソールだけだと寂しいので、hmtlに出してあげる
  display.innerHTML = "Sphere z position: " + sphereBody.position.z
  lastTime = time;
  
  // spher止まったらloopも止めちゃう
  if(sphereBody.position.z < 9 && sphereBody.position.z == prePositionZ){
    loopFlag = false
    return
  }
  
  requestAnimationFrame(simloop);
}
              
            
!
999px

Console