Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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

Auto Save

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

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="books.css" />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"
    />
	<link href="https://fonts.googleapis.com/css2?family=Merriweather:ital,wght@0,300;0,400;0,700;0,900;1,300;1,400;1,700;1,900&display=swap" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css2?family=Meddon&display=swap" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css2?family=Meow+Script&display=swap" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css2?family=Meow+Script&family=Zeyada&display=swap" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css2?family=League+Script&display=swap" rel="stylesheet">

    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
    />
  </head>
  <body>
    <div id="container3D">
		<div id="title">
			<h1>
				<span class="new-york">NEW YORK</span>
				<span class="times">TIMES</span>
				<span class="bestseller">bestseller</span>
				<span class="books">BOOKS</span>
			  </h1>
			  
		</div>

      <div id="buttons">
        <button onclick="scatterBooks()">
          <i class="fas fa-glass-cheers"></i>
        </button>
      </div>
    </div>

    <script
      async
      src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r83/three.min.js"
    ></script>
    <script type="module" src="books.js"></script>
    <div class="author">
      Created by:
      <a href="https://www.jakobrossner.com" target="_blank">Jakob Rossner</a>
    </div>
  </body>
</html>
              
            
!

CSS

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

  body {
    font-family: 'Montserrat', cursive;
  }

#container3D {
    position: relative;
    width: 100%;
    height: 100vh;
    overflow: hidden;
  }
  
  #cameraInfo {
    position: absolute;
    top: 10px;
    left: 10px;
  }
  
  #controls {
    position: absolute;
    bottom: 20px;
    left: 20px;
  }
  
  #buttons {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
  }
  
  button {
    width: 90px;
    height: 90px;
    background-color: #ffda2a;
    border: 2px solid #8a7d44;
    color: white;
    font-size: 34px;
    cursor: pointer;
    border-radius: 50%;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.16), 0 2px 10px rgba(0, 0, 0, 0.12);
    transition: background-color 0.3s;
  }

  button i {
    text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
  }
  
  button:hover {
    background-color: #ffda2a;
  }
  
  
  #cameraPosition,
  #cameraRotation,
  #cameraLookAt {
    background-color: rgba(255, 255, 255, 0.8);
    padding: 10px;
    border-radius: 8px;
    font-family: 'Arial', sans-serif;
    font-size: 14px;
    margin-bottom: 10px;
  }

  .author {
    position: absolute;
    font-weight: 300;
    font-style: italic;
    bottom: 20px;
    right: 20px;
    font-family: 'Merriweather', serif;
    color: #000;
    font-size: 1vw;
  }
  
  #title h1 {
    position: absolute;
    top: 20px;
    left: 5%;
    text-align: left;
    text-shadow: 2px 2px 3px rgba(255, 255, 255, 0.3);
    font-size: 2vw;
    color: #333;
    letter-spacing: -0px;
  }
  
  .new-york {
    font-family: 'Merriweather', serif;
    font-weight: 600;
    font-style: italic;
  }
  
  .times {
    font-family: 'Merriweather', serif;
    font-weight: 400;
    font-style: italic;
  }
  
  .bestseller {
    font-family: 'League Script', cursive;
  }
  
  
  .books {
    font-family: 'Merriweather', serif;
    font-weight: 300;
  }

  @media (max-width: 768px) {
    #title h1 {
      font-size: 4vw; /* Adjust as needed */
    }
    button {
      width: 8vw; /* Adjust as needed */
      height: 8vw; /* Adjust as needed */
      font-size: 22px;
    }
    .author {
    font-size: 0.8vw;
  }
  }
              
            
!

JS

              
                import * as THREE from "https://cdn.skypack.dev/three@0.129.0/build/three.module.js";
import { OrbitControls } from "https://cdn.skypack.dev/three@0.129.0/examples/jsm/controls/OrbitControls.js";
import { GLTFLoader } from "https://cdn.skypack.dev/three@0.129.0/examples/jsm/loaders/GLTFLoader.js";

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
  50,
  window.innerWidth / window.innerHeight,
  0.1,
  100000
);

const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
const books = [];

let mouseX = window.innerWidth / 2;
let mouseY = window.innerHeight / 2;

let object;
let controls;

let objToRender = "dino";

const loader = new GLTFLoader();
const textureLoader = new THREE.TextureLoader();

const textures = [
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_UltraProcessedPeople.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_CantHurtMe.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_Spare.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_AtomicHabits.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_TheBoyMole.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_MidnightLibrary.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_TheLastThingHeToldMe.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_Oppenheimer.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_Sapiens.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_TheBodyKeepsTheScore.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_LessonsInChemistry.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_SubtleArtNot.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_BraidingSweetgrass.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_ThinkSlow.jpg",
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/B_Verity.jpg",
];

const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);

document.getElementById("container3D").appendChild(renderer.domElement);

camera.position.z = objToRender === "dino" ? 100 : 1100;

const topLight = new THREE.DirectionalLight(0xffffff, 7);
topLight.position.set(0, 500, 100);
topLight.castShadow = true;
scene.add(topLight);

const ambientLight = new THREE.AmbientLight(
  0x333333,
  objToRender === "dino" ? 1 : 1
);
scene.add(ambientLight);

