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.
<div id="container"></div>
<button id="start">start</button>
<script type="x-shader/x-vertex" id="vertexshader">
uniform float amplitude;
attribute vec3 customColor;
attribute vec3 displacement;
varying vec3 vNormal;
varying vec3 vColor;
void main() {
vNormal = normal;
vColor = customColor;
vec3 newPosition = position + normal * amplitude * displacement;
gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
}
</script>
<script type="x-shader/x-fragment" id="fragmentshader">
varying vec3 vNormal;
varying vec3 vColor;
void main() {
const float ambient = 0.6;
vec3 light = vec3( 1.0 );
light = normalize( light );
float directional = max( dot( vNormal, light ), 0.0 );
gl_FragColor = vec4( ( directional + ambient ) * vColor, 1.0 );
}
</script>
body {
margin: 0px;
overflow: hidden;
background: rgb(201,14,157); /* Old browsers */
background: -moz-linear-gradient(top, rgba(201,14,157,1) 0%, rgba(64,11,102,1) 52%, rgba(22,32,135,1) 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(top, rgba(201,14,157,1) 0%,rgba(64,11,102,1) 52%,rgba(22,32,135,1) 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, rgba(201,14,157,1) 0%,rgba(64,11,102,1) 52%,rgba(22,32,135,1) 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#c90e9d', endColorstr='#162087',GradientType=0 ); /* IE6-9 */
}
button {
background: black;
color: white;
border-radius: 4px;
padding: 10px 20px 8px;
position: fixed;
top: 40px;
left: calc(50vw - 50px);
font-size: 11px;
text-transform: uppercase;
letter-spacing: 0.2em;
border: 1px solid #555;
cursor: pointer;
}
audio {
position: fixed;
z-index: 3000;
top: 10px;
left: 10px;
opacity: 0.7;
}
// heavily commented for those trying to learn
const initCanvasAudio = name => {
// create the new audio
const audio = new Audio();
audio.src = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/28963/song18.mp3";
audio.controls = true;
audio.autoplay = true;
audio.crossOrigin = "anonymous";
document.body.appendChild(audio);
// wire it up
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const source = audioCtx.createMediaElementSource(audio);
const volumeControl = audioCtx.createGain();
source.connect(audioCtx.destination);
source.connect(volumeControl);
// I'm American so it's with a z
const analyzer = audioCtx.createAnalyser();
volumeControl.connect(analyzer);
analyzer.connect(audioCtx.destination);
//connect the volume adjustments from the user
volumeControl.gain.value = audio.volume;
// now we start with the three initialization
let renderer, scene, camera, stats, controls, mesh, uniforms;
let width = window.innerWidth,
height = window.innerHeight;
//have to kick off init and the animation
init();
animate();
function init() {
// create the camera and hook up orbit controls
camera = new THREE.PerspectiveCamera(40, width / height, 1, 10000);
camera.position.set(0, 0, 100);
controls = new THREE.OrbitControls(camera);
controls.autoRotate = true;
// create the scene
scene = new THREE.Scene();
// create the geometry
let geometry = new THREE.TorusKnotGeometry(20, 0.8, 67, 18, 15, 12);
geometry.center();
let tessellateModifier = new THREE.TessellateModifier(8);
for (let i = 0; i < 6; i++) {
tessellateModifier.modify(geometry);
}
geometry = new THREE.BufferGeometry().fromGeometry(geometry);
let numFaces = geometry.attributes.position.count / 3;
//map the colors, fragments
let colors = new Float32Array(numFaces * 3 * 3);
let displacement = new Float32Array(numFaces * 3 * 3);
let color = new THREE.Color();
for (let f = 0; f < numFaces; f++) {
let index = 9 * f;
let h = 0.2 * Math.random();
let s = 0.5 + 0.5 * Math.random();
let l = 0.5 + 0.5 * Math.random();
color.setHSL(h, s, l);
let d = 10 * (0.5 - Math.random());
for (let i = 0; i < 3; i++) {
colors[index + 5 * i] = color.r;
colors[index + 8 * i + 1] = color.g;
colors[index + 2 * i + 2] = color.b;
displacement[index + 3 * i] = d;
displacement[index + 3 * i + 1] = d;
displacement[index + 3 * i + 2] = d;
}
}
// add them to the geometry
geometry.addAttribute("customColor", new THREE.BufferAttribute(colors, 3));
geometry.addAttribute(
"displacement",
new THREE.BufferAttribute(displacement, 3)
);
// attach the shader material you see in the html
uniforms = {
amplitude: { value: 0.0 }
};
const shaderMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: document.getElementById("vertexshader").textContent,
fragmentShader: document.getElementById("fragmentshader").textContent
});
// create the mesh (this is where the geometry and material are added to the scene)
mesh = new THREE.Mesh(geometry, shaderMaterial);
scene.add(mesh);
// renderer
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(width, height);
renderer.autoClear = false;
renderer.setClearColor(0x000000, 0.0);
const container = document.getElementById("container");
container.appendChild(renderer.domElement);
window.addEventListener("resize", onWindowResize, false);
}
// make it still work when you resize the screen
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
controls.update();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// rAF and get the frequency data from the audio all the time so we can use it to update the amplitude
function animate() {
var freqData = new Uint8Array(analyzer.frequencyBinCount);
analyzer.getByteFrequencyData(freqData);
requestAnimationFrame(animate);
render(freqData);
}
//render the sucker
function render(freqData) {
// this is what makes the shader pop. This line of code feeds the audio in
uniforms.amplitude.value = numscale(freqData[0], 0, 300, -2, 2);
// we have to update the orbit controls anytime we render
controls.update();
renderer.render(scene, camera);
}
};
// chrome needs sound kicked off by the user now
const button = document.querySelector("button");
button.addEventListener("click", event => {
initCanvasAudio();
button.remove();
});
// helper function to map scales
const numscale = (num, in_min, in_max, out_min, out_max) => {
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
};
Also see: Tab Triggers