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.
<canvas></canvas>
<button class="about">About</button>
<div class="home hidden">
<div>
<p>
Welcome to an experimental world.
</p>
<p>
Lose yourself by clicking on the coloured spheres to get a new point of view.
<br> Return to your initial position by clicking anywhere in the darkness.
</p>
<p>
This experiment can be seen on both mobile and desktop. You can control the camera with your phone or tablet, I suggest you to try it for fun.
</p>
<p>
Thanks to <a target="_blank" href="https://twitter.com/Alex_Tournay">Alexandre Tournay</a> for his precious advice.
</p>
<p>
The main technology for this is <a target="_blank" href="https://threejs.org/">ThreeJs</a> for the WebGL and <a target="_blank" href="https://greensock.com/">GSAP</a> for all the transitions. Miscellaneous plugins for shaders and specific features can be found in the settings of this pen.
</p>
<p>
<button class="start">Start your journey</button>
</p>
<a target="_blank" href="https://twitter.com/Mamboleoo">Mamboleoo</a>
</div>
</div>
<script type="x-shader/x-vertex" id="wrapVertexShader">
attribute float size; attribute float opacity; varying float vOpacity; void main() { vOpacity = opacity; vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); gl_PointSize = size; gl_Position = projectionMatrix * mvPosition; }
</script>
<script type="x-shader/x-fragment" id="wrapFragmentShader">
varying float vOpacity; uniform sampler2D texture; void main(){ vec4 color = vec4( 1.0,1.0,1.0, vOpacity) * texture2D( texture, gl_PointCoord ); gl_FragColor = color; }
</script>
body{
overflow: hidden;
margin: 0;
background:black url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/127738/nucleus_thumbnail.jpg) center / cover;
}
*{box-sizing:border-box;}
canvas{
transition: filter 0.4s ease-out;
&.blurry{
pointer-events:none;
filter: blur(4px);
}
}
.home{
position: fixed;
top: 50%;
left: calc(50% - 300px);
transform: translateY(-50%);
width:600px;
border:1px solid #ccc;
background: rgba(0,0,0,0.9);
color: white;
display: flex;
justify-content: center;
align-items: center;
padding:20px;
font-family: 'Quicksand', sans-serif;
font-weight: 100;
font-size: 15px;
line-height: 1.4;
transition: 0.4s ease-out;
}
a{
color: white;
border-bottom: 1px solid white;
text-decoration: none;
display: inline-block;
margin-top: 10px;
}
.start{
border:1px solid white;
background:none;
color: white;
padding:10px 15px;
font-family: 'Quicksand', sans-serif;
margin:10px 0;
font-size: 18px;
cursor: pointer;
position: relative;
transition:0.3s ease-out;
overflow: hidden;
&:before{
content:"";
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height:100%;
background:white;
transform: rotate(90deg);
transform-origin: 100% 100%;
transition:transform 0.3s ease-in;
z-index:-1;
}
&:hover{
color:black;
&:before{
transform: rotate(0);
transition:transform 0.3s ease-out;
transform-origin: 0% 0%;
}
}
}
p{
margin: 0 0 10px 0;
}
.home.hidden{
opacity:0;
visibility:hidden;
}
@media (max-width: 650px){
.home{
width: calc(100% - 24px);
left:12px;
top: 12px;
transform:none;
position: absolute;
max-height: calc(100% - 24px);
overflow:auto;
display: block;
}
}
.about{
position: absolute;
bottom: 10px;
right: 10px;
color:white;
border:none;
background:none;
font-size: 24px;
font-family: 'Quicksand', sans-serif;
cursor: pointer;
padding: 0px 5px;
border-bottom: 1px solid white;
}
var opts = {
noiseForce: 0.6,
curvesLength: 10,
radius: 350,
cubesQuantity: 45,
pathOpacity: 0.2,
speedAverage: 150000, //Ms
zCamera: 900
};
var ww = window.innerWidth,
wh = window.innerHeight;
var renderer = new THREE.WebGLRenderer({
canvas: document.querySelector("canvas")
});
renderer.setClearColor(0x000000);
renderer.setSize(ww, wh);
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(50, ww / wh, 1, 2000);
camera.position.set(0, 0, opts.zCamera);
camera.destinationLook = new THREE.Vector3();
function resetCamera() {
TweenMax.to(camera.position, 2, {
x: 0,
y: 0,
z: opts.zCamera,
ease: Power1.easeInOut
});
}
resetCamera();
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2(0.3, 0.3);
mouse.activated = false;
document.querySelector("canvas").addEventListener("mouseup", onMouseUp);
document.querySelector("canvas").addEventListener("touchstart", onMouseUp);
window.addEventListener("mousemove", detectSphereMouse);
function onMouseUp(e) {
if (e.type === "touchstart") {
mouse.x = (e.touches[0].clientX / ww) * 2 - 1;
mouse.y = -(e.touches[0].clientY / wh) * 2 + 1;
} else {
mouse.x = (e.clientX / ww) * 2 - 1;
mouse.y = -(e.clientY / wh) * 2 + 1;
}
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(cubesObject.children, true);
if (intersects.length > 0) {
//Check if clicked item is instance of a Mesh
if (intersects[0].object instanceof THREE.Mesh) {
if (clickedCube) {
clickedCube.cameraMustFollow = false;
clickedCube.visible = true;
TweenMax.to(clickedCube.material, 0.6, {
opacity: 1,
ease: Power1.easeIn
});
}
clickedCube = intersects[0].object;
TweenMax.to(clickedCube.material, 0.6, {
opacity: 0,
ease: Power1.easeIn,
onComplete: function() {
clickedCube.visible = false;
}
});
clickedCube.cameraMustFollow = true;
}
} else {
if (clickedCube) {
clickedCube.cameraMustFollow = false;
clickedCube.visible = true;
TweenMax.to(clickedCube.material, 0.6, {
opacity: 1,
ease: Power1.easeIn
});
}
clickedCube = null;
resetCamera();
}
}
function detectSphereMouse(e){
if (!mouse.activated) {
mouse.activated = true;
}
mouse.x = (e.clientX / ww) * 2 - 1;
mouse.y = -(e.clientY / wh) * 2 + 1;
var ratio = 0.2;
if (clickedCube) {
ratio = Math.PI * 0.6;
}
TweenMax.to(camera.rotation, 1, {
ease: Back.easeOut,
y: -mouse.x * ratio,
x: mouse.y * ratio
});
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(cubesObject.children, true);
if (hoverCube) {
hoverCube.material.emissive = new THREE.Color(0x000000);
TweenMax.to(hoverCube.scale, 1.5, {
ease: Elastic.easeOut.config(1.55, 0.2),
x: hoverCube.size,
y: hoverCube.size,
z: hoverCube.size
});
}
if (intersects.length > 0) {
document.body.style.cursor = "pointer";
hoverCube = intersects[0].object;
hoverCube.material.emissive = new THREE.Color(0x313131);
TweenMax.to(hoverCube.scale, 1.6, {
ease: Back.easeOut,
x: hoverCube.size * 1.5,
y: hoverCube.size * 1.5,
z: hoverCube.size * 1.5
});
} else {
document.body.style.cursor = "default";
hoverCube = null;
}
};
window.addEventListener("resize", function() {
ww = window.innerWidth;
wh = window.innerHeight;
camera.aspect = ww / wh;
camera.updateProjectionMatrix();
renderer.setSize(ww, wh);
fxaa.uniforms.resolution.value.set(1 / ww, 1 / wh);
});
//ON DEVICE MOVE
function orientationEvent(e) {
mouse.activated = false;
var ratio = Math.PI * 2;
var alpha = Math.round(e.alpha / 360 * ratio * 1000) / 1000;
var beta = Math.round(e.beta / 360 * ratio * 1000) / 1000;
var gamma = Math.round(e.gamma / 360 * ratio * 1000) / 1000;
camera.rotation.x = -gamma;
camera.rotation.y = beta;
camera.rotation.z = alpha;
}
window.addEventListener("deviceorientation", orientationEvent, true);
//LIGHTS
var pointLight = new THREE.PointLight(0xffffff, 0.5);
scene.add(pointLight);
var light = new THREE.HemisphereLight(0xffffff, 0xcccccc, 0.5);
scene.add(light);
var basicCube = new THREE.SphereBufferGeometry(1, 8, 8);
var clickedCube = null;
var hoverCube = null;
//Create a path filled with cubes
function Necklace() {
var points = [];
//Set the amount of points to create the paths
var lengthPathPoints = 40;
noise.seed(Math.random());
var rotation = Math.random() * Math.PI;
//Generate points to create random curves
for (var i = 0; i < lengthPathPoints; i++) {
var x = Math.cos(i / lengthPathPoints * Math.PI * 2) * opts.radius;
var y = Math.sin(i / lengthPathPoints * Math.PI * 2) * opts.radius;
var random = noise.simplex2(x * 0.002, y * 0.002) * opts.noiseForce;
x *= (random - 1);
y *= (random - 1);
var z = 0;
var vertex = new THREE.Vector3(x, y, z);
vertex.applyAxisAngle(new THREE.Vector3(0, 0.1, 0).normalize(), rotation);
points.push(vertex);
}
this.curve = new THREE.CatmullRomCurve3(points);
this.curve.closed = true;
var geometry = new THREE.Geometry();
geometry.vertices = this.curve.getPoints(100);
var material = new THREE.LineBasicMaterial({
color: 0xdddddd,
transparent: true,
opacity: opts.pathOpacity
});
this.lace = new THREE.Line(geometry, material);
this.items = new THREE.Object3D();
this.speed = (Math.random() + 0.5) * opts.speedAverage;
var angle = Math.random() * 60 + 150;
for (i = 0; i < opts.cubesQuantity; i++) {
var color = new THREE.Color("hsl(" + (Math.random() * 20 + angle) + ", 100%, 50%)");
var sphereMaterial = new THREE.MeshPhongMaterial({
color: color,
transparent: true,
shading: THREE.FlatShading
});
var size = (Math.random() * 0.8 + 0.4) * 12;
var mesh = new THREE.Mesh(basicCube, sphereMaterial);
mesh.ratio = i / opts.cubesQuantity;
mesh.size = size;
mesh.scale.set(size, size, size);
mesh.cameraMustFollow = false;
this.items.add(mesh);
}
this.object = new THREE.Object3D();
this.object.add(this.items);
}
var interval = 0.001;
Necklace.prototype.update = function(a) {
//Loop through all cubes in one path
var cube, tempA, percent, p1, p2;
for (var i = 0; i < this.items.children.length; i++) {
cube = this.items.children[i];
tempA = a + cube.ratio * this.speed;
percent = ((tempA % this.speed) / this.speed) % 1;
//Calculate the next point position
p1 = this.curve.getPointAt(percent);
//Calculate the point where the cube must look At
p2 = this.curve.getPointAt((percent + interval) % 1);
//Update the position & rotation of the cube
cube.position.set(p1.x, p1.y, p1.z);
cube.lookAt(p2);
if (cube.cameraMustFollow) {
TweenMax.to(camera.position, 2, {
x: p1.x,
y: p1.y,
z: p1.z,
ease: Power1.easeOut
});
}
}
};
// ===============
//WRAP WITH PARTICLES
// ===============
var totalParticlesWrap = 3000;
var wrapGeom = new THREE.Geometry();
var positions = new Float32Array(totalParticlesWrap * 3);
var sizes = new Float32Array(totalParticlesWrap);
var opacities = new Float32Array(totalParticlesWrap);
var texture = new THREE.TextureLoader().load("https://s3-us-west-2.amazonaws.com/s.cdpn.io/127738/dotTexture.png");
for (var i = 0; i < totalParticlesWrap; i++) {
var vector = new THREE.Vector3(0, 0, 0);
vector.speedX = (Math.random() - 0.5) * 0.0004;
vector.speedY = (Math.random() - 0.5) * 0.0004;
vector.speedZ = (Math.random() - 0.5) * 0.0004;
vector.applyMatrix4(new THREE.Matrix4().makeTranslation(0, Math.random() * 2000 + 300, 0));
vector.applyMatrix4(new THREE.Matrix4().makeRotationX(Math.random() * (Math.PI * 2)));
vector.applyMatrix4(new THREE.Matrix4().makeRotationY(Math.random() * (Math.PI * 2)));
vector.applyMatrix4(new THREE.Matrix4().makeRotationZ(Math.random() * (Math.PI * 2)));
wrapGeom.vertices.push(vector);
//Add to attributes
vector.toArray(positions, i * 3);
sizes[i] = Math.random() * 1 + 1;
opacities[i] = Math.random() * 0.7 + 0.1;
}
var bufferWrapGeom = new THREE.BufferGeometry();
bufferWrapGeom.addAttribute('position', new THREE.BufferAttribute(positions, 3));
bufferWrapGeom.addAttribute('size', new THREE.BufferAttribute(sizes, 1));
bufferWrapGeom.addAttribute('opacity', new THREE.BufferAttribute(opacities, 1));
var wrapMatShader = new THREE.ShaderMaterial({
uniforms: {
texture: {
value: texture
}
},
vertexShader: document.getElementById("wrapVertexShader").textContent,
fragmentShader: document.getElementById("wrapFragmentShader").textContent,
transparent: true
});
var wrap = new THREE.Points(bufferWrapGeom, wrapMatShader);
scene.add(wrap);
function updateWrap(a) {
for (var i = 0; i < totalParticlesWrap; i++) {
var vector = wrapGeom.vertices[i];
vector.applyMatrix4(new THREE.Matrix4().makeRotationX(vector.speedX));
vector.applyMatrix4(new THREE.Matrix4().makeRotationY(vector.speedY));
vector.applyMatrix4(new THREE.Matrix4().makeRotationZ(vector.speedZ));
vector.toArray(positions, i * 3);
}
bufferWrapGeom.addAttribute('position', new THREE.BufferAttribute(positions, 3));
}
//Black center
var geom = new THREE.SphereGeometry(100, 32, 32);
var mat = new THREE.MeshPhongMaterial({
color: 0x000000
});
var core = new THREE.Mesh(geom, mat);
scene.add(core);
var geom = new THREE.SphereBufferGeometry(1, 15, 15);
var mat = new THREE.MeshBasicMaterial({
color: 0xffffff
});
var atoms = new THREE.Object3D();
scene.add(atoms);
for (var i = 0; i < 150; i++) {
var nucleus = new THREE.Mesh(geom, mat);
var size = Math.random() * 6 + 1.5;
nucleus.speedX = (Math.random() - 0.5) * 0.08;
nucleus.speedY = (Math.random() - 0.5) * 0.08;
nucleus.speedZ = (Math.random() - 0.5) * 0.08;
nucleus.applyMatrix(new THREE.Matrix4().makeScale(size, size, size));
nucleus.applyMatrix(new THREE.Matrix4().makeTranslation(0, 100 + Math.random() * 10, 0));
nucleus.applyMatrix(new THREE.Matrix4().makeRotationX(Math.random() * (Math.PI * 2)));
nucleus.applyMatrix(new THREE.Matrix4().makeRotationY(Math.random() * (Math.PI * 2)));
nucleus.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.random() * (Math.PI * 2)));
atoms.add(nucleus);
}
function updateNucleus(a) {
for (var i = 0; i < atoms.children.length; i++) {
var part = atoms.children[i];
part.applyMatrix(new THREE.Matrix4().makeRotationX(part.speedX));
part.applyMatrix(new THREE.Matrix4().makeRotationY(part.speedY));
part.applyMatrix(new THREE.Matrix4().makeRotationZ(part.speedZ));
}
}
//Create scene
var necks = [];
var cubesObject = new THREE.Object3D();
scene.add(cubesObject);
//Generate n path with cubes
for (var i = 0; i < opts.curvesLength; i++) {
var neck = new Necklace();
cubesObject.add(neck.object);
necks.push(neck);
}
var renderPass = new THREE.RenderPass(scene, camera);
var shift = new THREE.ShaderPass(THREE.RGBShiftShader);
shift.uniforms.amount.value = -0.0015;
shift.uniforms.angle.value = 180;
var fxaa = new THREE.ShaderPass(THREE.FXAAShader);
fxaa.uniforms.resolution.value.set(1 / ww, 1 / wh);
composer = new THREE.EffectComposer(renderer);
composer.addPass(renderPass);
composer.addPass(shift);
composer.addPass(fxaa);
fxaa.renderToScreen = true;
// ========
//RENDER
// ========
function render(a) {
requestAnimationFrame(render);
for (var i = 0; i < necks.length; i++) {
necks[i].update(a);
}
updateNucleus(a);
updateWrap(a);
renderer.render(scene, camera);
composer.render(0.05);
}
requestAnimationFrame(render);
var startButton = document.querySelector(".start");
startButton.addEventListener("click", windowDown);
startButton.addEventListener("touchend", windowDown);
document.querySelector(".about").addEventListener("click", windowDown);
function windowDown(e) {
document.querySelector("canvas").classList.toggle("blurry");
document.querySelector(".home").classList.toggle("hidden");
}
Also see: Tab Triggers