cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <div id="world" />

<svg id="trigCycle" width="160" height="160" xmlns="http://www.w3.org/2000/svg" >
  <circle cx="60" cy="60" r="50" stroke="#CCC" stroke-width="1" fill="none" />
	<path id="trigoArc" fill="#F00" opacity=".2" />
	<line id="axisCos" x1="0" y1="60" x2="120" y2="60" stroke-width="1" stroke="#CCC" />
	<line id="axisSin" x1="60" y1="0" x2="60" y2="120" stroke-width="1" stroke="#CCC" />
	<line id="trigoLine" x1="60" y1="60" x2="120" y2="60" stroke-width="1" stroke="#f00" />
	<circle id="trigoPoint" cx="0" cy="0" r="5" fill="#F00" />
	<circle id="cosPoint" cx="0" cy="60" r="2" fill="#0A0" />
	<circle id="sinPoint" cx="60" cy="0" r="2" fill="#00F" />
	<line id="cosLine" x1="60" y1="60" x2="110" y2="60" stroke-width="1" stroke="#0A0" />
	<line id="sinLine" x1="60" y1="60" x2="60" y2="110" stroke-width="1" stroke="#00f" />
	<line id="projSinLine" x1="60" y1="60" x2="110" y2="60" stroke-width="1" stroke="#CCC" stroke-dasharray="1, 2" />
	<line id="projCosLine" x1="60" y1="60" x2="60" y2="110" stroke-width="1" stroke="#CCC" stroke-dasharray="1, 2" />
	<text id="cosLabel" class="trigoText" x="122" y="62" fill="#0A0" >cos</text>
	<text id="sinLabel" class="trigoText" x="62" y="120" fill="#00F" >sin</text>
</svg>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Open+Sans:600);

body{
  background-color: #dbe6e6;
  font-family: "Open Sans";
}

#world{
	position: absolute;
	width:100%;
	height: 100%;
  overflow:hidden;
}

.marker{
  position: absolute;
  left: 50%;
  transform: translate(-50%,0%);
  font-size: 18px;
  padding:4px;
}


.trigoText{
	font-size: 9px;
	font-weight: bold;
	text-transform: uppercase;
}

#trigCycle{
  position: absolute;
  left: 20px;
  top: 20px;
}

.angle{
  color:#F00;
}
            
          
!
            
              //THREEJS RELATED VARIABLES 

var scene,
  camera, fieldOfView, aspectRatio, nearPlane, farPlane,
  gobalLight, shadowLight, backLight,
  renderer,
  container,
  controls;

// OTHER VARIABLES

var PI = Math.PI;
var hero;
var container;

// MATERIALS

var brownMat = new THREE.MeshStandardMaterial({
    color: 0x401A07,
    side:THREE.DoubleSide,
    shading:THREE.SmoothShading,
    roughness:1,
  });

var blackMat = new THREE.MeshPhongMaterial({
    color: 0x100707,
    shading:THREE.FlatShading,
  });
  
var redMat = new THREE.MeshPhongMaterial({
    color: 0xAA5757,
    shading:THREE.FlatShading,
  });

var blueMat = new THREE.MeshPhongMaterial({
    color: 0x5b9696,
    shading:THREE.FlatShading,
  });

var whiteMat = new THREE.MeshPhongMaterial({
    color: 0xffffff,
    shading:THREE.FlatShading,
  });

var currentMaterial = new THREE.MeshPhongMaterial({
    color: 0xff0000,
    shading:THREE.FlatShading,
  });
  

//INIT THREE JS, SCREEN AND MOUSE EVENTS