if (objToRender === "dino") {
  controls = new OrbitControls(camera, renderer.domElement);
}

controls.target.set(-62.88, -162.58, 797.29);

camera.position.set(965.67, 341.61, 1732.97);
const rotationX = -28.18;
const rotationY = 44.58;
const rotationZ = 20.69;

camera.rotation.set(
  THREE.MathUtils.degToRad(rotationX),
  THREE.MathUtils.degToRad(rotationY),
  THREE.MathUtils.degToRad(rotationZ)
);

controls.update();

let orbitRadius = 2000;
let orbitAngle = Math.PI / 4;

function animate() {
  const x = orbitRadius * Math.cos(orbitAngle);
  const z = orbitRadius * Math.sin(orbitAngle);

  camera.position.set(x, camera.position.y, z);

  camera.lookAt(controls.target);

  orbitAngle += 0.001;
  requestAnimationFrame(animate);

  if (object && objToRender === "eye") {
    object.rotation.y = -3 + (mouseX / window.innerWidth) * 3;
    object.rotation.x = -1.2 + (mouseY * 2.5) / window.innerHeight;
  }

  if (object) {
    object.rotation.y -= 0.002;
  }

  renderer.render(scene, camera);
}

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

document.onmousemove = (e) => {
  mouseX = e.clientX;
  mouseY = e.clientY;
};

function scatterBooks() {
  stopCarousel();
  const cols = 4;
  const rows = Math.ceil(books.length / cols);

  const spacing = 600;
  const randomOffset = 0;

  books.forEach((book, index) => {
    const row = Math.floor(index / cols);
    const col = index % cols;

    book.position.x =
      col * spacing + (Math.random() * randomOffset - randomOffset / 2);
    book.position.z =
      row * spacing + (Math.random() * randomOffset - randomOffset / 2);

    book.position.y = 0;

    book.rotation.x = -90 * (Math.PI / 180);
    book.rotation.y = 0;

    book.rotation.z = (Math.random() * 17.8 - 0.4) * (Math.PI / 180);
  });
}

function gridBooks() {
  stopCarousel();
  let rows = 4;
  let cols = 3;
  let spacing = 444;

  books.forEach((book, index) => {
    let row = Math.floor(index / cols);
    let col = index % cols;
    book.position.set(col * spacing, row * 544, 0);
    book.rotation.set(0, 0, 0);
  });
}

let carouselAnimating = true;
let carouselAngle = 0.0041;

function carouselBooks() {
  if (!carouselAnimating) return;
  let radius = 30;
  books.forEach((book, index) => {
    let angle = (index / books.length) * Math.PI * 2 + carouselAngle;
    book.position.set(Math.cos(angle) * radius, 0, Math.sin(angle) * radius);
    book.lookAt(new THREE.Vector3(0, 0, 0));
  });
  carouselAngle += 0.0041;
  requestAnimationFrame(carouselBooks);
}

function startCarousel() {
  carouselAnimating = true;
  carouselBooks();
}

function stopCarousel() {
  carouselAnimating = false;
}

window.scatterBooks = scatterBooks;
window.gridBooks = gridBooks;
window.carouselBooks = carouselBooks;

function updateNumberOfBooks() {
  const numBooks = 23;

  books.forEach((book) => {
    scene.remove(book);
  });
  books.length = 0;

  loader.load(
    "https://raw.githubusercontent.com/simple11q/nybooksellers/main/bookAbetare.gltf",
    function (gltf) {
      for (let i = 0; i < numBooks; i++) {
        const textureIndex = i % textures.length;
        const texture = textureLoader.load(textures[textureIndex]);
        const model = gltf.scene.clone();
        model.traverse(function (node) {
          if (node.isMesh) {
            let newMaterial = new THREE.MeshBasicMaterial();
            newMaterial.map = texture;
            node.material = newMaterial;
          }
        });

        model.position.x += i * 2;
        model.position.z += i * 67;
        if (i >= 7) {
          model.rotation.x -= (i - 6) * 0.04;
        }
        scene.add(model);
        books.push(model);
      }
    }
  );
}

window.updateNumberOfBooks = updateNumberOfBooks;

updateNumberOfBooks();

const backgroundTexture = textureLoader.load(
  "https://raw.githubusercontent.com/simple11q/nybooksellers/main/lib_white3.jpg?token=GHSAT0AAAAAACGBXCZYDKKSXMSVQGEECYFIZG3FSSA"
);

const backgroundGeometry = new THREE.SphereGeometry(9500, 32, 32);

const backgroundMaterial = new THREE.MeshBasicMaterial({
  map: backgroundTexture,
  side: THREE.BackSide,
});

const backgroundMesh = new THREE.Mesh(backgroundGeometry, backgroundMaterial);
scene.add(backgroundMesh);

let hoveredBook = null;

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

  raycaster.setFromCamera(mouse, camera);

  const intersects = raycaster.intersectObjects(books, true);

  if (intersects.length > 0) {
    const book = intersects[0].object;
    if (hoveredBook && hoveredBook !== book) {
      hoveredBook.position.y = 0;
    }
    book.position.y = 220;
    hoveredBook = book;
  } else if (hoveredBook) {
    hoveredBook.position.y = 0;
    hoveredBook = null;
  }
});

animate();
              
            
!
999px

Console