HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
@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;
}
import * as THREE from "https://esm.sh/three@0.160.0";
import { GLTFLoader } from 'https://esm.sh/three@0.160.0/examples/jsm/loaders/GLTFLoader.js'
import { DRACOLoader } from 'https://esm.sh/three@0.160.0/examples/jsm/loaders/DRACOLoader.js'
import gsap from "https://esm.sh/gsap@3.12.4";
import { ScrollTrigger } from 'https://esm.sh/gsap@3.12.4/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: 50, 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: 50}, {x: 80, z: 10, 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() })
Also see: Tab Triggers