function initScreenAnd3D() {
  container = document.getElementById('world');
  HEIGHT = container.offsetHeight;
  WIDTH = container.width;
  windowHalfX = WIDTH / 2;
  windowHalfY = HEIGHT / 2;

  scene = new THREE.Scene();
  
  scene.fog = new THREE.Fog(0xd6eae6, 150,300);
  
  aspectRatio = WIDTH / HEIGHT;
  fieldOfView = 50;
  nearPlane = 1;
  farPlane = 2000;
  camera = new THREE.PerspectiveCamera(
    fieldOfView,
    aspectRatio,
    nearPlane,
    farPlane
  );
  camera.position.x = 0;
  camera.position.z = 100;
  camera.position.y = 0;
  //camera.lookAt(new THREE.Vector3(0, 30, 0));

  renderer = new THREE.WebGLRenderer({
    alpha: true,
    antialias: true
  });
  renderer.setSize(WIDTH, HEIGHT);
  renderer.setPixelRatio(window.devicePixelRatio ? window.devicePixelRatio : 1)
  renderer.shadowMap.enabled = true;

  container.appendChild(renderer.domElement);

  window.addEventListener('resize', handleWindowResize, false);

  handleWindowResize();
}

function handleWindowResize() {
  HEIGHT = container.offsetHeight;
  WIDTH = container.offsetWidth;
  windowHalfX = WIDTH / 2;
  windowHalfY = HEIGHT / 2;
  renderer.setSize(WIDTH, HEIGHT);
  camera.aspect = WIDTH / HEIGHT;
  camera.updateProjectionMatrix();
}

function createLights() {
  globalLight = new THREE.AmbientLight(0xffffff, 1);
  shadowLight = new THREE.DirectionalLight(0xffffff, 1);
  shadowLight.position.set(10, 8, 8);
  shadowLight.castShadow = true;
  shadowLight.shadow.camera.left = -40;
  shadowLight.shadow.camera.right = 40;
  shadowLight.shadow.camera.top = 40;
  shadowLight.shadow.camera.bottom = -40;
  shadowLight.shadow.camera.near = 1;
  shadowLight.shadow.camera.far = 1000;
  shadowLight.shadow.mapSize.width = shadowLight.shadow.mapSize.height = 2048;
  scene.add(globalLight);
  scene.add(shadowLight);
}

Hero = function() {
  this.runningCycle = 0;
  this.mesh = new THREE.Group();
  this.body = new THREE.Group();
  this.mesh.add(this.body);
  
  var torsoGeom = new THREE.CubeGeometry(8,8,8, 1);//
  this.torso = new THREE.Mesh(torsoGeom, blueMat);
  this.torso.position.y = 8;
  this.torso.castShadow = true;
  this.body.add(this.torso);
  
  var handGeom = new THREE.CubeGeometry(3,3,3, 1);
  this.handR = new THREE.Mesh(handGeom, brownMat);
  this.handR.position.z=7;
  this.handR.position.y=8;
  this.body.add(this.handR);
  
  this.handL = this.handR.clone();
  this.handL.position.z = - this.handR.position.z;
  this.body.add(this.handL);
  
  var headGeom = new THREE.CubeGeometry(16,16,16, 1);//
  this.head = new THREE.Mesh(headGeom, blueMat);
  this.head.position.y = 21;
  this.head.castShadow = true;
  this.body.add(this.head);
  
  var legGeom = new THREE.CubeGeometry(8,3,5, 1);
  
  this.legR = new THREE.Mesh(legGeom, brownMat);
  this.legR.position.x = 0;
  this.legR.position.z = 7;
  this.legR.position.y = 0;
  this.legR.castShadow = true;
  this.body.add(this.legR);
  
  this.legL = this.legR.clone();
  this.legL.position.z = - this.legR.position.z;
  this.legL.castShadow = true;
  this.body.add(this.legL);

  this.body.traverse(function(object) {
    if (object instanceof THREE.Mesh) {
      object.castShadow = true;
      object.receiveShadow = true;
    }
  });
}

