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

              
                <h1>Bouncing Ball that Moves</h1>

<p>Click outside of the grey canvas and then press one of these keys:</p>
<ul>
  <li>0:  reset animation to initial state
  <li>1:  take one step
  <li>g:  "go" &mdash; start the animation <q>loop</q>
  <li>SPC:  stop the animation
</ul>

              
            
!

CSS

              
                body {
  max-width: 100%;
}
/* feel free to style the canvas any way you want. If you want it to
      use the entire window, set width: 100% and height: 100%. */

canvas {
  width: 80%;
  height: 500px;
  display: block;
  margin: 10px auto;
}

              
            
!

JS

              
                // parameters of the scene and animation

var guiParams = {
    ballRadius: 1,
    ballBouncePeriod: 3,  // time to bounce once, in arbitrary time units
    maxBallHeight: 8,     // max y position of the center of the ball
    deltaT: 0.035,        // time between steps, in arbitrary time units
    ballVelocityX: 0.8,   // movement per frame in X direction
    ballInitialX: 0       // initial X position
};

var scene = new THREE.Scene();

// create green ground plane and add to the scene

var ground = new THREE.Mesh(new THREE.PlaneGeometry(10,10),
                            new THREE.MeshBasicMaterial({color:0x009900}) );
ground.rotation.x = -Math.PI/2;
scene.add(ground);

var renderer = new THREE.WebGLRenderer();

TW.mainInit(renderer,scene);

TW.cameraSetup(renderer, scene, 
               {minx: -5, maxx: 5,
                miny: 0, maxy: guiParams.maxBallHeight+guiParams.ballRadius,
                minz: -5, maxz: 5});

// state variables for the animation

var animationState;

// resets the animationState to its initial setting

function resetAnimationState() {
   animationState = {
       time: 0,
       ballHeight: guiParams.maxBallHeight,    // fall from maximum height
       ballPositionX: guiParams.ballInitialX   // reset to initial X position
   };
}

resetAnimationState();

// ball needs to be global so we can update its position

var ball;

function makeBall () {
    scene.remove(ball);
    var ballG =  new THREE.SphereGeometry(guiParams.ballRadius,30,30);
    var ballMat = new THREE.MeshNormalMaterial();
    ball = new THREE.Mesh(ballG,ballMat);
    ball.position.set(animationState.ballPositionX, animationState.ballHeight, 0);
    scene.add(ball);
}

makeBall();

// transforms x in the range [minx,maxx] to y in the range [miny,maxy]

function linearMap (x, minx, maxx, miny, maxy) {
    // t is in the range [0,1]
    t = (x-minx)/(maxx-minx);
    y = t*(maxy-miny)+miny;
    return y;
};

// sets the position of the ball based on current time

function setBallPosition (time) {
    // rescale the time dimension so that the period of bouncing maps to pi
    var angle = time * Math.PI / guiParams.ballBouncePeriod; 
    var abs_cos = Math.abs(Math.cos(angle));
    var ballHeight = linearMap(abs_cos, 0, 1, 
                               guiParams.ballRadius, guiParams.maxBallHeight);
    ball.position.y = ballHeight;
    var ballX = guiParams.ballInitialX + guiParams.ballVelocityX * time;
    ball.position.x = ballX;
    return [ballX, ballHeight];
}

// resets the ball to the initial state and renders the scene

function firstState() {
    resetAnimationState();
    var state = setBallPosition(animationState.time);
    animationState.ballPositionX = state[0];
    animationState.ballHeight = state[1];
    TW.render();
}

firstState();

// updates the state of the animation (time and ballHeight)

function updateState() {
    animationState.time += guiParams.deltaT;
    var state = setBallPosition(animationState.time);
    animationState.ballPositionX = state[0];
    animationState.ballHeight = state[1];
    console.log("time is " + animationState.time);
    console.log("ball height and position are " + animationState.ballHeight + " and " + animationState.ballPositionX);
}

// performs one step of the animation
                
function oneStep() {
    updateState();
    TW.render();
}
    
// stored so that we can cancel the animation if we want

var animationId = null;                

// starts continuous animation loop

function animate(timestamp) {
    oneStep();
    animationId = requestAnimationFrame(animate);
}

// halts the animation

function stopAnimation() {
    if (animationId != null) {
        cancelAnimationFrame(animationId);
        console.log("Cancelled animation using " + animationId);
    }
}

// setup keyboard controls

TW.setKeyboardCallback("0", firstState, "reset animation");
TW.setKeyboardCallback("1", oneStep, "advance by one step");
TW.setKeyboardCallback("g", animate, "go:  start animation");
TW.setKeyboardCallback(" ", stopAnimation, "stop animation");

// setup gui controls

var gui = new dat.GUI();
gui.add(guiParams, "ballRadius", 0.1, 3).onChange(
                    function () { makeBall(); TW.render() });
gui.add(guiParams, "deltaT", 0.001, 0.999).step(0.001);
gui.add(guiParams, "ballBouncePeriod", 1, 30).step(1);

              
            
!
999px

Console