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.
body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display: block;
}
#info a {
color: #046;
font-weight: bold;
}
var container, stats, controls;
var camera, scene, renderer, light;
var draggableObjects = [];
var IKJoints = [];
var endEffector = null;
var boxGeometry = new THREE.BoxBufferGeometry(100, 100, 100);
var white = new THREE.MeshLambertMaterial({ color: 0x888888 });
init();
animate();
function init() {
container = document.createElement('div');
document.body.appendChild(container);
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000);
camera.position.set(50, 100, 150);
controls = new THREE.OrbitControls(camera);
controls.target.set(0, 45, 0);
controls.update();
scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);//0xa0a0a0
scene.fog = new THREE.Fog(0xffffff, 200, 400);//0xa0a0a0
light = new THREE.HemisphereLight(0xffffff, 0x444444);
light.position.set(0, 200, 0);
scene.add(light);
light = new THREE.DirectionalLight(0xbbbbbb);
light.position.set(0, 200, 100);
light.castShadow = true;
light.shadow.camera.top = 180;
light.shadow.camera.bottom = - 100;
light.shadow.camera.left = - 120;
light.shadow.camera.right = 120;
scene.add(light);
//scene.add(new THREE.CameraHelper(light.shadow.camera));
// ground
var mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(2000, 2000), new THREE.MeshPhongMaterial({ color: 0x999999, depthWrite: false }));
mesh.rotation.x = - Math.PI / 2;
mesh.receiveShadow = true;
scene.add(mesh);
var grid = new THREE.GridHelper(2000, 20, 0x000000, 0x000000);
grid.material.opacity = 0.2;
grid.material.transparent = true;
scene.add(grid);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.style.margin = 0;
document.body.style.padding = 0;
document.body.style.overflow = 'hidden';
document.body.style.position = 'fixed';
container.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
//Assemble the Robot Arm
//var base = new THREE.Group();
//scene.add(base);
var base = addJoint(scene, [0, 0, 0], [0, 1, 0], [0, 0], [0.05, 0.1, 0.05], [0, 5, 0]);
var firstJoint = addJoint(base, [0, 11.52001, 0], [0, 1, 0], [-180, 180], [0.1, 0.1, 0.1], [0, 2.5, 0]);
var secondJoint = addJoint(firstJoint, [-6.55, 4.6, 0.0], [1, 0, 0], [-90, 90], [0.1, 0.45, 0.1], [-3.450041, 14.7, 0]);
var thirdJoint = addJoint(secondJoint, [1.247041, 32.02634, -0.0739485], [1, 0, 0], [-150, 150], [0.05, 0.35, 0.05], [2.8, 15.14, 0]);
var fourthJoint = addJoint(thirdJoint, [2.984276, 30.01859, 0.0], [1, 0, 0], [-90, 90], [0.05, 0.05, 0.05], [4.8, 0.17, 0]);
var fifthJoint = addJoint(fourthJoint, [4.333822, 4.200262, 0.0], [0, 1, 0], [-180, 180], [0.1, 0.035, 0.035], [3.156178, 0.3, 0]);
endEffector = new THREE.Group();
fifthJoint.add(endEffector);
endEffector.position.set(8.3, 1.0, 0.0);
//Math.random() *
var target = new THREE.Mesh(boxGeometry, new THREE.MeshLambertMaterial({ color: 0x3399dd }));
target.position.set(0, 100, 0);
target.scale.set(0.075, 0.075, 0.075);
target.transparent = true;
target.opacity = 0.5;
target.castShadow = true;
target.receiveShadow = true;
scene.add(target);
draggableObjects.push(target);
var dragControls = new THREE.DragControls(draggableObjects, camera, renderer.domElement);
dragControls.addEventListener('dragstart', function () {
controls.enabled = false;
});
dragControls.addEventListener('dragend', function () {
controls.enabled = true;
});
}
function addJoint(base, position, axis, limits, size, graphicsOffset) {
var joint = new THREE.Group();
base.add(joint);
joint.position.set(position[0], position[1], position[2]);
joint.axis = new THREE.Vector3(axis[0], axis[1], axis[2]);
joint.minLimit = limits[0] * 0.0174533;
joint.maxLimit = limits[1] * 0.0174533;
IKJoints.push(joint);
var box = new THREE.Mesh(boxGeometry, white);
joint.add(box);
box.scale.set(size[0], size[1], size[2]);
box.position.set(graphicsOffset[0], graphicsOffset[1], graphicsOffset[2]);
box.castShadow = true;
//box.receiveShadow = true;
return joint;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
//Beautiful CCDIK
function solveIK(targetPosition) {
var tooltipPosition = new THREE.Vector3();
for (var i = IKJoints.length - 1; i >= 0; i--) {
IKJoints[i].updateMatrixWorld();
endEffector.getWorldPosition(tooltipPosition);
//Rotate towards the Target
//(Ideally this could be done entirely in worldspace (instead of local space))
var toolDirection = IKJoints[i].worldToLocal(tooltipPosition.clone()).normalize();
var targetDirection = IKJoints[i].worldToLocal(targetPosition.clone()).normalize();
var fromToQuat = new THREE.Quaternion(0, 0, 0, 1).setFromUnitVectors(toolDirection, targetDirection);
IKJoints[i].quaternion.multiply(fromToQuat);
//Find the rotation from here to the parent, and rotate the axis by it...
//This ensures that you're always rotating with the hinge
var invRot = IKJoints[i].quaternion.clone().inverse();
var parentAxis = IKJoints[i].axis.clone().applyQuaternion(invRot);
fromToQuat.setFromUnitVectors(IKJoints[i].axis, parentAxis);
IKJoints[i].quaternion.multiply(fromToQuat);
//Clamp to Joint Limits - Devious and relies on sensical computation of these values...
//Seems like rotations range from -pi, pi... not the worst... but bad for clamps through there
var clampedRot = IKJoints[i].rotation.toVector3().clampScalar(IKJoints[i].minLimit, IKJoints[i].maxLimit);
IKJoints[i].rotation.setFromVector3(clampedRot);
IKJoints[i].updateMatrixWorld();
}
}
function animate() {
solveIK(draggableObjects[0].position);
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
Also see: Tab Triggers