css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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");
}
            
          
!
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