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

              
                <div class="container">
  <h1 class="text"><span>it's a crane</span><span>it's a crane</span></h1>
</div>
              
            
!

CSS

              
                body {
  margin: 0;
  background-color: #edda5b;
  overflow: hidden;
}

.container {
  height: 100vh;
  display: flex;
  align-items: center;
  z-index: -1;
  position: absolute;
}

.text {
  display: inline-flex;
  overflow: hidden;
  font-size: 6rem;
  text-transform: uppercase;
}

.text span {
  white-space: nowrap;
  -webkit-animation: text-animation 30s linear infinite;
  animation: text-animation 30s linear infinite;
  padding-right: 5vw;
  letter-spacing: 4vw;
  color: #f8f8f8;
  font-family: "Trispace", sans-serif;
}

@-webkit-keyframes text-animation {
  0% {
    -webkit-transform: translate3D(0, 0, 0);
  }

  100% {
    -webkit-transform: translate3D(-100%, 0, 0);
  }
}

@keyframes text-animation {
  0% {
    transform: translate3D(0, 0, 0);
  }

  100% {
    transform: translate3D(-100%, 0, 0);
  }
}

              
            
!

JS

              
                import Stats from "https://esm.sh/stats.js@0.17.0";
import * as CANNON from "https://esm.sh/cannon-es@0.20.0";
import * as THREE from "https://esm.sh/three@0.158.0";
import { GLTFLoader } from "https://esm.sh/three@0.158.0/addons/loaders/GLTFLoader.js";

// fps stats
const stats = new Stats();
stats.domElement.style.position = "absolute";
stats.domElement.style.left = "0px";
stats.domElement.style.top = "0px";
stats.setMode(0);
document.body.appendChild(stats.domElement);

// renderer init
const renderer = new THREE.WebGLRenderer({
  alpha: true,
  antialias: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);

// world physics init
const world = new CANNON.World();
world.gravity.set(0, -9.82 * 2, 0); // m/s²

//scene + camera init
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
  45,
  window.innerWidth / window.innerHeight,
  1,
  100
);
camera.position.set(2, 4, -7);
camera.lookAt(0.5, 2, 0);

//model init
let model, ropeEnd;
const loader = new GLTFLoader();
loader.load("https://ngie1rdk6xf2zgb2.public.blob.vercel-storage.com/QmTebAT7KbT5hN2wv8LF52Rm3P6TE4NhYHJcRg4iknos6X-0LqHlJrxHnLuqUHgCy9LLqaMamgvJr.gltf",
  (gltf) => {
    gltf.scene.traverse((object) => {
      if (object.isMesh) {
        object.castShadow = true; //enable shadows
      }
    });
    scene.add(gltf.scene);
    model = scene.getObjectByName("grua");
    ropeEnd = scene.getObjectByName("end");
    model.rotation.y = -1.2;
    scene.getObjectByName("base").receiveShadow = true;

    initRope();
  }
);

//lights init
const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 0.5 * Math.PI);
scene.add(hemiLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8 * Math.PI);
dirLight.position.set(-10, 10, -10);
dirLight.shadow.bias = -0.0002;
dirLight.castShadow = true;
dirLight.shadow.mapSize.width = 1048;
dirLight.shadow.mapSize.height = 1048;
scene.add(dirLight);

//ground init
const planeMaterial = new THREE.ShadowMaterial();
planeMaterial.opacity = 0.2;
const geometry = new THREE.PlaneGeometry(20, 20);
const groundMesh = new THREE.Mesh(geometry, planeMaterial);
groundMesh.rotation.x = -Math.PI / 2;
groundMesh.receiveShadow = true;
scene.add(groundMesh);

function initRope() {
  var mass = 10;
  var size = 0.05;
  var dist = size * 2;
  var lastAddedSegmentBody = null;
  var N = 20;
  const point = new THREE.Vector3();
  scene.getObjectByName("point").getWorldPosition(point);
  world.solver.iterations = N; // to be able to propagate force throw the chain of N spheres, we need at least N solver iterations.
  for (let i = 0; i < N; i++) {
    const segmentBody = new CANNON.Body({ mass: i === 0 ? 0 : mass });
    segmentBody.addShape(new CANNON.Box(new CANNON.Vec3(size, size, size)));

    if (i !== N - 1) {
      segmentBody.position.set(point.x, -i * dist + point.y, point.z);
      ropeSegmentsMesh.push(
        new THREE.Vector3(point.x, -i * dist + point.y, point.z)
      );
    } else {
      //last
      segmentBody.mass = 30;
      segmentBody.position.set(point.x, -i * dist + point.y - 1, point.z);
      segmentBody.quaternion.setFromAxisAngle(
        new CANNON.Vec3(0, 0, 1),
        -Math.PI / 2
      );
    }

    ropeSegmentsBody.push(segmentBody);
    world.addBody(segmentBody);

    // connect this body to the last one added
    if (lastAddedSegmentBody != null) {
      world.addConstraint(
        new CANNON.DistanceConstraint(
          segmentBody,
          lastAddedSegmentBody,
          dist - 0.02
        )
      );
    }
    // keep track of the lastly added body
    lastAddedSegmentBody = segmentBody;
  }

  const material = new THREE.LineBasicMaterial({ color: 0x00000b });
  const geometry = new THREE.BufferGeometry().setFromPoints(ropeSegmentsMesh);
  rope = new THREE.Line(geometry, material);
  rope.castShadow = true;
  scene.add(rope);
}

let rope;
let ropeSegmentsMesh = [];
let ropeSegmentsBody = [];

function animate() {
  updatePhysics();
  render();
  stats.update();

  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}
animate();

const pointerPos = { x: 0, y: 0 };
function render() {
  renderer.render(scene, camera);
  if (model && pointerPos.x !== 0) {
    model.rotation.y +=
      (normalize(pointerPos.x, -1, 1, -1.5, 0.5) - model.rotation.y) * 0.01;
  }
}

function updatePhysics() {
  world.fixedStep(); //synchronize three and cannon

  if (rope) {
    const ropePositions = rope.geometry.attributes.position.array;
    let indexFloat = 0;
    ropeSegmentsBody.forEach((e, index) => {
      if (index === 0) {
        const point = new THREE.Vector3();
        scene.getObjectByName("point").getWorldPosition(point);
        e.position.set(point.x, point.y, point.z);
      } else if (index === ropeSegmentsBody.length - 1) {
        ropeEnd.position.copy(e.position);
        ropeEnd.quaternion.copy(e.quaternion);
        ropePositions[indexFloat++] = e.position.x;
        ropePositions[indexFloat++] = e.position.y;
        ropePositions[indexFloat++] = e.position.z;
      } else {
        ropePositions[indexFloat++] = e.position.x;
        ropePositions[indexFloat++] = e.position.y;
        ropePositions[indexFloat++] = e.position.z;
      }
    });
    rope.geometry.attributes.position.needsUpdate = true;
  }
}

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

//mouse movement
function move(event) {
  const tx = -1 + (event.clientX / window.innerWidth) * 2;
  const ty = 1 - (event.clientY / window.innerHeight) * 2;
  pointerPos.x = tx;
  pointerPos.y = ty;
}
document.addEventListener("pointermove", move, false);

function normalize(v, vmin, vmax, tmin, tmax) {
  const nv = Math.max(Math.min(v, vmax), vmin);
  const dv = vmax - vmin;
  const pc = (nv - vmin) / dv;
  const dt = tmax - tmin;
  const tv = tmin + pc * dt;
  return tv;
}

              
            
!
999px

Console