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.space
canvas.planet
canvas.sun
body
overflow: hidden
cursor: none
.space, .planet, .sun
position: absolute
transform: translate(-50%, -50%)
.space, .planet
top: 50%
left: 50%
.planet, .sun
border-radius: 50%
// beware: this code is smelly
// started out as a much simpler sketch, followed by liberal cutting and pasting
//
// welp, enjoy!
// configurable
const PLANET_SIZE = 400;
const SUN_SIZE = 100;
const SUN_FACTOR = 0.1;
const PARALLAX_FACTOR = 1.25;
const DENSITY = 1000;
const LIGHT_COLORS = [
[229, 225, 217],
[0, 132, 173],
[30, 32, 31],
[255, 0, 0],
[0, 33, 252],
];
const BG_COLORS = [
[33, 28, 27],
[207, 203, 194],
[232, 225, 210],
[0, 33, 252],
[255, 0, 0],
];
// figure out how big the sun should be
// Math aliases
const abs = Math.abs;
const cos = Math.cos;
const ln = Math.log;
const PI2 = Math.PI * 2;
const sin = Math.sin;
const sqrt = Math.sqrt;
// canvas setup
const space = document.querySelector('.space');
const spaceCtx = space.getContext('2d');
space.style.height = space.style.width = 100 * PARALLAX_FACTOR + '%';
const planet = document.querySelector('.planet');
const planetCtx = planet.getContext('2d');
planet.height = planet.width = PLANET_SIZE;
planet.style.height = planet.style.width = PLANET_SIZE / 2 + 'px';
const sun = document.querySelector('.sun');
const sunCtx = sun.getContext('2d');
sun.height = sun.width = SUN_SIZE;
sun.style.height = sun.style.width = SUN_SIZE / 2 + 'px';
// globals:
// keep track of the window size
let width, height;
// index for color pairs
let color = 0;
// x & y center for the planet's incedent light
let shadowX = 0, shadowY = 0;
// create a new typedArray for x and y of each of the 3 elements
const bufferLength = Uint16Array.BYTES_PER_ELEMENT * DENSITY
const spaceBufferX = new ArrayBuffer(bufferLength);
const spaceValsX = new Uint16Array(spaceBufferX);
const spaceBufferY = new ArrayBuffer(bufferLength);
const spaceValsY = new Uint16Array(spaceBufferY);
const planetBufferX = new ArrayBuffer(bufferLength);
const planetValsX = new Uint16Array(planetBufferX);
const planetBufferY = new ArrayBuffer(bufferLength);
const planetValsY = new Uint16Array(planetBufferY);
const sunBufferX = new ArrayBuffer(bufferLength * SUN_FACTOR);
const sunValsX = new Uint16Array(sunBufferX);
const sunBufferY = new ArrayBuffer(bufferLength * SUN_FACTOR);
const sunValsY = new Uint16Array(sunBufferY);
function draw () {
spaceCtx.fillStyle = planetCtx.fillStyle = `rgba(${BG_COLORS[color]}, 0.34)`;
spaceCtx.fillRect(0, 0, width * 2, height * 2);
planetCtx.fillRect(0, 0, PLANET_SIZE, PLANET_SIZE);
sunCtx.clearRect(0, 0, SUN_SIZE, SUN_SIZE);
spaceCtx.fillStyle = planetCtx.fillStyle = sunCtx.fillStyle = `rgb(${LIGHT_COLORS[color]})`;
for (let i = DENSITY; i--;) {
let u1 = Math.random();
let u2 = Math.random();
let n1 = sqrt(-2 * ln(u1)) * sin(PI2 * u2);
let n2 = sqrt(-2 * ln(u1)) * cos(PI2 * u2);
// uniformly distributed
spaceValsX[i] = u1 * width * 2;
spaceValsY[i] = u2 * height * 2;
// normally distributed using Box-Muller transform
planetValsX[i] = shadowX + PLANET_SIZE / 4 * n1;
planetValsY[i] = shadowY + PLANET_SIZE / 4 * n2;
if (i < DENSITY * SUN_FACTOR) {
// normally distributed around the sun's center
sunValsX[i] = SUN_SIZE / 4 * (2 + n1);
sunValsY[i] = SUN_SIZE / 4 * (2 + n2);
}
}
// draw space
for (let i = DENSITY; i--;) {
let x = spaceValsX[i], y = spaceValsY[i];
spaceCtx.fillRect(x, y, 2, 2);
}
// draw planet
for (let i = DENSITY; i--;) {
let x = planetValsX[i], y = planetValsY[i];
if (x > 0 && x < PLANET_SIZE && y > 0 && y < PLANET_SIZE) {
planetCtx.fillRect(x, y, 2, 2);
}
}
// draw sun
for (let i = DENSITY * SUN_FACTOR; i--;) {
let x = sunValsX[i], y = sunValsY[i];
if (x > 0 && x < SUN_SIZE && y > 0 && y < SUN_SIZE) {
sunCtx.fillRect(x, y, 2, 2);
}
}
requestAnimationFrame(draw);
}
window.addEventListener('mousemove', mousemove);
window.addEventListener('resize', resize);
window.addEventListener('click', function () {
color = (color + 1) % BG_COLORS.length;
}, false);
function mousemove (e) {
let x = e.clientX;
let y = e.clientY;
let px = x / width;
let py = y / height;
space.style.left = 50 + (1 - px * 2) * PARALLAX_FACTOR * 10 + '%';
space.style.top = 50 + (1 - py * 2) * PARALLAX_FACTOR * 10 + '%';
shadowX = PLANET_SIZE * px;
shadowY = PLANET_SIZE * py;
sun.style.top = y + 'px';
sun.style.left = x + 'px';
}
function resize () {
width = window.innerWidth * PARALLAX_FACTOR;
height = window.innerHeight * PARALLAX_FACTOR;
space.height = height * 2;
space.width = width * 2;
}
// set height and width
resize();
// start the animation loop
draw();
Also see: Tab Triggers