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 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.
body { margin: 0; padding: 0;background: black; overflow: hidden;}
/*
TODO
sine / morph in y positions ?
horizontal movement euler + quarternion rotation around in between axis ?
*/
var isMouseDown = false;
var emptySlot = "emptySlot", planeTop = "planeTop", planeBottom = "planeBottom";
var camera, scene, renderer;
var mouse = {x: 0, y: 0};
var camPos = {x: 0, y: 0, z: 10};
var sw = window.innerWidth, sh = window.innerHeight;
var cols = 20;
var rows = 16;
var gap = 20;
var size = {
width: 100,
height: 30,
depth: 150,
}
var planeOffset = 250;
var allRowsDepth = rows * (size.depth + gap);
var allColsWidth = cols * (size.depth + gap);
var speedNormal = 4;
var speedFast = 34;
var speed = speedNormal;
var boxes = {
planeBottom: [],
planeTop: []
};
var boxes1d = [];
function num(min, max) { return Math.random() * (max - min) + min; }
function draw(props) {
var colours = {
slow: {
r: num(0, 0.2),
g: num(0.5, 0.9),
b: num(0.3, 0.7)
},
fast: {
r: num(0.9, 1.0),
g: num(0.1, 0.7),
b: num(0.2, 0.5)
}
}
var uniforms = {
r: { type: "f", value: colours.slow.r},
g: { type: "f", value: colours.slow.g},
b: { type: "f", value: colours.slow.b},
distanceX: { type: "f", value: 1.0},
distanceZ: { type: "f", value: 1.0},
pulse: { type: "f", value: 0},
speed: { type: "f", value: speed},
};
var material = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: vertexShader,
fragmentShader: fragmentShader
});
var geometry = new THREE.BoxGeometry(props.width, props.height, props.depth);
var object = new THREE.Mesh(geometry, material);
object.colours = colours;
return object;
}
function init() {
scene = new THREE.Scene();
// fog doesn't work for shaders - custom solution with distance calculations instead
// scene.fog = new THREE.FogExp2(0x002000, 0.001);
camera = new THREE.PerspectiveCamera( 100, sw / sh, 1, 10000 );
scene.add( camera );
// lights don't work either - out of the box anyway, not sure how to feed into shader
// var lightAbove = new THREE.DirectionalLight(0xff8080, 2);
// lightAbove.position.set(0, 1, 0.25).normalize();
// scene.add( lightAbove );
renderer = new THREE.WebGLRenderer();
renderer.setSize( sw, sh );
// renderer.setClearColor( scene.fog.color );
for (var j = 0, jl = rows; j < jl; j++) {
boxes.planeBottom[j] = [];
boxes.planeTop[j] = [];
for (var i = 0, il = cols; i < il; i++) {
boxes.planeBottom[j][i] = emptySlot;
boxes.planeTop[j][i] = emptySlot;
};
};
function createBox() {
var xi = Math.floor(Math.random() * cols), xai = xi;
var yi = Math.random() > 0.5 ? 1 : -1, yai = yi === -1 ? planeBottom : planeTop;
var zi = Math.floor(Math.random() * rows), zai = zi;
var x = (xi - cols / 2) * (size.width + gap);
var y = yi * planeOffset;
var z = zi * (size.depth + gap);
if (boxes[yai][zai][xai] === emptySlot) {
var box = draw(size);
box.position.y = y;
box.isWarping = false;
box.offset = {x: x, z: 0};
box.posZ = z;
boxes[yai][zai][xai] = box;
boxes1d.push(box);
scene.add(box);
}
}
for (var i = 0, il = rows * cols; i < il; i++) {
createBox();
};
document.body.appendChild(renderer.domElement);
function listen(eventNames, callback) {
for (var i = 0; i < eventNames.length; i++) {
window.addEventListener(eventNames[i], callback);
}
}
listen(["resize"], function(e){
sw = window.innerWidth;
sh = window.innerHeight
camera.aspect = sw / sh;
camera.updateProjectionMatrix();
renderer.setSize(sw, sh);
});
listen(["mousedown", "touchstart"], function(e) {
e.preventDefault();
isMouseDown = true;
});
listen(["mousemove", "touchmove"], function(e) {
e.preventDefault();
if (e.changedTouches && e.changedTouches[0]) e = e.changedTouches[0];
mouse.x = (e.clientX / sw) * 2 - 1;
mouse.y = -(e.clientY / sh) * 2 + 1;
});
listen(["mouseup", "touchend"], function(e) {
e.preventDefault();
isMouseDown = false;
});
render(0);
}
function move(x, y, z) {
var box = boxes[y][z][x];
if (box !== emptySlot) {
box.position.x = box.offset.x;
box.position.z = box.offset.z + box.posZ;
if (box.position.z > 0) {
box.posZ -= allRowsDepth;
}
// return;
// if (isMouseDown) return;
if (!box.isWarping && Math.random() > 0.999) {
var dir = Math.floor(Math.random() * 5), xn = x, zn = z, yn = y, yi = 0, xo = 0, zo = 0;
switch (dir) {
case 0 : xn++; xo = 1; break;
case 1 : xn--; xo = -1; break;
case 2 : zn++; zo = 1; break;
case 3 : zn--; zo = -1; break;
case 4 :
yn = (y === planeTop) ? planeBottom : planeTop;
yi = (y === planeTop) ? -1 : 1;
break;
}
if (boxes[yn][zn] && boxes[yn][zn][xn] === emptySlot) {
boxes[y][z][x] = emptySlot;
box.isWarping = true;
boxes[yn][zn][xn] = box;
// con.log( box.offset.x, box.offset.z);
if (dir === 4) { // slide vertically
TweenMax.to(box.position, 0.5, {
y: yi * planeOffset
});
} else { // slide horizontally
TweenMax.to(box.offset, 0.5, {
x: box.offset.x + xo * (size.width + gap),
z: box.offset.z + zo * (size.depth + gap),
});
}
TweenMax.to(box.offset, 0.6, {
onComplete: function() {
box.isWarping = false;
}
});
}
}
}
}
function render(time) {
speed -= (speed - (isMouseDown ? speedFast : speedNormal)) * 0.05;
var box;
for (var b = 0, bl = boxes1d.length; b < bl; b++) {
box = boxes1d[b];
box.posZ += speed;
// normalized z distance from camera
var distanceZ = 1 - ((allRowsDepth - box.posZ) / (allRowsDepth) - 1);
box.material.uniforms.distanceZ.value = distanceZ;
// normalized x distance from camera (centre)
var distanceX = 1 - (Math.abs(box.position.x)) / (allColsWidth / 3);
box.material.uniforms.distanceX.value = distanceX;
var colour = isMouseDown ? box.colours.fast : box.colours.slow;
box.material.uniforms.r.value -= (box.material.uniforms.r.value - colour.r) * 0.1;
box.material.uniforms.g.value -= (box.material.uniforms.g.value - colour.g) * 0.1;
box.material.uniforms.b.value -= (box.material.uniforms.b.value - colour.b) * 0.1;
// normalized speed
var currentSpeed = (speed - speedNormal) / (speedFast - speedNormal)
box.material.uniforms.speed.value = currentSpeed;
// pulses more with more speed... of course!
if (Math.random() > (0.99995 - currentSpeed * 0.005)) {
box.material.uniforms.pulse.value = 1;
}
box.material.uniforms.pulse.value -= box.material.uniforms.pulse.value * 0.1 / (currentSpeed + 1);
// if (b ==13) con.log(box.material.uniforms.speed.value);
}
for (var j = 0, jl = rows; j < jl; j++) { // iterate through rows: z
for (var i = 0, il = cols; i < il; i++) { // iterate throw cols: x
move(i, planeBottom, j);
move(i, planeTop, j);
};
};
camPos.x -= (camPos.x - mouse.x * 400) * 0.02;
camPos.y -= (camPos.y - mouse.y * 150) * 0.05;
camPos.z = -100;
camera.position.set(camPos.x, camPos.y, camPos.z);
// camera.lookAt( scene.position );
// camera.rotation.z = time * 0.0001;
camera.rotation.y = camPos.x / -1000;
camera.rotation.x = camPos.y / 1000;
// camera.rotation.z = camPos.x / -2000;
camera.rotation.z = (camPos.x - mouse.x * 400) / 2000;
renderer.render( scene, camera );
// if (time < 800)
requestAnimationFrame( render );
}
var vertexShader = [
"varying vec2 vUv;",
"void main()",
"{",
" vUv = uv;",
" vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
" gl_Position = projectionMatrix * mvPosition;",
"}"].join("");
var fragmentShader = [
"uniform float r;",
"uniform float g;",
"uniform float b;",
"uniform float distanceZ;",
"uniform float distanceX;",
"uniform float pulse;",
"uniform float speed;",
"varying vec2 vUv;",
// "float checkerRows = 8.0;",
// "float checkerCols = 16.0;",
"void main( void ) {",
" vec2 position = abs(-1.0 + 2.0 * vUv);",
" float edging = abs((pow(position.y, 5.0) + pow(position.x, 5.0)) / 2.0);",
" float perc = (0.2 * pow(speed + 1.0, 2.0) + edging * 0.8) * distanceZ * distanceX;",
// " float perc = distanceX * distanceZ;",
// " vec2 checkPosition = vUv;",
// " float checkerX = ceil(mod(checkPosition.x, 1.0 / checkerCols) - 1.0 / checkerCols / 2.0);",
// " float checkerY = ceil(mod(checkPosition.y, 1.0 / checkerRows) - 1.0 / checkerRows / 2.0);",
// " float checker = ceil(checkerX * checkerY);",
// " float r = checker;",
// " float g = checker;",
// " float b = checker;",
// " float perc = 1.0;",
" float red = r * perc + pulse;",
" float green = g * perc + pulse;",
" float blue = b * perc + pulse;",
" gl_FragColor = vec4(red, green, blue, 1.0);",
"}"].join("");
//console.log(THREE, TweenMax, planeTop, planeBottom);
init();
Also see: Tab Triggers