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 URL's 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 it's URL and the proper URL extention.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="https://rawgit.com/dataarts/dat.gui/master/build/dat.gui.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/javascript" src="https://rawgit.com/mrdoob/three.js/master/build/three.min.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">
uniform float time;
uniform vec2 resolution;
void main() {
gl_Position = vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
// Author:
// Title:
#ifdef GL_ES
precision mediump float;
#endif
#define M_PI 3.1415926535897932384626433832795
#define M_2PI M_PI * 2.
#define M_HPI M_PI / 2.
#define M_E exp(1.)
uniform vec2 u_resolution;
uniform float u_time;
uniform float u_a_amplitude;
uniform float u_a_pshift;
uniform float u_a_period;
uniform float u_a_vshift;
uniform float u_a_ratio;
uniform float u_a_decay;
uniform float u_a_exponent;
uniform float u_a_plusx;
uniform float u_a_sine;
uniform float u_a_square;
uniform float u_a_sawtooth;
uniform float u_a_triangle;
uniform float u_a_pulse;
uniform float u_a_noise;
uniform float u_a_dampedSine;
uniform float u_a_sineIn;
uniform float u_a_sineOut;
uniform float u_a_sineInOut;
uniform float u_a_polyIn;
uniform float u_a_polyOut;
uniform float u_a_polyInOut;
uniform float u_o_amplitude;
uniform float u_o_pshift;
uniform float u_o_period;
uniform float u_o_vshift;
uniform float u_o_ratio;
uniform float u_o_decay;
uniform float u_o_exponent;
uniform float u_o_plusx;
uniform float u_o_sine;
uniform float u_o_square;
uniform float u_o_sawtooth;
uniform float u_o_triangle;
uniform float u_o_pulse;
uniform float u_o_noise;
uniform float u_o_dampedSine;
uniform float u_o_sineIn;
uniform float u_o_sineOut;
uniform float u_o_sineInOut;
uniform float u_o_polyIn;
uniform float u_o_polyOut;
uniform float u_o_polyInOut;
uniform float u_p_pattern_height;
uniform float u_p_band_height;
float tempo = 15.;
int pattern_n = int(ceil(u_resolution.y / u_p_pattern_height));
vec2 pattern = vec2((u_p_pattern_height / u_resolution.y) * u_resolution.x, u_p_pattern_height);
vec2 band = vec2((u_p_band_height / u_resolution.y) * u_resolution.x, u_p_band_height);
vec2 gap = vec2(((u_p_pattern_height - u_p_band_height) / u_resolution.y * u_resolution.x), (u_p_pattern_height - u_p_band_height));
vec2 pattern_st = pattern / u_resolution;
vec2 band_st = band / u_resolution;
vec2 gap_st = gap / u_resolution;
float mv() {
float t = 60.0/tempo;
return u_time / t;
}
float rand(float x) {
return fract(sin(x)*100000.0);
}
float sine(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
float b = M_2PI / period;
float c = pshift * M_2PI;
return amp * sin((x * b) + c) + vshift;
}
float square(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
float b = 1./period;
return amp * floor(fract((x * b) + pshift) + .5) + vshift;
}
float sawtooth(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
float b = 1./period;
return amp * fract((x * b) + pshift) + vshift;
}
float triangle(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
float b = 1./period;
return amp * abs(2.0 * fract((x * b) + pshift) - 1.0) + vshift;
}
float pulse(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
float ratio = vars[4]; // 0 to 1 start at .25
float b = 1./period;
return amp * floor(fract((x * b) + pshift) - ratio) + vshift;
}
float noise(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
period = M_2PI / period;
pshift = pshift * M_2PI;
float i = floor((x * period) + pshift);
float f = fract((x * period) + pshift);
return amp * mix(rand(i), rand(i + 1.0), smoothstep(0.,1.,f)) + vshift;
}
float dampedSine(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
float decay = vars[5];
period = M_2PI / period;
pshift = pshift * M_2PI;
return ((amp * pow(M_E, -1.0 * decay * x)) * (cos((period * x) + pshift))) + vshift;
}
float sineIn(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
period = M_HPI / period;
pshift = pshift * M_HPI;
return 1. - (amp * cos(mod((x * period) + pshift, M_HPI)) + vshift);
}
float sineOut(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
period = M_HPI / period;
pshift = pshift * M_HPI;
return amp * sin(mod((x * period) + pshift, M_HPI)) + vshift;
}
float sineInOut(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
period = M_PI / period;
pshift = pshift * M_PI;
return (1. - (amp * cos(mod((x * period) + pshift, M_PI)) + vshift))/2.;
}
float polyIn(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
float exponent = vars[6];
float b = 1./period;
float inner = fract(x*b + pshift);
return amp * clamp(pow(inner,exponent),0.,1.) + vshift;
}
float polyOut(float x, float vars[9]) {
float amp = vars[0];
float pshift = vars[1];
float period = vars[2];
float vshift = vars[3];
float exponent = vars[6];
float b = 1./period;
float inner = fract(x*b + pshift);
return amp * clamp((1. - pow(1.- inner,exponent)),0.,1.) + vshift + amp;
}
float polyInOut(float x, float vars[9]) {
float period = vars[2];
return (mod(x,period) <= period/2.) ? polyIn(x*2., vars) : polyOut(x*2., vars);
}
float alpha(float x) {
float o = 0.;
float vars[9];
vars[0] = u_a_amplitude;
vars[1] = u_a_pshift;
vars[2] = u_a_period;
vars[3] = u_a_vshift;
vars[4] = u_a_ratio;
vars[5] = u_a_decay;
vars[6] = u_a_exponent;
vars[7] = u_a_plusx;
vars[8] = 0.;
o += sine(x, vars) * u_a_sine;
o += square(x, vars) * u_a_square;
o += sawtooth(x, vars) * u_a_sawtooth;
o += triangle(x, vars) * u_a_triangle;
o += pulse(x, vars) * u_a_pulse;
o += noise(x, vars) * u_a_noise;
o += dampedSine(x, vars) * u_a_dampedSine;
o += sineIn(x, vars) * u_a_sineIn;
o += sineOut(x, vars) * u_a_sineOut;
o += sineInOut(x, vars) * u_a_sineInOut;
o += polyIn(x, vars) * u_a_polyIn;
o += polyOut(x, vars) * u_a_polyOut;
o += polyInOut(x, vars) * u_a_polyInOut;
o += u_a_plusx * x;
return o;
}
float omega(float x) {
float o = 0.;
float vars[9];
vars[0] = u_o_amplitude;
vars[1] = u_o_pshift;
vars[2] = u_o_period;
vars[3] = u_o_vshift;
vars[4] = u_o_ratio;
vars[5] = u_o_decay;
vars[6] = u_a_exponent;
vars[7] = u_o_plusx;
vars[8] = 0.;
o += sine(x, vars) * u_o_sine;
o += square(x, vars) * u_o_square;
o += sawtooth(x, vars) * u_o_sawtooth;
o += triangle(x, vars) * u_o_triangle;
o += pulse(x, vars) * u_o_pulse;
o += noise(x, vars) * u_o_noise;
o += dampedSine(x, vars) * u_o_dampedSine;
o += sineIn(x, vars) * u_o_sineIn;
o += sineOut(x, vars) * u_o_sineOut;
o += sineInOut(x, vars) * u_o_sineInOut;
o += polyIn(x, vars) * u_o_polyIn;
o += polyOut(x, vars) * u_o_polyOut;
o += polyInOut(x, vars) * u_o_polyInOut;
o += u_o_plusx * x;
return o;
}
float delta(vec2 st) {
float ax = alpha(st.x);
float ox = omega(st.x);
float factor = st.y;
return ax + ((ox - ax) * factor);
}
float eval(vec2 st) {
float fx = delta(st);
float dist = mod((st.y - fx), pattern_st.y);
return step(dist,band_st.y);
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
st.x *= u_resolution.x/u_resolution.y;
// st *= 2.0 - 1.0;
vec3 color = vec3(eval(st));
gl_FragColor = vec4(color,1.0);
}
</script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
html,body{
height:100%;
padding:0;
margin:0;
background: #000000;
}
*{
box-sizing:border-box;
}
canvas {
border-width: 10px;
border-style: solid;
border-color: #ffffff;
width: auto;
height: auto;
}
#container {
width:100%;
height:100%;
display:flex;
justify-content:center;
align-items:center;
}
var opts = {
canvas: {
width: {val: 1080, min: 100, max: 2000, name: "Width"},
height:{val: 720, min: 100, max: 2000, name: "Height"},
},
alpha: {
amplitude: {val: .25, min:-4, max:4, name: "Amplitude"},
pshift: {val: 0.0, min: -4, max: 4, name: "Phase Shift"},
period: {val: .25, min: 0.001, max: 4, name: "Period"},
vshift: {val: .5, min: -4, max: 4, name: "Vertical Shift"},
ratio: {val: .5, min: 0, max: 1, name: "Pulse Ratio"},
decay: {val: 4, min: 0, max: 10, name: "Damp Decay"},
exponent: {val: 3, min: 0, max:6, name: "Exponent"},
plusx: {val: 0, min: -10.0, max: 10, name: "Plus X"},
type: {val: "sine", name: "Wave Type"}
},
omega: {
amplitude: {val: .25, min:-4, max:4, name: "Amplitude"},
pshift: {val: 0.0, min: -4, max: 4, name: "Phase Shift"},
period: {val: .25, min: 0.001, max: 4, name: "Period"},
vshift: {val: .5, min: -4, max: 4, name: "Vertical Shift"},
ratio: {val: .5, min: 0, max: 1, name: "Pulse Ratio"},
decay: {val: 4, min: 0, max: 10, name: "Damp Decay"},
exponent: {val: 3, min: 0, max:6, name: "Exponent"},
plusx: {val: 0, min: -10.0, max: 10.0, name: "Plus X"},
type: {val: "sine", name: "Wave Type"}
},
pattern: {
patternHeight: {val: 50, min: 0, max: 400, name: "pattern Height"},
bandHeight: {val: 25, min: 0, max: 400, name: "Band Height"}
}
};
var waveTypes = [
"sine",
"square",
"sawtooth",
"triangle",
"pulse",
"noise",
"dampedSine",
"sineIn",
"sineOut",
"sineInOut",
"polyIn",
"polyOut",
"polyInOut",
]
var container;
var camera, scene, renderer;
var uniforms, material, mesh;
var gui, alphaF, omegaF, canvasF, patternF, beatF;
var startTime = Date.now();
init();
animate();
function init() {
initUniforms();
initThree();
initGUI();
}
function initUniforms() {
uniforms = {
u_time: { type: "f", value: 1.0 },
u_resolution: { type: "v2", value: new THREE.Vector2(opts.canvas.width.val, opts.canvas.height.val) },
u_a_amplitude: { type: "f", value: opts.alpha.amplitude.val },
u_a_pshift: { type: "f", value: opts.alpha.pshift.val },
u_a_period: { type: "f", value: opts.alpha.period.val },
u_a_vshift: { type: "f", value: opts.alpha.vshift.val },
u_a_ratio: {type: "f", value: opts.alpha.ratio.val},
u_a_decay: {type: "f", value: opts.alpha.decay.val},
u_a_exponent: {type: "f", value: opts.alpha.exponent.val},
u_a_plusx: {type:"f", value: opts.alpha.plusx.val},
u_o_amplitude: { type: "f", value: opts.omega.amplitude.val },
u_o_pshift: { type: "f", value: opts.omega.pshift.val },
u_o_period: { type: "f", value: opts.omega.period.val },
u_o_vshift: { type: "f", value: opts.omega.vshift.val },
u_o_ratio: {type: "f", value: opts.omega.ratio.val},
u_o_decay: {type: "f", value: opts.omega.decay.val},
u_o_exponent: {type: "f", value: opts.omega.exponent.val},
u_o_plusx: {type:"f", value: opts.omega.plusx.val},
u_p_pattern_height: {type: "f", value: opts.pattern.patternHeight.val},
u_p_band_height: {type: "f", value: opts.pattern.bandHeight.val}
};
waveTypes.forEach(w => {
var aVal = opts.alpha.type.val == w ? 1 : 0;
var oVal = opts.omega.type.val == w ? 1 : 0;
var uaname = "u_a_" + w;
var uoname = "u_o_" + w;
uniforms[uaname] = {type: "f", value: aVal};
uniforms[uoname] = {type: "f", value: oVal};
});
}
function initThree() {
container = document.getElementById('container');
camera = new THREE.Camera();
camera.position.z = 1;
scene = new THREE.Scene();
material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragmentShader').textContent
});
mesh = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio ? window.devicePixelRatio : 1);
container.appendChild(renderer.domElement);
renderer.setSize(opts.canvas.width.val, opts.canvas.height.val);
}
function initGUI() {
gui = new dat.GUI();
alphaF = gui.addFolder("Alpha-Wave");
omegaF = gui.addFolder("Omega-Wave");
patternF = gui.addFolder("Pattern");
canvasF = gui.addFolder("Canvas");
var vars = {
alphaType: opts.alpha.type.val,
omegaType: opts.alpha.type.val
};
alphaF.open();
omegaF.open();
alphaF.add(uniforms.u_a_amplitude, "value").min(opts.alpha.amplitude.min).max(opts.alpha.amplitude.max).name(opts.alpha.amplitude.name);
alphaF.add(uniforms.u_a_pshift, "value").min(opts.alpha.pshift.min).max(opts.alpha.pshift.max).name(opts.alpha.pshift.name);
alphaF.add(uniforms.u_a_period, "value").min(opts.alpha.period.min).max(opts.alpha.period.max).name(opts.alpha.period.name);
alphaF.add(uniforms.u_a_vshift, "value").min(opts.alpha.vshift.min).max(opts.alpha.vshift.max).name(opts.alpha.vshift.name);
alphaF.add(uniforms.u_a_ratio, "value").min(opts.alpha.ratio.min).max(opts.alpha.ratio.max).name(opts.alpha.ratio.name);
alphaF.add(uniforms.u_a_decay, "value").min(opts.alpha.decay.min).max(opts.alpha.decay.max).name(opts.alpha.decay.name);
alphaF.add(uniforms.u_a_exponent, "value").min(opts.alpha.exponent.min).max(opts.alpha.exponent.max).name(opts.alpha.exponent.name);
alphaF.add(uniforms.u_a_plusx, "value").min(opts.alpha.plusx.min).max(opts.alpha.plusx.max).name(opts.alpha.plusx.name);
alphaF.add(vars, "alphaType", waveTypes).name(opts.alpha.type.name).onChange(val => {
waveTypes.forEach(w => {
var aVal = (val === w) ? 1 : 0;
var uaname = "u_a_" + w;
uniforms[uaname].value = aVal;
});
});
omegaF.add(uniforms.u_o_amplitude, "value").min(opts.omega.amplitude.min).max(opts.omega.amplitude.max).name(opts.omega.amplitude.name);
omegaF.add(uniforms.u_o_pshift, "value").min(opts.omega.pshift.min).max(opts.omega.pshift.max).name(opts.omega.pshift.name);
omegaF.add(uniforms.u_o_period, "value").min(opts.omega.period.min).max(opts.omega.period.max).name(opts.omega.period.name);
omegaF.add(uniforms.u_o_vshift, "value").min(opts.omega.vshift.min).max(opts.omega.vshift.max).name(opts.omega.vshift.name);
omegaF.add(uniforms.u_o_ratio, "value").min(opts.omega.ratio.min).max(opts.omega.ratio.max).name(opts.omega.ratio.name);
omegaF.add(uniforms.u_o_decay, "value").min(opts.omega.decay.min).max(opts.omega.decay.max).name(opts.omega.decay.name);
omegaF.add(uniforms.u_o_exponent, "value").min(opts.omega.exponent.min).max(opts.omega.exponent.max).name(opts.omega.exponent.name);
omegaF.add(uniforms.u_o_plusx, "value").min(opts.omega.plusx.min).max(opts.omega.plusx.max).name(opts.omega.plusx.name);
omegaF.add(vars, "omegaType", waveTypes).name(opts.omega.type.name).onChange(val => {
waveTypes.forEach(w => {
var oVal = (val === w) ? 1 : 0;
var uoname = "u_o_" + w;
uniforms[uoname].value = oVal;
console.log(uniforms[uoname].value);
});
});
patternF.add(uniforms.u_p_pattern_height, "value").min(opts.pattern.patternHeight.min).max(opts.pattern.patternHeight.max).name(opts.pattern.patternHeight.name);
patternF.add(uniforms.u_p_band_height, "value").min(opts.pattern.bandHeight.min).max(opts.pattern.bandHeight.max).name(opts.pattern.bandHeight.name);
canvasF.add(uniforms.u_resolution.value, "x").min(opts.canvas.width.min).max(opts.canvas.width.max).name(opts.canvas.width.name).onChange(val => {
renderer.setSize(val, renderer.getSize().y);
});
canvasF.add(uniforms.u_resolution.value, "y").min(opts.canvas.height.min).max(opts.canvas.height.max).name(opts.canvas.height.name).onChange(val => {
renderer.setSize(renderer.getSize().x, val);
});
}
function animate() {
requestAnimationFrame(animate);
update();
render();
}
function update() {
var elapsedMilliseconds = Date.now() - startTime;
var elapsedSeconds = elapsedMilliseconds / 1000.;
uniforms.u_time.value = elapsedSeconds;
}
function render() {
renderer.render(scene, camera);
}
Also see: Tab Triggers