Hero.prototype.run = function(){
  this.runningCycle += .03;
  var t = this.runningCycle;
  t = t % (2*PI);
  var amp = 4;
  
  this.legR.position.x =  Math.cos(t) * amp;
  this.legR.position.y = Math.max (0, - Math.sin(t) * amp);  
  
  this.legL.position.x =  Math.cos(t + PI) * amp;
  this.legL.position.y = Math.max (0, - Math.sin(t + PI) * amp);
  
  if (t>PI){
    this.legR.rotation.z = Math.cos(t * 2 + PI/2) * PI/4; 
    this.legL.rotation.z = 0; 
  } else{
    this.legR.rotation.z = 0; 
    this.legL.rotation.z = Math.cos(t * 2 + PI/2) *  PI/4;  
  }

  this.torso.position.y = 8 - Math.cos(  t * 2 ) * amp * .2;
  this.torso.rotation.y = -Math.cos( t + PI ) * amp * .05;
  
  this.head.position.y = 21 - Math.cos(  t * 2 ) * amp * .3;
  this.head.rotation.x = Math.cos( t ) * amp * .02;
  this.head.rotation.y =  Math.cos( t ) * amp * .01;

  this.handR.position.x = -Math.cos( t ) * amp;
  this.handR.rotation.z = -Math.cos( t ) * PI/8;
  this.handL.position.x = -Math.cos( t + PI) * amp;
  this.handL.rotation.z = -Math.cos( t + PI) * PI/8;
}

function createHero() {
  hero = new Hero();
  hero.mesh.position.y=-15;
  scene.add(hero.mesh);
}
var rot = -1;

function loop(){
  
  updateTrigoCircle(hero.runningCycle);
  hero.run();
  rot+=.01;
  hero.mesh.rotation.y = -Math.PI/4 + Math.sin(rot * Math.PI/8);
  render();  
  requestAnimationFrame(loop);
}

function render(){
  renderer.render(scene, camera);
}

window.addEventListener('load', init, false);

function init(event){
  initScreenAnd3D();
  createLights();
  createHero();
  loop();
}


// Trigo Circle

var PI = Math.PI;
var trigoArc = document.getElementById("trigoArc");
var trigoLine = document.getElementById("trigoLine");
var trigoPoint = document.getElementById("trigoPoint");
var cosPoint = document.getElementById("cosPoint");
var sinPoint = document.getElementById("sinPoint");
var cosLine = document.getElementById("cosLine");
var sinLine = document.getElementById("sinLine");
var projSinLine = document.getElementById("projSinLine");
var projCosLine = document.getElementById("projCosLine");

var cAngle = 0;
var tp = {
	radiusArc: 10,
	centerX: 60,
	centerY: 60, 
	radiusLines: 50,
};

function updateTrigoCircle(angle) {
	angle %= PI * 2;
	var cos = Math.cos(angle);
	var sin = Math.sin(angle);
  var start = {
    x: tp.centerX + tp.radiusArc * cos,
		y: tp.centerY + tp.radiusArc * sin
	};
	var end = {
		x: tp.centerX + tp.radiusArc,
		y: tp.centerY
	};

	var arcSweep = angle >= PI ? 1 : 0;
	var d = [ "M", tp.centerX, tp.centerY, 
					 	"L", start.x, start.y, 
					 	"A", tp.radiusArc, tp.radiusArc, 0, arcSweep, 0, end.x, end.y,
					 	"L", tp.centerX, tp.centerY
	].join(" ");
	
	trigoArc.setAttribute("d", d);
	trigoLine.setAttribute("x2", tp.centerX + cos * tp.radiusLines);
	trigoLine.setAttribute("y2", tp.centerY + sin * tp.radiusLines);
	trigoPoint.setAttribute("cx", tp.centerX + cos * tp.radiusLines);
	trigoPoint.setAttribute("cy", tp.centerY + sin * tp.radiusLines);
	cosPoint.setAttribute("cx", tp.centerX + cos * tp.radiusLines);
	sinPoint.setAttribute("cy", tp.centerY + sin * tp.radiusLines);
	cosLine.setAttribute("x2", tp.centerX + cos * tp.radiusLines);
	sinLine.setAttribute("y2", tp.centerY + sin * tp.radiusLines);
	projSinLine.setAttribute("x2", tp.centerX + cos * tp.radiusLines);
	projSinLine.setAttribute("y2", tp.centerY + sin * tp.radiusLines);
	projSinLine.setAttribute("y1", tp.centerY + sin * tp.radiusLines);
	projCosLine.setAttribute("x2", tp.centerX + cos * tp.radiusLines);
	projCosLine.setAttribute("x1", tp.centerX + cos * tp.radiusLines);
	projCosLine.setAttribute("y2", tp.centerY + sin * tp.radiusLines);
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console