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.
* { margin: 0; padding: 0; }
console.clear()
const OPTIONS = {
fadeFactor: 0.1,
scaleX: 0,
scaleY: 0,
rotationAngle: 0
}
const OBJECTS_COUNT = 100
const gui = new dat.GUI()
gui.add(OPTIONS, 'fadeFactor', 0, 0.3)
gui.add(OPTIONS, 'scaleX', -1, 1, 0.05)
gui.add(OPTIONS, 'scaleY', -1, 1, 0.05)
gui.add(OPTIONS, 'rotationAngle', -5, 5, 0.1)
// Create orthographic camera, so we can look at our fullscreen quads
let orthoCamera
{
const left = -innerWidth / 2
const right = innerWidth / 2
const top = -innerHeight / 2
const bottom = innerHeight / 2
const near = -100
const far = 100
orthoCamera = new THREE.OrthographicCamera(left, right, top, bottom, near, far)
orthoCamera.position.z = -10
orthoCamera.lookAt(new THREE.Vector3(0, 0, 0))
}
const fullscreenQuadGeometry = new THREE.PlaneGeometry(innerWidth, innerHeight)
const uvMatrix = new THREE.Matrix3()
// tx : Float, ty : Float, sx : Float, sy : Float, rotation : Float, cx : Float, cy : Float
const fadeMaterial = new THREE.ShaderMaterial({
uniforms: {
inputTexture: { value: null },
fadeFactor: { value: OPTIONS.fadeFactor },
uvMatrix: { value: uvMatrix }
},
vertexShader: `
uniform mat3 uvMatrix;
varying vec2 vUv;
void main () {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
vUv = (uvMatrix * vec3(uv, 1.0)).xy;
}
`,
fragmentShader: `
uniform sampler2D inputTexture;
uniform float fadeFactor;
varying vec2 vUv;
void main () {
float dist = distance(vUv, vec2(0.5));
vec4 texColor = texture2D(inputTexture, vUv);
vec4 fadeColor = vec4(0.0, 0.0, 0.0, 1.0);
gl_FragColor = mix(texColor, fadeColor, fadeFactor);
}
`
})
const fadePlane = new THREE.Mesh(
fullscreenQuadGeometry,
fadeMaterial
)
const resultMaterial = new THREE.MeshBasicMaterial({ map: null })
const resultPlane = new THREE.Mesh(
fullscreenQuadGeometry,
resultMaterial
)
// Create WebGLRenderer and append its canvas to DOM
const renderer = new THREE.WebGLRenderer()
renderer.setSize(innerWidth, innerHeight)
renderer.setPixelRatio(devicePixelRatio || 1)
document.body.appendChild(renderer.domElement)
// Create PerspectiveCamera
let camera
{
const fieldOfView = 45
const screenAspect = innerWidth / innerHeight
const near = 0.1
const far = 10
camera = new THREE.PerspectiveCamera(fieldOfView, screenAspect, near, far)
camera.position.z = 8
}
// Create Scene
const scene = new THREE.Scene()
// Programatically create a checkerbox texture for each object
const texture = new THREE.DataTexture(new Uint8Array([ // data
0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC,
0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF,
0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC,
0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF,
0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC,
0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF,
0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC,
0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF, 0xCC, 0xFF,
]), 8, 8, THREE.LuminanceFormat)
// Create objects to render
const boxGeometry = new THREE.BoxGeometry()
const sphereGeometry = new THREE.SphereGeometry()
const torusGeometry = new THREE.TorusGeometry()
const coneGeometry = new THREE.ConeGeometry()
const meshes = []
const meshTransforms = []
const sharedUniforms = {
checkerTexture: { value: texture }
}
for (let i = 0; i < OBJECTS_COUNT; i++) {
const randGeometryPicker = Math.random()
let geometry = boxGeometry
if (randGeometryPicker >= 0.25) {
geometry = sphereGeometry
}
if (randGeometryPicker >= 0.5) {
geometry = torusGeometry
}
if (randGeometryPicker >= 0.75) {
geometry = coneGeometry
}
const randR = Math.random()
const randG = Math.random()
const randB = Math.random()
const multColor = new THREE.Vector4(randR, randG, randB, 1)
const material = new THREE.ShaderMaterial({
uniforms: {
...sharedUniforms,
multColor: { value: multColor }
},
vertexShader: `
varying vec2 vUv;
void main () {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
vUv = uv;
}
`,
fragmentShader: `
uniform sampler2D checkerTexture;
uniform vec4 multColor;
varying vec2 vUv;
void main () {
gl_FragColor = texture2D(checkerTexture, vUv) * multColor;
}
`
})
const mesh = new THREE.Mesh(geometry, material)
// Assign random positions and scale to each object
meshTransforms.push({
x: (Math.random() - 0.5) * 5,
y: (Math.random() - 0.5) * 5,
z: (Math.random() - 0.5) * 2,
scale: Math.random() * 0.2 + 0.1
})
meshes.push(mesh)
scene.add(mesh)
}
// Create two extra framebuffers manually
// It is important we use let instead of const variables, as we will need to swap them
let framebuffer1 = new THREE.WebGLRenderTarget(innerWidth, innerHeight)
let framebuffer2 = new THREE.WebGLRenderTarget(innerWidth, innerHeight)
renderer.setClearColor(0x111111)
renderer.setRenderTarget(framebuffer1)
renderer.clearColor()
renderer.setRenderTarget(framebuffer2)
renderer.clearColor()
// Start render loop
renderer.setAnimationLoop(drawFrame)
function drawFrame (timeElapsed) {
timeElapsed /= 1000
// Animate our objects around the scene
for (let i = 0; i < OBJECTS_COUNT; i++) {
const mesh = meshes[i]
const meshTransform = meshTransforms[i]
const moveRadius = 0.4
const x = meshTransform.x + Math.cos(timeElapsed + meshTransform.y * 2) * moveRadius
const y = meshTransform.y + Math.sin(timeElapsed + meshTransform.x * 2) * moveRadius
const z = meshTransform.z + Math.cos(timeElapsed - meshTransform.x * 2) * moveRadius
const rotAddSpeed = timeElapsed
const rotX = x + rotAddSpeed
const rotY = y + rotAddSpeed
const rotZ = z + rotAddSpeed
mesh.position.set(x, y, z)
mesh.rotation.set(rotX, rotY, rotZ)
mesh.scale.set(meshTransform.scale, meshTransform.scale, meshTransform.scale)
}
renderer.autoClearColor = false
fadeMaterial.uniforms.inputTexture.value = framebuffer1.texture
fadeMaterial.uniforms.fadeFactor.value = OPTIONS.fadeFactor
renderer.setRenderTarget(framebuffer2)
renderer.render(fadePlane, orthoCamera)
renderer.render(scene, camera)
renderer.setRenderTarget(null)
resultPlane.material.map = framebuffer2.texture
renderer.render(resultPlane, orthoCamera)
const uvScaleX = mapNumberRange(OPTIONS.scaleX, -1, 1, 1.05, 0.95)
const uvScaleY = mapNumberRange(OPTIONS.scaleY, -1, 1, 1.05, 0.95)
const rotation = THREE.MathUtils.degToRad(OPTIONS.rotationAngle)
uvMatrix.setUvTransform(0, 0, uvScaleX, uvScaleY, rotation, 0.5, 0.5)
const swap = framebuffer1
framebuffer1 = framebuffer2
framebuffer2 = swap
}
function mapNumberRange (val, inMin, inMax, outMin, outMax) {
return (val - inMin) * (outMax - outMin) / (inMax - inMin) + outMin
}
Also see: Tab Triggers