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>Moving Leg starter</h1>

<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

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

              
            
!

JS

              
                // total number of steps in animation

var steps = 100;

// leg parameters for animation

var params = {
    ankleRotation: -Math.PI/3,         // angle of foot relative to lower leg
    kneeRotation: -2*Math.PI/3,        // angle of lower leg relative to thigh
    hipRotation: -Math.PI/4,           // angle of hip in scene coordinate frame
    ankleChange: (Math.PI/3)/steps,    // change of ankleRotation per frame
    kneeChange: (2*Math.PI/3)/steps,   // change of kneeRotation per frame
    hipChange: (3*Math.PI/4)/steps,    // change of hipRotation per frame
    footLength: 10,                    // dimensions of parts of leg
    calfLength: 20,
    thighLength: 25,
    step: 0                            // frame number in animation
};

var scene = new THREE.Scene();

var renderer = new THREE.WebGLRenderer();

TW.mainInit(renderer,scene);

TW.cameraSetup(renderer,
               scene,
               {minx: 0, maxx: 5,
                maxy: 0, miny: -(params.thighLength + params.calfLength),
                minz: -3, maxz: 3});

var leg;

function makeLeg(scene, params) {
   // makes a leg going down from the scene origin, with joints rotating around 
   // the z axis. Meshes are shoe, calf, and thigh. Composite objects are foot, 
   // lowerleg, and leg

   foot = new THREE.Object3D();
   var shoe = TW.createMesh(new THREE.CylinderGeometry(2, 1, params.footLength));
   shoe.position.x = params.footLength/2;
   shoe.rotation.z = Math.PI/2;
   foot.add(shoe);

   lowerleg = new THREE.Object3D();
   var calf = TW.createMesh(new THREE.CylinderGeometry(3, 2, params.calfLength));
   calf.position.y = -params.calfLength/2;
   lowerleg.add(calf);
   foot.position.y = -params.calfLength;
   foot.rotation.z = params.ankleRotation;
   lowerleg.add(foot);

   leg = new THREE.Object3D();
   var thigh = TW.createMesh(new THREE.CylinderGeometry(5, 4, params.thighLength));
   thigh.position.y = -params.thighLength/2;
   leg.add(thigh);
   lowerleg.position.y = -params.thighLength;
   lowerleg.rotation.z = params.kneeRotation;
   leg.add(lowerleg);

   leg.rotation.z = params.hipRotation;
   scene.add(leg); 
}

// firstState() resets the leg to the initial state and renders the scene

function firstState() {
   params.ankleRotation = -Math.PI/3;
   params.kneeRotation = -2*Math.PI/3;
   params.hipRotation = -Math.PI/4;
   params.step = 0;
   if ( leg != null ) {
       scene.remove(leg);
   }
   makeLeg(scene, params);
   TW.render();
}

firstState();

// COMPLETE updateState() TO UPDATE THE ANKLE, KNEE, AND HIP ROTATIONS, AND INCREMENT
// THE STEP NUMBER, FOR THE NEXT FRAME OF THE ANIMATION

function updateState() {



}

// COMPLETE oneStep() TO PERFORM ONE STEP OF THE ANIMATION: REMOVE THE CURRENT LEG
// OBJECT, UPDATE THE STATE, REMAKE THE LEG, AND RENDER THE SCENE

function oneStep() {



}

// stored so that we can cancel the animation if we want

var animationID = null;

// MODIFY animate() SO THAT THE ANIMATION STOPS WHEN params.step REACHES steps

function animate (timestamp) {
   oneStep();


   animationID = requestAnimationFrame(animate);
}

// stopAnimation() halts the animation

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

// keyboard controls for the animation control

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

              
            
!
999px

Console