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

              
                <main>
  <section id="hero"></section>
  <section id="introduction"></section>
  <section id="right"></section>
  <section id="boot"></section>
  <section id="boot-2"></section>
  <section id="sadmac"></section>
  <section id="ram-intro"></section>
  <section id="ram"></section>
</main>
<div class="webgl"></div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=EB+Garamond:ital,wght@0,600;1,600&display=swap');

html,
body {
  --col1: #000000; 
  --col2: #ffffff;
  
  padding: 0;
  margin: 0;
  background: var(--col2);
  color: var(--col1);
}

main {
  counter-reset: section -1;
  
  section {
    height: 100svh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 28vh;
    counter-increment: section;
    border-bottom: 1px solid;
    
    &::before {
      font-family: 'EB Garamond', serif;
      font-weight: 600;
      letter-spacing: -0.045em;
      content: "Section "counter(section);
    }

    &#hero,
    &#introduction {
      background: #ede8e8;
    }
  }
}

.webgl {
  padding: 0;
  position: fixed;
  left: 0;
  top: calc((100dvh - 100svh) / 2);
  height: 100svh;
  z-index: 2;
  pointer-events: none;
  line-height: 0;
}
              
            
!

JS

              
                import * as THREE from "https://esm.sh/three";
import { GLTFLoader } from 'https://esm.sh/three/examples/jsm/loaders/GLTFLoader.js'
import { DRACOLoader } from 'https://esm.sh/three/examples/jsm/loaders/DRACOLoader.js'

import gsap from "https://esm.sh/gsap";
import { ScrollTrigger } from 'https://esm.sh/gsap/ScrollTrigger'

class Scene {
  constructor(model) {
    this.views = [
      { bottom: 0, height: 1 }, //standard
      { bottom: 0, height: 0 } //wireframe
    ]
    
    this.renderer = new THREE.WebGLRenderer({
      powerPreference: 'high-performance',
      antialias: true,
      alpha: true
    })
   
    this.deviceRatio = Math.min(window.devicePixelRatio, 1.5)
    
    this.renderer.useLegacyLights = true
    this.renderer.setSize(window.innerWidth, window.innerHeight)
    this.renderer.shadowMap.enabled = true
    this.renderer.shadowMap.type = THREE.PCFSoftShadowMap
    this.renderer.setPixelRatio(this.deviceRatio)
    this.renderer.outputColorSpace = THREE.LinearSRGBColorSpace
    
    document.querySelector('.webgl').appendChild(this.renderer.domElement)
    
    //scene
    this.scene = new THREE.Scene()
    this.views.forEach((view, i) => {
      let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000)
      camera.position.fromArray([0, 0, 180])
      camera.layers.disableAll()
      camera.layers.enable(i)
      view.camera = camera
      camera.lookAt(new THREE.Vector3(0, 5, 0))
      
    })
    
    //lights
    this.light = new THREE.PointLight(0xffffff, 0.75)
    this.light.position.z = 200
    this.light.position.x = 0
    this.light.position.y = 100
    this.light.castShadow = true
    this.scene.add(this.light)

    this.softLight = new THREE.AmbientLight(0xffffff, 0.5)
    this.scene.add(this.softLight)
  
    this.spotLight = new THREE.SpotLight(0xffffff, 0)
    this.spotLight.position.set(200, 0, 100)
    this.scene.add(this.spotLight)

    this.onResize()
    window.addEventListener('resize', this.onResize, false)
    this.modelGroup = new THREE.Group()
    
    //wireframe
    const analog = model.getObjectByName('analog_body')
    
    let wireframe = analog.clone()
    wireframe.name = 'wireframe'
    wireframe.children = []
    
    analog.traverse((mesh) => {
      if(mesh.isMesh) {
        let edges = new THREE.EdgesGeometry(mesh.geometry)
        let line = new THREE.LineSegments(edges)
        line.material.depthTest = false
        line.material.opacity = 1
        line.material.lineWidth = 1
        line.material.color = new THREE.Color(0x000000)
        line.material.transparent = true
        line.layers.set(1)
        wireframe.add(line)
      }
    })
    
    model.name = 'model'
    const mac = new THREE.Group()
    mac.name = 'mac'
    mac.add(model)
    mac.add(wireframe)
    this.modelGroup.add(mac)
    this.scene.add(this.modelGroup)
  }
  
  render = () => {
    this.views.forEach((view) => {
      let camera = view.camera
      let bottom = Math.floor(this.h * view.bottom)
      let height = Math.floor(this.h * view.height)
      
      this.renderer.setViewport(0, 0, this.w, this.h)
      this.renderer.setScissor(0, bottom, this.w, height)
      this.renderer.setScissorTest(true)
      camera.aspect = this.w / this.h
      this.renderer.render(this.scene, camera)
    })
  }
  
  onResize = () => {
    let height = document.querySelector('.webgl').offsetHeight
    this.w = window.innerWidth
    this.h = height > 200 ? height : window.innerHeight 
    
    this.views.forEach((view) => {
      let camera = view.camera
      camera.aspect = this.w / this.h
      let camZ = (screen.width - (this.w * 1)) / 3
      camera.position.z = height > 1024 ? 180 + (height / 65) : 180
      camera.updateProjectionMatrix()
    })

    this.renderer.setSize(this.w, this.h)
    this.render()
  }
}

