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.
<canvas id="canvas"></canvas>
<p class="collection">
<a href="https://codepen.io/collection/AGZywR" target="_blank">WebGL Collection</a>
</p>
body {
margin: 0;
}
canvas {
display: block;
}
function App() {
const conf = {
nx: 40,
ny: 100,
cscale: chroma.scale(['#2175D8', '#DC5DCE', '#CC223D', '#F07414', '#FDEE61', '#74C425']).mode('lch'),
darken: -1,
angle: Math.PI / 3,
timeCoef: 0.1
};
let renderer, scene, camera;
let width, height;
const { randFloat: rnd } = THREE.Math;
const uTime = { value: 0 }, uTimeCoef = { value: conf.timeCoef };
const polylines = [];
init();
function init() {
renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('canvas'), antialias: true });
camera = new THREE.PerspectiveCamera();
updateSize();
window.addEventListener('resize', updateSize, false);
document.body.addEventListener('click', initRandomScene);
initScene();
requestAnimationFrame(animate);
}
function initScene() {
scene = new THREE.Scene();
const vertexShader = `
uniform float uTime, uTimeCoef;
uniform float uSize;
uniform mat2 uMat2;
uniform vec3 uRnd1;
uniform vec3 uRnd2;
uniform vec3 uRnd3;
uniform vec3 uRnd4;
uniform vec3 uRnd5;
attribute vec3 next, prev;
attribute float side;
varying vec2 vUv;
vec2 dp(vec2 sv) {
return (1.5 * sv * uMat2);
}
void main() {
vUv = uv;
vec2 pos = dp(position.xy);
// Well... I know I should update geometry instead...
// Computing normal here is not needed
// vec2 sprev = dp(prev.xy);
// vec2 snext = dp(next.xy);
// vec2 tangent = normalize(snext - sprev);
// vec2 normal = vec2(-tangent.y, tangent.x);
// float dist = length(snext - sprev);
// normal *= smoothstep(0.0, 0.02, dist);
vec2 normal = dp(vec2(1, 0));
normal *= uSize;
float time = uTime * uTimeCoef;
vec3 rnd1 = vec3(cos(time * uRnd1.x + uRnd3.x), cos(time * uRnd1.y + uRnd3.y), cos(time * uRnd1.z + uRnd3.z));
vec3 rnd2 = vec3(cos(time * uRnd2.x + uRnd4.x), cos(time * uRnd2.y + uRnd4.y), cos(time * uRnd2.z + uRnd4.z));
normal *= 1.0
+ uRnd5.x * (cos((position.y + rnd1.x) * 20.0 * rnd1.y) + 1.0)
+ uRnd5.y * (sin((position.y + rnd2.x) * 20.0 * rnd2.y) + 1.0)
+ uRnd5.z * (cos((position.y + rnd1.z) * 20.0 * rnd2.z) + 1.0);
pos.xy -= normal * side;
gl_Position = vec4(pos, 0.0, 1.0);
}
`;
const fragmentShader = `
uniform vec3 uColor1;
uniform vec3 uColor2;
varying vec2 vUv;
void main() {
gl_FragColor = vec4(mix(uColor1, uColor2, vUv.x), 1.0);
}
`;
const dx = 2 / (conf.nx), dy = -2 / (conf.ny - 1);
const ox = -1 + dx / 2, oy = 1;
const mat2 = Float32Array.from([Math.cos(conf.angle), -Math.sin(conf.angle), Math.sin(conf.angle), Math.cos(conf.angle)]);
for (let i = 0; i < conf.nx; i++) {
const points = [];
for (let j = 0; j < conf.ny; j++) {
const x = ox + i * dx, y = oy + j * dy;
points.push(new THREE.Vector3(x, y, 0));
}
const polyline = new Polyline({ points });
polylines.push(polyline);
const material = new THREE.ShaderMaterial({
uniforms: {
uTime,
uTimeCoef,
uMat2: { value: mat2 },
uSize: { value: 1.5 / conf.nx },
uRnd1: { value: new THREE.Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)) },
uRnd2: { value: new THREE.Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)) },
uRnd3: { value: new THREE.Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)) },
uRnd4: { value: new THREE.Vector3(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)) },
uRnd5: { value: new THREE.Vector3(rnd(0.2, 0.5), rnd(0.3, 0.6), rnd(0.4, 0.7)) },
uColor1: { value: new THREE.Color(conf.cscale(i / conf.nx).hex()) },
uColor2: { value: new THREE.Color(conf.cscale(i / conf.nx).darken(conf.darken).hex()) }
},
vertexShader,
fragmentShader
});
const mesh = new THREE.Mesh(polyline.geometry, material);
scene.add(mesh);
}
}
function initRandomScene() {
conf.nx = Math.floor(rnd(20, 200));
conf.cscale = randomCScale();
conf.darken = rnd(0, 1) > 0.5 ? rnd(-4, -0.5) : rnd(0.5, 4);
conf.angle = rnd(0, 2 * Math.PI);
uTimeCoef.value = rnd(0.05, 0.2);
disposeScene();
initScene();
}
function disposeScene() {
for (let i=0; i<scene.children.length; i++) {
const mesh = scene.children[i];
scene.remove(mesh);
mesh.geometry.dispose();
mesh.material.dispose();
}
scene.dispose();
}
function randomCScale() {
const colors = [], n = 2 + Math.floor(rnd(0, 4));
for (let i = 0; i < n; i++) {
colors.push(chroma.random());
}
return chroma.scale(colors).mode('lch');
}
function animate(t) {
uTime.value = t * 0.001;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
function updateSize() {
width = window.innerWidth;
height = window.innerHeight;
renderer.setSize(width, height);
}
}
// adapted from https://github.com/oframe/ogl/blob/master/src/extras/Polyline.js
const Polyline = (function () {
const tmp = new THREE.Vector3();
class Polyline {
constructor(params) {
const { points } = params;
this.points = points;
this.count = points.length;
this.init();
this.updateGeometry();
}
init() {
this.geometry = new THREE.BufferGeometry();
this.position = new Float32Array(this.count * 3 * 2);
this.prev = new Float32Array(this.count * 3 * 2);
this.next = new Float32Array(this.count * 3 * 2);
const side = new Float32Array(this.count * 1 * 2);
const uv = new Float32Array(this.count * 2 * 2);
const index = new Uint16Array((this.count - 1) * 3 * 2);
for (let i = 0; i < this.count; i++) {
const i2 = i * 2;
side.set([-1, 1], i2);
const v = i / (this.count - 1);
uv.set([0, v, 1, v], i * 4);
if (i === this.count - 1) continue;
index.set([i2 + 0, i2 + 1, i2 + 2], (i2 + 0) * 3);
index.set([i2 + 2, i2 + 1, i2 + 3], (i2 + 1) * 3);
}
this.geometry.setAttribute('position', new THREE.BufferAttribute(this.position, 3));
this.geometry.setAttribute('prev', new THREE.BufferAttribute(this.prev, 3));
this.geometry.setAttribute('next', new THREE.BufferAttribute(this.next, 3));
this.geometry.setAttribute('side', new THREE.BufferAttribute(side, 1));
this.geometry.setAttribute('uv', new THREE.BufferAttribute(uv, 2));
this.geometry.setIndex(new THREE.BufferAttribute(index, 1));
}
updateGeometry() {
this.points.forEach((p, i) => {
p.toArray(this.position, i * 3 * 2);
p.toArray(this.position, i * 3 * 2 + 3);
if (!i) {
tmp.copy(p).sub(this.points[i + 1]).add(p);
tmp.toArray(this.prev, i * 3 * 2);
tmp.toArray(this.prev, i * 3 * 2 + 3);
} else {
p.toArray(this.next, (i - 1) * 3 * 2);
p.toArray(this.next, (i - 1) * 3 * 2 + 3);
}
if (i === this.points.length - 1) {
tmp.copy(p).sub(this.points[i - 1]).add(p);
tmp.toArray(this.next, i * 3 * 2);
tmp.toArray(this.next, i * 3 * 2 + 3);
} else {
p.toArray(this.prev, (i + 1) * 3 * 2);
p.toArray(this.prev, (i + 1) * 3 * 2 + 3);
}
});
this.geometry.attributes.position.needsUpdate = true;
this.geometry.attributes.prev.needsUpdate = true;
this.geometry.attributes.next.needsUpdate = true;
}
}
return Polyline;
})();
App();
Also see: Tab Triggers