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="scene"></canvas>
body {
margin: 0;
overflow: hidden;
}
canvas {
width:100vw;
height:100vh;
}
console.clear();
// Get the canvas element from the DOM
const canvas = document.getElementById('scene');
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
// Store the 2D context
const ctx = canvas.getContext('2d');
if (window.devicePixelRatio > 1) {
canvas.width = canvas.clientWidth * 2;
canvas.height = canvas.clientHeight * 2;
ctx.scale(2, 2);
}
function getTexture(emoji) {
const tempCanvas = document.createElement("canvas");
const tempCtx = tempCanvas.getContext("2d");
tempCanvas.width = 60;
tempCanvas.height = 60;
tempCtx.textAlign = 'center';
tempCtx.textBaseline = 'middle';
tempCtx.font = '54px serif';
tempCtx.fillText(emoji, 30, 35);
return tempCanvas;
}
const textures = [getTexture('🦊'), getTexture('🦓'), getTexture('🐹'), getTexture('🐨')];
/* ====================== */
/* ====== VARIABLES ===== */
/* ====================== */
let width = canvas.offsetWidth; // Width of the canvas
let height = canvas.offsetHeight; // Height of the canvas
const dots = []; // Every dots in an array
/* ====================== */
/* ====== CONSTANTS ===== */
/* ====================== */
/* Some of those constants may change if the user resizes their screen but I still strongly believe they belong to the Constants part of the variables */
let DOTS_AMOUNT = Math.min(width, height); // Amount of dots on the screen
const DOT_RADIUS = 20; // Radius of the dots
let PROJECTION_CENTER_X = width / 2; // X center of the canvas HTML
let PROJECTION_CENTER_Y = height / 2; // Y center of the canvas HTML
let PERSPECTIVE = width * 0.8;
let GLOBE_RADIUS = Math.min(width, height) * 0.4;
class Dot {
constructor() {
this.theta = Math.random() * 2 * Math.PI; // Random value between [0, 2Pi]
this.phi = Math.acos((Math.random() * 2) - 1); // Random value between [0, Pi]
this.texture = textures[Math.floor(Math.random() * textures.length)];
// Calculate the [x, y, z] coordinates of the dot along the globe
this.x = 0;
this.y = 0;
this.z = 0;
this.radius = Math.random() * (GLOBE_RADIUS * 0.2) + (GLOBE_RADIUS * 0.8);
this.xProjected = 0;
this.yProjected = 0;
this.scaleProjected = 0;
TweenMax.to(this, 40, {
theta: this.theta + Math.PI * 2,
repeat: -1,
ease: Power0.easeNone
});
}
// Do some math to project the 3D position into the 2D canvas
project() {
this.x = this.radius * Math.sin(this.phi) * Math.cos(this.theta);
this.y = this.radius * Math.cos(this.phi);
this.z = this.radius * Math.sin(this.phi) * Math.sin(this.theta) + this.radius;
this.scaleProjected = PERSPECTIVE / (PERSPECTIVE + this.z);
this.xProjected = (this.x * this.scaleProjected) + PROJECTION_CENTER_X;
this.yProjected = (this.y * this.scaleProjected) + PROJECTION_CENTER_Y;
}
// Draw the dot on the canvas
draw() {
ctx.drawImage(this.texture, this.xProjected - DOT_RADIUS, this.yProjected - DOT_RADIUS, DOT_RADIUS * 2 * this.scaleProjected, DOT_RADIUS * 2 * this.scaleProjected)
}
}
function createDots() {
// Empty the array of dots
dots.length = 0;
// Create a new dot based on the amount needed
for (let i = 0; i < DOTS_AMOUNT; i++) {
// Create a new dot and push it into the array
dots.push(new Dot());
}
}
/* ====================== */
/* ======== RENDER ====== */
/* ====================== */
function render() {
// Clear the scene
ctx.clearRect(0, 0, width, height);
// Loop through the dots array and project every dot
for (var i = 0; i < dots.length; i++) {
dots[i].project();
}
// Sort dots array based on their projected size
dots.sort((dot1, dot2) => {
return dot1.scaleProjected - dot2.scaleProjected;
});
// Loop through the dots array and draw every dot
for (var i = 0; i < dots.length; i++) {
dots[i].draw();
}
window.requestAnimationFrame(render);
}
// Function called after the user resized its screen
function afterResize () {
width = canvas.offsetWidth;
height = canvas.offsetHeight;
if (window.devicePixelRatio > 1) {
canvas.width = canvas.clientWidth * 2;
canvas.height = canvas.clientHeight * 2;
ctx.scale(2, 2);
} else {
canvas.width = width;
canvas.height = height;
}
PROJECTION_CENTER_X = width / 2;
PROJECTION_CENTER_Y = height / 2;
PERSPECTIVE = width * 0.8;
GLOBE_RADIUS = Math.min(width, height) * 0.4;
DOTS_AMOUNT = Math.min(width, height);
createDots(); // Reset all dots
}
// Variable used to store a timeout when user resized its screen
let resizeTimeout;
// Function called right after user resized its screen
function onResize () {
// Clear the timeout variable
resizeTimeout = window.clearTimeout(resizeTimeout);
// Store a new timeout to avoid calling afterResize for every resize event
resizeTimeout = window.setTimeout(afterResize, 500);
}
window.addEventListener('resize', onResize);
// Populate the dots array with random dots
createDots();
// Render the scene
window.requestAnimationFrame(render);
Also see: Tab Triggers