let loadModel = () => {
  let model
  let manager = new THREE.LoadingManager(() => { initScene(model) })
  
  let gltfLoader = new GLTFLoader(manager)
  const dracoLoader = new DRACOLoader()
  //directory three/examples/js/libs/draco/gltf/*
  dracoLoader.setDecoderPath('https://giorgi.io/codepen/draco/')
  gltfLoader.setDRACOLoader(dracoLoader)
  
  gltfLoader.load(
    'https://giorgi.io/codepen/mac_codepen.glb',
    (obj) => { model = obj.scene }
  )
}

let initScene = (model) => {
  let scene = new Scene(model)
  let all = scene.modelGroup
  let mac = all.getObjectByName('mac')
  
  let crt = mac.getObjectByName('crt')
  let logic = mac.getObjectByName('logic')
  let floppy = mac.getObjectByName('floppy')
  let analog = mac.getObjectByName('analog')
  let support = mac.getObjectByName('support')
  
  let camera = scene.views[0].camera

  gsap.registerPlugin(ScrollTrigger)
  ScrollTrigger.config({ ignoreMobileResize: true })
  
  let sectionTimeline = (section, callback, options={}, defaults={}, custom=()=>{}) => {
    let tl = new gsap.timeline({
      ...{
        onUpdate: scene.render,
        scrollTrigger: {
          ...{ 
            trigger: '#' + section,
            scrub: true,
            start: 'clamp(top bottom)',
            end: 'clamp(top top)',
          },
          ...options
        },
        defaults: {
          ...{
            duration: 1,
            ease: 'power2.inOut'
          },
          ...defaults
        }
      },
      ...custom
    })
    callback(tl)
  }
  
  //1 Introduction
  sectionTimeline('introduction', (tl) => {
    tl.fromTo(mac.position, { x: 67, y: -47, z: 100 }, {x: -40, y:-47, z: 20, ease: 'power1.out'}, 0)
    tl.fromTo(mac.rotation, {y: -1.22}, {y: -6.6, ease: 'power1.out'}, 0)
  })
  
  //2 Right
  sectionTimeline('right', (tl) => {
    tl.fromTo(mac.position, {x: -40, z: 20}, {x: 80, z: -50, ease: 'power1.Out'}, 0)
    tl.fromTo(mac.rotation, {y: -6.6}, {y: -0.5, ease: 'power1.Out'}, 0)
  })
  
  //3 Boot
  sectionTimeline('boot', (tl) => {
    tl.fromTo(mac.position, {x: 80}, {x: 0, ease: 'power1.Out'}, 0)
    tl.fromTo(mac.rotation, {y: -0.5}, {y: 0, ease: 'power1.Out'}, 0)
  })
  
  //4 Boot 2
  sectionTimeline('boot-2', (tl) => {
    tl.fromTo(scene.light, { intensity: 0.75 }, { intensity: 0.5, ease: 'power1.Out'}, 0)
    tl.fromTo(scene.softLight, { intensity: 0.5 }, { intensity: 0, ease: 'power1.Out'}, 0)
    tl.fromTo('body', {'--col1': '#000000', '--col2': '#ffffff'}, {'--col1': '#ffffff', '--col2': '#000', duration: 1}, 0)
  }, {
    scrub: false,
    toggleActions: 'play play reverse reverse',
    start: 'top top'
  })
  
  //5 Sad Mac
  sectionTimeline('sadmac', (tl) => {
    tl.fromTo(mac.position, {x: 0}, {x: -80, ease: 'power1.Out'}, 0)
    tl.fromTo(mac.rotation, {y: 0}, {y: 0.5, ease: 'power1.Out'}, 0)
  })
  
  //6 Ram
  sectionTimeline('ram-intro', (tl) => {
    tl.fromTo(mac.position, {x: -80, y:-47 }, {x: 80, y: 15 }, 'start')
    tl.fromTo(mac.rotation, {y: 0.5 }, {y: -1.35, duration: 0.6 }, 'start')
    tl.fromTo(scene.spotLight, { intensity: 0 }, { intensity: 0.8 }, 'start')
    
    tl.fromTo(crt.position, {z: 0 }, {z: 240, ease: 'power2.In' }, 'start+=20%')
    tl.fromTo(support.position, {z: 0 }, {z: 180, ease: 'power2.In' }, 'start+=20%')
    tl.fromTo(floppy.position, {z: 0 }, {z: 180, ease: 'power2.In' }, 'start+=20%')
    tl.fromTo(analog.position, {z: 0 }, {z: -150, ease: 'power2.In' }, 'start+=20%')
    tl.fromTo(logic.position, {z: 0 }, {z: -20 }, 'start+=20%')
    
    tl.fromTo(mac.rotation, {x: 0}, {x: 1.2 }, 'start')
    tl.fromTo(mac.scale, {x: 1, y: 1, z: 1}, {x: 1.65, y: 1.65, z: 1.65, duration: 0.7}, 'start+=30%')
  },{
    end: '200% top',
  },{
    ease: 'power1.InOut'
  })
  
  //toggle wireframe
  gsap.fromTo(scene.views[1], { height: 1, bottom: 0 }, {
    height: 0,
    bottom: 1,
    ease: 'none',
    onUpdate: scene.render,
    scrollTrigger: {
      trigger: '#right',
      scrub: true,
      start: 'top bottom',
      end: 'top top'
    }
  })
  
  //set first frame defaults
  gsap.set(scene.views[1], { height: 1, bottom: 0 })
  gsap.set(mac.position, { x: 67, y: -47, z: 100 })
  gsap.set(mac.rotation, { y: -1.22, x: 0 })
  gsap.set(mac.scale, {x: 1, y: 1, z: 1})
  scene.render()
}

document.addEventListener('DOMContentLoaded', () => { loadModel() })
              
            
!
999px

Console