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="two"></div>
html, body {
height: 100%;
background: #111;
overflow: hidden;
}
svg,
canvas {
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #111;
}
// The width and height of the box
// Also used for placing certain things
const W = 300;
const H = 300;
// Using 'box' to mean 'the larger, wrapper shape that contains all of the small moving parts'
// and using 'square' to mean 'one of the smaller moving parts'
// For both of these consts, the integer is the length of one side of that shape
const BOXSIZE = 150;
const SQSIZE = 15;
// lightnesses of grays
const STARTL = 10;
const ENDL = 30;
function randomDec(min, max) {
return Math.random() * (max - min) + min;
}
var elem = document.getElementById('two');
var two = new Two({ width: W, height: H, type: Two.Types.canvas}).appendTo(elem);
// Make the canvas
var box = two.makeRectangle(W/2, H/2, BOXSIZE, BOXSIZE);
box.noStroke();
box.noFill();
// Function to convert relative x/y to absolute x/y
function makeAbs(vertex) {
return {x: vertex.x + box.translation.x, y: vertex.y + box.translation.y}
}
// Convert the corner vertices of the 'canvas' (inner box) into absolute coords
box.tl = makeAbs(box.vertices[0]);
box.tr = makeAbs(box.vertices[1]);
box.br = makeAbs(box.vertices[2]);
box.bl = makeAbs(box.vertices[3]);
function Square() {
// Called the first time a Square object is constructed
this.make = () => {
this.origin = {};
// Find a starting y, making sure the top edge is at least the top y and that the bottom edge is at most the bottom y
this.origin.y = randomDec(box.tl.y + (SQSIZE/2), box.bl.y - (SQSIZE/2));
this.origin.x = Math.floor(randomDec(box.tl.x - (SQSIZE/2) - BOXSIZE, box.tl.x - (SQSIZE/2)));
// Progress counter will be in integers -- for each .25, .5, or .75
// movement, we'll be adding 1, 2, or 3 to this, to help figure out
// which gradient to apply
this.progress = 0;
// velocity is pretty literally amount the square travels to the right
this.velocity = [.25, .5, .75][Math.floor(Math.random() * 3)];
// Make the actual square shape, make it invisible for now
this.s = two.makeRectangle(this.origin.x, this.origin.y, SQSIZE, SQSIZE);
this.s.noFill();
this.s.noStroke();
};
// When the square hits the right edge, reuse it
this.reset = () => {
// Move it back to the starting x, make it invisible
this.s.translation.x = this.origin.x;
this.s.noFill();
// Give it a new velocity
this.velocity = [.25, .5, .75][Math.floor(Math.random() * 3)];
// Reset the progress ticker
this.progress = 0;
if (this.completed) {
this.completed = 0;
}
}
// run this.make when the object is created
(() => {
this.make();
})();
}
// Make all stops (gradient start/ends) ahead of time
var numStops = BOXSIZE * 4 + 1;
var zeroStops = [];
var oneStops = [];
for (var i = 0; i < numStops; i++) {
let lightness = i / (numStops - 1) * (ENDL - STARTL);
// Two.Stop params: (offset, color)
zeroStops.push(new Two.Stop(0, 'hsl(0, 0%, '+ lightness +'%)'));
oneStops.push(new Two.Stop(1, 'hsl(0, 0%, '+ lightness +'%)'));
}
// Make all gradients ahead of time, using the stops above
var grads = [];
for (var i = 0; i < numStops - 1; i++) {
// Two.LinearGradient params: (x1, y1, x2, y2, [Two.Stops])
// x1/y1/x2/y2 are all offset from the center of the element that gets the gradient
// so: start at the middle of the left edge, end at the midle of the right edge
let grad = new Two.LinearGradient(-SQSIZE/2, 0, SQSIZE/2, 0, [zeroStops[i], oneStops[i+1]]);
grads.push(grad);
}
// Make all squares ahead of time
var sqs = [];
for (var i = 0; i < 500; i++) {
sqs.push(new Square());
}
// The animation loop
two.bind('update', function(frameCount, timeDelta) {
for (var i = 0, len = sqs.length; i < len; i++) {
// Move every square by its velocity
sqs[i].s.translation.x += sqs[i].velocity;
}
// Determine the completion amount and paint the square
for (var i = 0, len = sqs.length; i < len; i++) {
// ... but only if the square has gotten to the wrappers' left edge!
if (sqs[i].s.translation.x - (SQSIZE/2) >= box.tl.x) {
// Increment progress ticker by 1, 2, or 3,
// depending on if velocity was .25, .5, or .75
sqs[i].progress += (sqs[i].velocity / .25);
// Determine the fill
sqs[i].s.fill = grads[sqs[i].progress];
sqs[i].s.noStroke();
}
}
// See if we should reset the square
for (var i = 0, len = sqs.length; i < len; i++) {
// (i.e., if the right edge of the individual square > the right edge of the wrapping box)
if (sqs[i].s.translation.x + (SQSIZE/2) > box.tr.x) {
sqs[i].reset();
}
}
}).play();
Also see: Tab Triggers