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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

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.

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

              
                <div class="container">
  <div class="vignette"></div>
  <div class="controls">
    <button class="js-view-button" data-view="front">View Front</button>
    <button class="js-view-button" data-view="top">View Top</button>
    <button class="js-view-button" data-view="bottom">View Bottom</button>
  </div>
</div>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
}
.container {
  height: 100vh;
  width: 100%;
  position: relative;
  canvas {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    z-index: 1;
  }
}

.vignette {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  z-index: 2;
  background-image: radial-gradient(rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0.2) 100%);
  pointer-events: none;
}

.controls {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 3;
  padding: 1rem;
  button {
    -webkit-appearance: none;
    border: none;
    outline: none;
    padding: 1rem;
    margin-right: 1rem;
    margin-bottom: 1rem;
    // background-image: linear-gradient(45deg, #6880FD, #FB80A0);
    background-image: linear-gradient(45deg, #7A80FF, #B380F4);
    color: #fafafa;
    letter-spacing: 0.1rem;
    text-transform: uppercase;
    cursor: pointer;
  }
}


.dg.ac {
  z-index: 100 !important;
}
              
            
!

JS

              
                const container = document.getElementsByClassName('container')[0];
const orthoFactor = 2;

const params = {
  STRETCH_FACTOR: 10,
  AMPLITUDE_FACTOR: 1,
  SPEED_FACTOR: 2,
  LAYERS: 100,
};

const viewButtons = document.querySelectorAll('.js-view-button');

let renderer;
let camera;
let controls;
let scene;

function init() {
  const w = container.offsetWidth;
  const h = container.offsetHeight;
  
  renderer = new THREE.WebGLRenderer({antialias: true});
  renderer.setSize(w, h);
  renderer.setClearColor(0xEFEFEF);
  renderer.setPixelRatio(window.devicePixelRatio)
  container.appendChild(renderer.domElement);
  
  scene = new THREE.Scene();
  
  const cameraLeft = w / -orthoFactor;
  const cameraRight = w / orthoFactor;
  const cameraTop = h / orthoFactor;
  const cameraBottom = h / -orthoFactor;
  camera = new THREE.OrthographicCamera( cameraLeft, cameraRight, cameraTop, cameraBottom, 1, 100 );
  camera.position.set(10, 0, 0);
  camera.lookAt(scene.position);
  
  controls = new THREE.OrbitControls( camera, renderer.domElement );
  controls.enableDamping = true;
  controls.dampingFactor = 0.2;
  controls.rotateSpeed = 0.3;
  
  const hemilight = new THREE.HemisphereLight(0xFFFFFF, 0xFAFAFA, 2);
  
  scene.add(hemilight);
  
  scene.add(camera);
  
  createMeshTornado(scene, params, camera);
  console.log('render');
  render();
}

function render() {
  window.requestAnimationFrame(render);
  renderer.render(scene, camera);
  controls.update();
}

function createMeshTornado(scene, params, camera) {
  const amplitude = params.AMPLITUDE_FACTOR * params.LAYERS;
  const stretch = params.LAYERS / params.STRETCH_FACTOR;
  const mat = new THREE.MeshNormalMaterial();
  const baseSize = {
    x: 0.5,
    y: 0.1,
    z: 0.5
  };
  const basePosition = {
    x: 0.0,
    y: 0.0,
    z: 0.0
  };
  
  const containerObj = new THREE.Object3D();
  containerObj.name = 'mario';
  // containerObj.position.set(0, -(params.LAYERS*baseSize.y/2), 0);
  for(let i = 0; i < params.LAYERS + 0; i++) {
    const oscillator = ((1 - Math.cos(2 * Math.PI * (i/(amplitude)))) / stretch)
    let x = (baseSize.x * i) * oscillator;
    let y = baseSize.y;
    let z = (baseSize.z * i) * oscillator;
    
    const geo = new THREE.BoxGeometry(x, y, z);
    const box = new THREE.Mesh(geo, mat);
    box.position.set(0, y*i, 0);
    
    TweenMax.to(box.rotation, (params.LAYERS / params.SPEED_FACTOR)/i, {y: Math.PI, repeat: -1, ease: Power0.easeNone});
    
    containerObj.add(box);
  }
  
  scene.add(containerObj);
  
  //For centering the meshGroup
  var viewBox = new THREE.Box3().setFromObject(containerObj);
  viewBox.center(containerObj.position);
  containerObj.localToWorld(viewBox);
  containerObj.position.multiplyScalar(-1);

//For fitting the object to the screen when using orthographic camera

 camera.zoom = Math.min(container.offsetWidth / (viewBox.max.x - viewBox.min.x), container.offsetHeight / (viewBox.max.y - viewBox.min.y)) * 0.8;
 camera.updateProjectionMatrix();
 camera.updateMatrix();
  
}

function makeGui() {
  const gui = new dat.GUI();
  const layers = gui.add(params, 'LAYERS', 10, 200);
  const stretch = gui.add(params, 'STRETCH_FACTOR', 2, 20);
  const amp = gui.add(params, 'AMPLITUDE_FACTOR', 0.5, 2);
  const speed = gui.add(params, 'SPEED_FACTOR', 0.5, 10);

  function handleOnChange(val) {
    scene.remove( scene.getObjectByName('mario') );
    scene.add(createMeshTornado(scene, params, camera))
  }
  layers.onFinishChange(handleOnChange);
  stretch.onFinishChange(handleOnChange);
  amp.onFinishChange(handleOnChange);
  speed.onFinishChange(handleOnChange);
}

function handleViewButtonClick(e) {
  if (e.target.getAttribute('data-view') === "front") {
    camera.position.set(10, 0, 0);
  } else if (e.target.getAttribute('data-view') === "top") {
    camera.position.set(10, 15, 0);
  } else if (e.target.getAttribute('data-view') === "bottom") {
    camera.position.set(10, -15, 0);
  }
}

init();
makeGui();

window.addEventListener('resize', function() {
  renderer.setSize(window.innerWidth, window.innerHeight);
  const w = container.offsetWidth;
  const h = container.offsetHeight;
  camera.left = w / -orthoFactor;
  camera.right = w / orthoFactor;
  camera.top = h / orthoFactor;
  camera.bottom = h / -orthoFactor;
  camera.updateProjectionMatrix();
}, false);

_.forEach(viewButtons, el => {
  el.addEventListener('click', handleViewButtonClick);
})

              
            
!
999px

Console