HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!doctype html>
<html>
<head>
<title>Camera helper demo</title>
<style>
body { margin: 10px; min-width: 820px; }
div.canvas { display: inline-block; display: inline-block; margin: 0px 10px; }
canvas { width: 400px; height: 300px; }
</style>
</head>
<body>
<h1>Camera helper demo</h1>
<div id="rendered" class="canvas">
<h2>Rendered Scene</h2>
</div>
<div id="really" class="canvas">
<h2>What's Really Happening</h2>
</div>
<p>
See the <a href="https://threejs.org/examples/#webgl_camera">Three.js camera helper official demo</a>, <a href="https://threejs.org/examples/#webgl_geometry_extrude_splines"> spline extrusion example</a>, and <a href="https://threejs.org/docs/#api/en/helpers/CameraHelper"> documentation</a>.
</p>
</body>
</html>
function createNose(params) {
var sd = params.sphereDetail || 10;
var radius = params.noseRadius || 0.6;
var noseGeometry = new THREE.SphereGeometry(radius,sd,sd);
var color = params.noseColor || TW.BLACK;
var mat = new THREE.MeshBasicMaterial({'color':color});
var noseMesh = new THREE.Mesh(noseGeometry, mat);
return noseMesh;
}
function addNose(head,params) {
/* adds a nose to the head. It's placed by creating a composite object
* centered in the middle of the head, and positioning the nose at the
* head radius on +Z, then rotating around X by a little. */
var noseframe = new THREE.Object3D();
var nose = createNose(params);
var radius = params.headRadius || 2;
nose.position.z = radius; // within the noseframe
noseframe.add(nose);
var angle = params.noseRotation || TW.degrees2radians(10);
noseframe.rotation.x = angle;
head.add(noseframe);
return head;
}
function createEar(params) {
// side is 1 (right) or -1 (left)
var sd = params.sphereDetail || 10;
var radius = params.earRadius || 0.6;
var earGeometry = new THREE.SphereGeometry(radius,sd,sd);
var color = params.earColor || TW.BLACK;
var mat = new THREE.MeshBasicMaterial({color:color});
var ear = new THREE.Mesh(earGeometry, mat);
//Flattens the sphere to make it look more like a flat disk
ear.scale.z = params.earScale || 0.5;
return ear;
}
function addEar(head,params,side) {
/* adds an ear to the head on the right (side=1) or left
* (side=-1). The center of the ear is flush with the surface of the
* head by moving it out by the radius, and rotating it around the z
* axis to get it to the desired height. */
var earframe = new THREE.Object3D();
var ear = createEar(params);
var radius = params.headRadius || 2;
var angle = params.earAngle || Math.PI/4;
ear.position.x = side * radius; // within the earframe
earframe.rotation.z = side * angle;
earframe.add(ear);
head.add(earframe);
return head;
}
function createEye(params) {
var sd = params.sphereDetail || 10;
var radius = params.eyeRadius || 0.3;
var eyeGeometry = new THREE.SphereGeometry(radius,sd,sd);
var color = params.eyeColor || TW.BLACK;
var mat = new THREE.MeshBasicMaterial({color:color});
var eyeMesh = new THREE.Mesh(eyeGeometry, mat);
return eyeMesh;
}
function addEye(head,params,side) {
/* adds an eye to the head on the right (side=1) or left
* (side=-1). The center of the eye is flush with the surface of the
* head by moving it out along the z axis by the radius, and rotating
* it around the x and then y axes to get it to the desired height. */
var eyeframe = new THREE.Object3D();
var eye = createEye(params);
var radius = params.headRadius || 2;
eye.position.z = radius; // within the eyeframe
var angleX = params.eyeAngleX || -Math.PI/6;
var angleY = params.eyeAngleY || Math.PI/6;
eyeframe.rotation.x = angleX;
eyeframe.rotation.y = side * angleY;
eyeframe.add(eye);
head.add(eyeframe);
return head;
}
function createHead(params) {
/* Returns a teddy bear head object, with origin in the center, and
* eyes on the +Z side of the head, and ears on the left (-X) and
* right (+X) sides. */
var head = new THREE.Object3D();
var sd = params.sphereDetail || 10;
var radius = params.headRadius || 2;
var headGeometry = new THREE.SphereGeometry(radius, sd, sd);
var color = params.headColor || 0xB07040; // like body but slightly darker
var mat = new THREE.MeshBasicMaterial({color: color});
mat.wireframe = params.wireframe || false;
var headMesh = new THREE.Mesh(headGeometry, mat);
head.add(headMesh);
if(params.nose) {
addNose(head,params);
}
if(params.ears) {
addEar(head,params,1);
addEar(head,params,-1);
}
if(params.eyes) {
addEye(head,params,1);
addEye(head,params,-1);
}
return head;
}
function createLimb(radiusTop, radiusBottom, length, params) {
/* returns an Object with the center at the top and the negative Y
* axis running down the center. */
var limb = new THREE.Object3D();
var cd = params.cylinderDetail || 10;
// Turns out there's an error in Three.js if cd is a non-integer
var limbGeom = new THREE.CylinderGeometry(radiusTop,radiusBottom,length,cd);
var color = params.limbColor || 0xB07040; // same as head, like body but slightly darker
var mat = new THREE.MeshBasicMaterial({color:color});
var limbMesh = new THREE.Mesh( limbGeom, mat );
limbMesh.position.y = -length/2;
limb.add(limbMesh);
return limb;
}
function addArm(bear,params,side) {
/* adds an arm to the bear on the right (side=1) or left (side=-1). */
var top = params.armRadiusTop || 0.7;
var bot = params.armRadiusBottom || 0.6;
var len = params.armLength || 5;
var arm = createLimb(top,bot,len,params);
var radius = params.bodyRadius || 3;
var scale = params.bodyScaleY || 2;
var sx = params.shoulderWidth || radius * 0.5;
var sy = params.shoulderHeight || scale * radius * 0.7;
arm.position.set( side * sx, sy, 0 );
arm.rotation.z = side * Math.PI/2;
bear.add(arm);
}
function addLeg(bear,params,side) {
/* adds a leg to the bear on the right (side=1) or left (side=-1). */
var top = params.legRadiusTop || 0.7;
var bot = params.legRadiusBottom || 0.6;
var len = params.legLength || 5;
var leg = createLimb(top,bot,len,params);
leg.name = (side == 1 ? "right leg" : "left leg");
var radius = params.bodyRadius || 3;
var scale = params.bodyScaleY || 2;
var hx = side * params.hipWidth || side * radius * 0.5;
var hy = params.hipHeight || scale * radius * -0.7;
leg.position.set( hx, hy, 0 );
leg.rotation.x = params.legRotationX;
leg.rotation.z = side * params.legRotationZ;
bear.add(leg);
}
function createBody(params) {
if( !params ) params = {};
var body = new THREE.Object3D();
var radius = params.bodyRadius || 3;
var sd = params.sphereDetail || 20;
var bodyGeom = new THREE.SphereGeometry(radius,sd,sd);
var bodyColor = params.bodyColor || 0xD08050;
var mat = new THREE.MeshBasicMaterial({color: bodyColor});
mat.wireframe = params.wireframe || false;
var bodyMesh = new THREE.Mesh(bodyGeom, mat);
var scale = params.bodyScaleY || 2;
bodyMesh.scale.y = scale;
body.add(bodyMesh);
if(params.arms) {
addArm(body,params,1);
addArm(body,params,-1);
}
if(params.legs) {
addLeg(body,params,1);
addLeg(body,params,-1);
}
return body;
}
function createTeddyBear(params) {
var bear = new THREE.Object3D();
var body = createBody(params);
bear.add(body);
if(params.head) {
var head = createHead(params);
var bs = params.bodyScaleY || 2;
var br = params.bodyRadius || 3;
var hr = params.headRadius || 1;
// calculate position for the center of the head
head.position.y = bs*br+hr;
bear.add(head);
}
return bear;
}
var params = {
wireframe: false,
sphereDetail: 10,
cylinderDetail: 10,
nose: true,
noseRadius: 0.5,
noseRotation: TW.degrees2radians(10),
ears: true,
earRadius: 0.6,
earScale: 0.5,
earAngle: Math.PI/4,
eyes: true,
eyeRadius: 0.3,
eyeAngleX: -Math.PI/6,
eyeAngleY: +Math.PI/6,
arms: true,
armLength: 7,
armRadiusTop: 1.5,
armRadiusBottom: 1.2,
legs: true,
legRadiusTop: 1.8,
legRadiusBottom: 1.4,
legLength: 9,
legRotationX: -TW.degrees2radians(60),
legRotationZ: TW.degrees2radians(20),
hipWidth: 2.5,
hipHeight: -7,
head: true,
headRadius: 2,
bodyRadius: 5,
bodyScaleY: 2,
noop: "last param"
};
var renderer = new THREE.WebGLRenderer();
var scene = new THREE.Scene();
TW.mainInit(renderer,scene,{parent: document.getElementById('rendered')} );
var cameraParams = {
near: 5,
far: 30,
fov: 75, // degrees?!
aspectRatio: 400/300, // from the dimensions of the canvas. see CSS
atX: 0,
atY: 0,
atZ: 0,
eyeX: 0,
eyeY: 0,
eyeZ: 25,
upX: 0,
upY: 1,
upZ: 0
};
var bear = createTeddyBear(params);
scene.add(bear);
// global, so we can modify it from the GUI
var camera = new THREE.PerspectiveCamera();
// globals, modified from the above
var at = new THREE.Vector3();
var eye = new THREE.Vector3();
var up = new THREE.Vector3();
function setCameraView() {
at.set( cameraParams.atX, cameraParams.atY, cameraParams.atZ );
eye.set( cameraParams.eyeX, cameraParams.eyeY, cameraParams.eyeZ );
up.set( cameraParams.upX, cameraParams.upY, cameraParams.upZ );
}
setCameraView();
function setupCamera(scene) {
// camera shape
var fov = cameraParams.fov || 75; // in degrees
var aspect = cameraParams.aspectRatio || 400/300; // canvas width/height
var near = cameraParams.near || 5; // measured from eye
var far = cameraParams.far || 30; // measured from eye
camera = new THREE.PerspectiveCamera(fov,aspect,near,far);
// camera location
setCameraView();
camera.position.copy(eye);
// Cameras inherit an "up" vector from Object3D.
camera.up.copy(up);
camera.lookAt(at);
return camera;
}
function adjustCamera() {
// the following are for the camera shape
camera.fov = cameraParams.fov;
camera.aspect = cameraParams.aspectRatio;
camera.near = cameraParams.near;
camera.far = cameraParams.far;
// to account for the settings above
camera.updateProjectionMatrix();
// camera location
setCameraView();
camera.position.copy(eye);
// Cameras inherit an "up" vector from Object3D.
camera.up.copy(up);
camera.lookAt(at);
}
adjustCamera();
var leftrender = function () { renderer.render(scene,camera); }
TW.lastClickTarget.TW_state.render = leftrender;
TW.toggleAxes("show");
// Second Canvas, showing both first scene and frustum
var renderer2 = new THREE.WebGLRenderer();
var scene2 = new THREE.Scene();
TW.mainInit(renderer2,scene2,{parent: document.getElementById('really')} );
bearRight = createTeddyBear(params);
scene2.add(bearRight);
var cameraHelper = new THREE.CameraHelper(camera);
scene2.add(cameraHelper);
var upHelper = null;
var up;
var eye;
function addUpHelper() {
if(upHelper) {
scene2.remove(upHelper);
}
up = new THREE.Vector3( cameraParams.upX,
cameraParams.upY,
cameraParams.upZ );
eye = new THREE.Vector3( cameraParams.eyeX,
cameraParams.eyeY,
cameraParams.eyeZ );
up.normalize();
upHelper = new THREE.ArrowHelper( up, eye, 10, TW.LIME );
scene2.add(upHelper);
}
addUpHelper();
TW.cameraSetup(renderer2,scene2,
{minx: -15, maxx: 15,
miny: -15, maxy: 15,
minz: -15, maxz: 15});
TW.viewFromAboveFrontSide();
rightrender = TW.lastClickTarget.TW_state.render;
function redo() {
adjustCamera();
cameraHelper.update();
addUpHelper();
leftrender();
rightrender();
}
var gui = new dat.GUI();
gui.add(cameraParams,'fov',1,179).onChange(redo);
gui.add(cameraParams,'aspectRatio',0.1,10).onChange(redo);
gui.add(cameraParams,'near',1,50).onChange(redo);
gui.add(cameraParams,'far',1,50).onChange(redo);
gui.add(cameraParams,'atX',-10,10).onChange(redo);
gui.add(cameraParams,'atY',-10,10).onChange(redo);
gui.add(cameraParams,'atZ',-10,10).onChange(redo);
gui.add(cameraParams,'eyeX',-10,10).onChange(redo);
gui.add(cameraParams,'eyeY',-10,10).onChange(redo);
gui.add(cameraParams,'eyeZ',-30,30).onChange(redo);
gui.add(cameraParams,'upX',-10,10).onChange(redo);
gui.add(cameraParams,'upY',-10,10).onChange(redo);
gui.add(cameraParams,'upZ',-10,10).onChange(redo);
Also see: Tab Triggers