Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                html, body {
  margin: 0;
  width: 100%;
  height: 100%
}

canvas {
  position: fixed;
  width: 100%;
  height: 100%
}

              
            
!

JS

              
                let scene, camera, cameraCtrl, renderer;
let width, height, cx, cy;
let rndColor = chroma.random();

let conf = {
  nbObjects: 260,
  mainRadius: 20,
  objectRadius: 2.7,
  noiseCoef: 80,
  timeCoef: 40,
  mouseCoef: 30,
  scaleCoef: 1,
  minScale: 0,
  color1: '',
  color2: '',
  color3: '',
};

let objects;
const simplex = new SimplexNoise();
let gradient;

const mouse = new THREE.Vector2();
const mousePlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
const mousePosition = new THREE.Vector3();
const raycaster = new THREE.Raycaster();

function init() {
  scene = new THREE.Scene();
  camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);
  camera.position.z = 100;
  cameraCtrl = new THREE.OrbitControls(camera);
  cameraCtrl.autoRotate = true;
  cameraCtrl.autoRotateSpeed = 3;

  renderer = new THREE.WebGLRenderer({ antialias: true });
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  initRandomGradient();
  initScene();

  const gui = new dat.GUI();
  gui.add(conf, 'noiseCoef', 0, 150, 1);
  gui.add(conf, 'timeCoef', 0, 100, 1);
  gui.add(conf, 'mouseCoef', 0, 100, 1);
  gui.add(conf, 'scaleCoef', 1, 5);
  gui.add(conf, 'minScale', 0, 1, 0.1);
  const updateGradient = v => { gradient = chroma.scale([conf.color1, conf.color2, conf.color3]); };
  gui.addColor(conf, 'color1').onChange(updateGradient).listen();
  gui.addColor(conf, 'color2').onChange(updateGradient).listen();
  gui.addColor(conf, 'color3').onChange(updateGradient).listen();
  gui.close();

  onWindowResize();
  window.addEventListener('resize', onWindowResize, false);
  renderer.domElement.addEventListener('mousemove', updateMousePosition);
  renderer.domElement.addEventListener('mouseup', initRandomGradient);

  animate();
};

function initRandomGradient() {
  const color = chroma.random();
  conf.color1 = color.set('hsl.l', rnd(0.5)).hex();
  conf.color2 = color.hex();
  conf.color3 = color.set('hsl.l', 0.7 + rnd(0.3)).hex();
  gradient = chroma.scale([conf.color1, conf.color2, conf.color3]);
}

function initScene() {
  scene = new THREE.Scene();
  scene.background = new THREE.Color(0xffffff);

  this.initLights();

  objects = [];
  const o3d = new THREE.Object3D(), o = new THREE.Vector3();
  let points = getFibonacciSpherePoints(conf.nbObjects, conf.mainRadius, false);
  let geometry = new THREE.SphereBufferGeometry(conf.objectRadius, 16, 16), material, mesh;
  for (let i = 0; i < conf.nbObjects; i++) {
    let p = points[i];
    material = new THREE.MeshLambertMaterial({ color: 0x808080 });
    mesh = new THREE.Mesh(geometry, material);
    mesh.position.z = -(conf.objectRadius - 0.2);

    let group = new THREE.Object3D();
    group.mesh = mesh;
    group.add(mesh);
    group.position.set(p.x, p.y, p.z);
    group.lookAt(o);
    objects.push(group);
    o3d.add(group);
  }
  scene.add(o3d);
}

function initLights() {
  scene.add(new THREE.AmbientLight(0x808080));
  scene.add(new THREE.HemisphereLight(0xaa3030, 0x3030aa, 1));
}

function animate() {
  requestAnimationFrame(animate);

  cameraCtrl.update();

  let o, noise, scale;
  const time = Date.now() * conf.timeCoef / 200000;
  const coef = conf.noiseCoef / 2000, mcoef = conf.mouseCoef / 150;
  const mx = mouse.x * mcoef, my = mouse.y * mcoef;
  for (let i = 0; i < conf.nbObjects; i++) {
    o = objects[i];
    noise = simplex.noise3D(o.position.x * coef + mx + time, o.position.y * coef + my + time, o.position.z * coef + time);
    scale = conf.minScale + conf.scaleCoef * (1 + noise);
    o.scale.set(scale, scale, scale);
    o.mesh.material.color = new THREE.Color(gradient((1 + noise) / 2).hex());
  }

  renderer.render(scene, camera);
};

function onWindowResize() {
  width = window.innerWidth; cx = width / 2;
  height = window.innerHeight; cy = height / 2;
  camera.aspect = width / height;
  camera.updateProjectionMatrix();
  renderer.setSize(width, height);
}

function updateMousePosition(e) {
  const v = new THREE.Vector3();
  camera.getWorldDirection(v);
  v.normalize();
  mousePlane.normal = v;

  mouse.x = (e.clientX / width) * 2 - 1;
  mouse.y = - (e.clientY / height) * 2 + 1;

  raycaster.setFromCamera(mouse, camera);
  raycaster.ray.intersectPlane(mousePlane, mousePosition);
}

function getFibonacciSpherePoints(samples, radius, randomize) {
  samples = samples || 1;
  radius = radius || 1;
  randomize = randomize || true;
  let random = 1;
  if (randomize) {
    random = Math.random() * samples;
  }
  let points = []
  let offset = 2 / samples
  let increment = Math.PI * (3 - Math.sqrt(5));
  for (let i = 0; i < samples; i++) {
    let y = ((i * offset) - 1) + (offset / 2);
    let distance = Math.sqrt(1 - Math.pow(y, 2));
    let phi = ((i + random) % samples) * increment;
    let x = Math.cos(phi) * distance;
    let z = Math.sin(phi) * distance;
    x = x * radius;
    y = y * radius;
    z = z * radius;
    let point = {
      'x': x,
      'y': y,
      'z': z
    }
    points.push(point);
  }
  return points;
}

function rnd(max, negative) {
  return negative ? Math.random() * 2 * max - max : Math.random() * max;
}

init();

              
            
!
999px

Console