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

              
                <section>
    <div class="canvas"></div>
</section>
              
            
!

CSS

              
                html, body {
    padding: 0;
    margin: 0;
}

video, canvas, iframe {
    display: block;
}

.canvas {
    width: 100vw;
    height: 100vh;
    canvas {
        display: block;
        width: 100%;
        height: 100%;
    }
}
              
            
!

JS

              
                const canvas = document.querySelector(".canvas");

init(canvas);

function init(ele) {
  if (ele != null) {
    ele.camera = createCamera(ele);
    ele.scene = createScene(ele);
    ele.renderer = createRenderer(ele);

    // ====================================
    // add objects ========================
    // ====================================

    const objects = {
      plane: createPlane(),
      "cube 1": createCube({ x: -0.95, y: 0.05, z: -0.95 }, 45),
      "cube 2": createCube({ x: 0.95, y: 0.05, z: -0.95 }, -45),
      "cube 3": createCube({ x: -0.95, y: 0.05, z: 0.95 }, 135),
      "cube 4": createCube({ x: 0.95, y: 0.05, z: 0.95 }, -135)
    };

    for (const prop in objects) {
      objects[prop].name = prop;
      ele.scene.add(objects[prop]);
    }

    function createCube(position, yRotation) {
      const size = 0.1;
      const geometry = new THREE.BoxGeometry(size, size, size);
      const material = new THREE.MeshBasicMaterial(0xff00ce);
      const cube = new THREE.Mesh(geometry, material);

      cube.position.x = position.x;
      cube.position.y = position.y;
      cube.position.z = position.z;

      cube.clickable = true;

      cube.animation = gsap.timeline({
        paused: true,
        onUpdate: function () {
          ele.camera.updateProjectionMatrix();
        }
      });

      cube.animation.to(ele.camera.position, 2, { x: 0, y: 0.12, z: 0 });
      cube.animation.to(
        ele.camera.rotation,
        2,
        {
          x: THREE.MathUtils.degToRad(0),
          y: THREE.MathUtils.degToRad(yRotation),
          z: THREE.MathUtils.degToRad(0)
        },
        0
      );
      cube.animation.to(ele.camera, 2, { zoom: 6 }, 0);

      return cube;
    }

    // ====================================
    // controls ===========================
    // ====================================

    ele.raycaster = new THREE.Raycaster();
    ele.mouse = new THREE.Vector2();

    mouseMove();

    function mouseMove() {
      window.addEventListener("mousemove", function (e) {
        ele.mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
        ele.mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;

        ele.raycaster.setFromCamera(ele.mouse, ele.camera);

        ele.intersects = ele.raycaster.intersectObjects(
          ele.scene.children,
          true
        );

        // changes onject is 'clickable'
        container.style.cursor = isClickable(ele.intersects) ? "pointer" : "";
      });
    }

    cubeClick();

    function cubeClick() {
      window.addEventListener("click", function (e) {
        if (isClickable(ele.intersects)) {
          ele.intersects[0].object.animation.play();
        }
      });
    }

    function isClickable(arr) {
      const clickable =
        arr.length && "clickable" in arr[0].object && arr[0].object.clickable;

      return clickable;
    }

    // ====================================
    // boilerplate stuff ==================
    // ====================================

    render();
    resize();

    function render(time) {
      requestAnimationFrame(render);
      ele.renderer.render(ele.scene, ele.camera);
    }

    function resize() {
      window.addEventListener("resize", function () {
        ele.renderer.setSize(window.innerWidth, window.innerHeight);
        ele.camera.aspect = window.innerWidth / window.innerHeight;
        ele.camera.updateProjectionMatrix();
      });
    }
  }
}

function createPlane() {
  const geometry = new THREE.PlaneGeometry(2, 2);
  const material = new THREE.MeshBasicMaterial({
    color: "orange",
    side: THREE.DoubleSide
  });
  const plane = new THREE.Mesh(geometry, material);

  plane.position.set(0, 0, 0);
  plane.rotation.x = THREE.MathUtils.degToRad(90);

  return plane;
}

// ====================================
// boilerplate stuff ==================
// ====================================

function createCamera(node) {
  const fov = 100; // Camera frustum vertical field of view.
  const aspect = window.innerWidth / window.innerHeight; // Camera frustum aspect ratio.
  const near = 0.01; // Camera frustum near plane.
  const far = 5; // Camera frustum far plane.

  const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);

  camera.position.z = 2;
  camera.position.y = THREE.MathUtils.degToRad(60);

  camera.lookAt(0, 0, 0);

  return camera;
}

function createScene(node) {
  const scene = new THREE.Scene();

  return scene;
}

function createRenderer(node) {
  const renderer = new THREE.WebGLRenderer({ antialias: true });

  renderer.setSize(window.innerWidth, window.innerHeight);

  node.appendChild(renderer.domElement);

  return renderer;
}

              
            
!
999px

Console