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.
<div class="stage" data-stage></div>
*, *:before, *:after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body,
.stage {
width: 100%;
height: 100%;
overflow: hidden;
}
body {
background-color: #f7d514;
}
const global = {
scene: null,
camera: null,
renderer: null,
world: null,
loadManager: null,
jsonLoader: null,
texLoader: null,
debugger: null,
debug: false,
};
const environment = {
tube: null,
}
const physix = {
body: null,
updates: []
}
const mesh = {
container: null,
jsonData: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/46992/plus-skin.json',
texSrc: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/46992/birch.jpg',
geometry: null,
plus: null,
texture: null,
};
const init = () => {
initScene();
initLight();
initWorld();
clonePlus();
cloneBasket();
initLoader();
loadModel();
loadTexture();
}
const initScene = () => {
global.scene = new THREE.Scene();
global.camera = new THREE.PerspectiveCamera(
10,
window.innerWidth / window.innerHeight,
0.0001,
10000
);
global.camera.position.set(60, 60, 50);
global.camera.lookAt(global.scene.position);
global.scene.add(global.camera);
global.renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
logarithmicDepthBuffer: true
});
global.renderer.setSize(window.innerWidth, window.innerHeight);
document
.querySelector("[data-stage]")
.appendChild(global.renderer.domElement);
};
const initLight = () => {
const lightTop = new THREE.PointLight( 0xffffff, 0.4, 100, 1);
lightTop.position.set(15, 40, -10);
global.scene.add(lightTop);
const lightBottom = new THREE.PointLight( 0xffffff, 0.4, 100, 1);
lightBottom.position.set(10, -30, 10);
global.scene.add(lightBottom);
const ambientLight = new THREE.AmbientLight( 0xffffff, 0.8 );
global.scene.add( ambientLight );
};
const initLoader = () => {
global.loadManager = new THREE.LoadingManager();
global.jsonLoader = new THREE.JSONLoader( global.loadManager );
global.texLoader = new THREE.TextureLoader( global.loadManager );
global.texLoader.setCrossOrigin('anonymous');
global.loadManager.onLoad = function() {
createTube();
createBasket();
createPlus(animate);
};
}
const loadModel = () => {
global.jsonLoader.load( mesh.jsonData, function (geometry) {
mesh.geometry = geometry;
});
}
const loadTexture = () => {
global.texLoader.load( mesh.texSrc, function (texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1, 1);
mesh.texture = texture;
});
}
const initWorld = () => {
global.world = new CANNON.World();
global.world.broadphase = new CANNON.NaiveBroadphase();
global.world.gravity.set( 0, -50, 0);
global.world.solver.iterations = 10;
global.debugger = new THREE.CannonDebugRenderer( global.scene, global.world );
}
const getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const createBall = () => {
let mass = 10.0;
let radius = 0.4;
let x = 0.1;
let y = 7.2;
let z = 0;
let shape = new CANNON.Sphere(radius * 1.1);
let body = new CANNON.Body({
mass: mass,
shape: shape,
linearDamping: 0.0,
angularDamping: 0.0,
});
body.position.set(x, y, z);
global.world.add(body);
const colors = [0x47debd, 0xffffff, 0x7821ec, 0x2e044e];
const color = colors[getRandomInt(0,3)];
let material = new THREE.MeshStandardMaterial({
emissive: 0x333333,
emissiveIntensity: 0.3,
color: color,
roughness: 0.9,
metalness: 0.1,
});
let ball = new THREE.Mesh(
new THREE.SphereGeometry(radius, 32, 32),
material
);
ball.position.set(x, y, z);
global.scene.add(ball);
let array = [body, ball];
physix.updates.push(array);
}
const updatePhysix = () => {
for(let i = 0; i < physix.updates.length; i++) {
physix.updates[i][1].position.copy( physix.updates[i][0].position );
physix.updates[i][1].quaternion.copy ( physix.updates[i][0].quaternion);
}
}
const clonePlus = () => {
const offset = 3;
const radius = 1.1;
physix.body = new CANNON.Body({
mass: 0,
linearDamping: 0.0,
angularDamping: 0.0,
});
let sphere = new CANNON.Sphere(radius);
const pos = [
[-offset, 0, 0],
[offset, 0, 0],
[0, -offset, 0],
[0, offset, 0],
[0, 0, -offset],
[0, 0, offset]
];
for(let i = 0; i < pos.length; i++) {
physix.body.addShape(sphere, new CANNON.Vec3(
pos[i][0], pos[i][1], pos[i][2]
));
}
const rot = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
];
for(let i = 0; i < rot.length; i++) {
let cylinder = new CANNON.Cylinder(radius, radius, radius*6, 16);
let q = new CANNON.Quaternion();
q.setFromAxisAngle(
new CANNON.Vec3(rot[i][0], rot[i][1], rot[i][2]),
Math.PI*0.5
);
cylinder.transformAllPoints(new CANNON.Vec3(),q);
physix.body.addShape(cylinder);
}
physix.body.position.set(0, 0, 0);
physix.body.aabbNeedsUpdate = true;
global.world.add( physix.body );
}
const createBasket = () => {
const radius = 4;
const thickness = 0.2;
const basket = new THREE.Object3D( 0, 0, 0 );
const material = new THREE.MeshStandardMaterial({
emissive: 0xf7d514,
emissiveIntensity: 0.2,
color: 0xf7d514,
bumpMap: mesh.texture,
bumpScale: 0.9,
roughness: 0.9,
metalness: 0.1,
side: THREE.DoubleSide,
});
for(let i = 0; i < 2; i++) {
let s = i * thickness;
const geometry = new THREE.CylinderGeometry( radius - s, radius - s, 3, 64, 1, true );
const cylinder = new THREE.Mesh( geometry, material );
basket.add( cylinder );
}
let ringGeometry = new THREE.RingGeometry( radius - thickness, radius, 64, 1 );
const ring = new THREE.Mesh( ringGeometry, material );
ring.position.set(0, 1.5, 0);
ring.rotation.set(Math.PI*0.5, 0, 0);
basket.add( ring );
let circleGeometry = new THREE.CircleGeometry( radius - thickness, 64 );
var circle = new THREE.Mesh( circleGeometry, material );
circle.position.set(0, 0, 0);
circle.rotation.set(Math.PI*0.5, 0, 0);//
basket.add( circle );
basket.position.set(10, -3, 0);
global.scene.add( basket );
}
const cloneBasket = () => {
const radius = 4;
const thickness = 0.1;
const offset = 10;
const units = 16;
const theta = ((Math.PI*2) / units);
const body = new CANNON.Body({ mass: 0 });
const cylinder = new CANNON.Cylinder(radius, radius, thickness, 16);
const q = new CANNON.Quaternion();
q.setFromAxisAngle( new CANNON.Vec3(1, 0, 0), Math.PI*0.5 );
cylinder.transformAllPoints(new CANNON.Vec3(), q);
body.addShape(cylinder);
body.position.set(offset, -3, 0);
global.world.add(body);
for(let i = 0; i < 16; i++) {
let angle = (theta * i);
let x = (radius * Math.cos(angle));
let z = (radius * Math.sin(angle));
let unit = new CANNON.Body({ mass: 0 });
let box = new CANNON.Box(new CANNON.Vec3(thickness, 3, theta*2));
unit.addShape(box);
unit.position.set(x + offset, -4.5, z);
unit.quaternion.setFromEuler(0, -theta*i, 0);
global.world.add(unit);
}
}
const createPlus = (callback) => {
mesh.container = new THREE.Group();
global.scene.add(mesh.container);
const material = new THREE.MeshStandardMaterial({
emissive: 0xf7d514,
emissiveIntensity: 0.2,
color: 0xf7d514,
bumpMap: mesh.texture,
bumpScale: 0.9,
roughness: 0.9,
metalness: 0.1,
});
mesh.plus = new THREE.Mesh(mesh.geometry, material);
mesh.container.add(mesh.plus);
const array = [mesh.plus, physix.body];
physix.updates.push(array);
callback();
}
const createTube = () => {
const geometry = new THREE.CylinderGeometry( 0.6, 0.6, 20, 32 );
const material = new THREE.MeshStandardMaterial({
emissive: 0xf7d514,
emissiveIntensity: 0.2,
color: 0xf7d514,
bumpMap: mesh.texture,
bumpScale: 0.9,
roughness: 0.9,
metalness: 0.1,
});
environment.tube = new THREE.Mesh( geometry, material );
environment.tube.position.set(0.1, 18, 0)
global.scene.add( environment.tube );
}
const animate = () => {
const time = 0.8;
const timeStep = 1.0 / 100.0;
let rotate = { x: 0 };
const tl = new TimelineMax({
repeat: -1,
onUpdate: () => {
global.world.step(timeStep);
render();
}
});
for(let i = 1; i < 5; i++) {
tl.to(environment.tube.position, time*0.5, {
y: 17,
ease: Power4.easeIn,
onComplete: () => {
createBall();
}
})
.to(environment.tube.position, time*0.5, {
y: 18,
ease: Power4.easeOut
})
.to(mesh.plus.rotation, time, {
z: -Math.PI * i * 0.5,
ease: Power4.easeIn,
})
}
};
window.addEventListener("resize", resizeHandler);
const render = () => {
updatePhysix();
if(global.debug) global.debugger.update();
global.renderer.render(global.scene, global.camera);
};
function resizeHandler() {
global.renderer.setSize(window.innerWidth, window.innerHeight);
global.camera.aspect = window.innerWidth / window.innerHeight;
global.camera.updateProjectionMatrix();
}
init();
Also see: Tab Triggers