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.
<!--
This pen is a reverse engineered (and slightly modified) version of the pen by Toshiya Marukubo (@toshiya-marukubo):
https://codepen.io/toshiya-marukubo/pen/mdRzyQz
I wanted to see how it worked and I figured I'd share :)
-->
* {
padding: 0;
margin: 0;
overflow: hidden;
}
// This is a self-executing function (note the parenthesis at the end that cause it to run).
(() => {
// This tells the browser to run this code in standards compliant mode rather than "quirks" mode.
'use strict';
// The next three lines create constant (unchangeable) references for a new canvas element, the canvas rendering context, and the diameter value, which determines the size and position of the rectangles that are drawn.
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const diameter = 15;
// These values can't be constants because they need to change whenever the browser is resized or the user moves the mouse.
let width = 0; // Width of the viewport
let height = 0; // Height of the viewport
let halfWidth = 0; // Half the width of the viewport (we're storing this to avoid having to calculate it repeatedly in the loop below).
let frame; // This variable holds the reference to the frame from requestAnimationFrame so we can cancel the frame when the user resizes or changes the orientation of the browser window.
let time = 0; // This is used to store the current position in the animation timeline.
let destination = 1; // This is used to smoothe the animation transition to a new position in the timeline.
// This function will be called initially on startup (below) and whenever the user resizes or reorients the browser window.
const resize = () => {
cancelAnimationFrame(frame); // Cancel the last requested animation frame (this prevents us having multiple animation loops running at the same time).
width = canvas.width = window.innerWidth; // Set the width variable and the canvas width to the width of the viewport.
height = canvas.height = window.innerHeight; // Set the height variable and the canvas height to the height of the viewport.
halfWidth = width / 2; // Store half the width of the viewport to save unnecessary computations in the animation loop.
ctx.globalCompositeOperation = 'lighter'; // This changes the mode of the canvas to blend colors together instead of simply stacking them (so drawing red on top of green produces yellow instead of just red on top of green). Also, this needs to be set after the canvas is resized or it does not work.
loop(); // This starts the animation loop.
};
// This function runs repeatedly to draw each frame of the animation.
const loop = () => {
frame = requestAnimationFrame(loop); // We go ahead and request the next animation frame first so it can easily be cancelled when the user resizes/reorients the browser window.
time += (destination - time) * 0.1; // Here we are adjusting the timeline position by 10% of the difference between the destination and the current timeline position per frame, which gives us a nice easing transition.
ctx.clearRect(0, 0, width, height); // This clears the canvas.
// This loop moves from the top to the bottom of the canvas in increments of the diameter defined above.
for (let i = 0; i < height; i += diameter) {
// This loop moves from the left to halfway across the canvas in increments of the diameter defined above.
for (let j = 0; j < halfWidth; j += diameter) {
// This loop runs for each of the three color channels (red, green, and blue).
for (let channel = 0; channel < 3; channel++) {
if (channel === 0) ctx.fillStyle = '#FF0000'; // Set the drawing color to red.
if (channel === 1) ctx.fillStyle = '#00FF00'; // Set the drawing color to green.
if (channel === 2) ctx.fillStyle = '#0000FF'; // Set the drawing color to blue.
// From this point, I won't pretend to fully understand the math wizardy below, so you'll have to ask @toshiya-marukubo
const index = i * width + j; // Store the current position on the canvas.
ctx.globalAlpha = Math.tan(index * index - time); // This adjusts the translucency of the rectangle that is drawn using location and timeline position.
// This draws a rectangle.
ctx.fillRect(
Math.tan(i * j - Math.sin(index + channel / 100) + time) * j + halfWidth - diameter / 2, // Top-left X coordinate.
i, // Top-left Y coordinate.
Math.tan(index + i / j + time + channel / 100) / 2 * diameter / 2, // Width of the rectangle.
Math.tan(index * index - time) * diameter / 2 // Height of the rectangle.
);
}
}
}
};
// This registers the function below to run when the page has finished loading.
window.onload = () => {
['mousemove', 'touchmove'].forEach(type => { // This runs the next function for each event type in the array.
window.addEventListener(type, event => { // This adds an event listener to run the next three lines whenever there is a mousemove or touchmove event.
event.preventDefault(); // Prevent the default event behavior.
const e = !!event.touches ? event.touches[0] : event; // Determine if this is a touch event or a mouse event (this is called a ternary expression).
destination = (e.pageX / width); // Set the timeline destination to the current mouse X position divided by viewport width (this gives us a number between 0 and 1).
});
});
canvas.style.background = '#000000'; // Sets the canvas background color to black.
document.body.appendChild(canvas); // Attaches the canvas to the document body.
resize(); // Resizes the viewport and starts the animation loop.
window.onresize = resize; // This registers the resize function to run whenever the page is resized.
}
})(); // Those last two parenthesis cause all of the code above to run.
Also see: Tab Triggers