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="view"></canvas>
<aside style="display:none">
<img id="star-texture" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/stars-02.png?v=1">
</aside>
<div id="quick-settings"></div>
body {
background: #111;
touch-action: none;
}
#view {
position: fixed;
top: 0;
left: 0;
visibility: hidden;
}
#quick-settings {
position: fixed;
top: 5px;
right: 170px;
right: 205px;
}
console.clear();
const log = console.log.bind(console);
const TAU = Math.PI * 2;
//
// PARTICLE
// ===========================================================================
class Particle {
alive = false;
constructor(public texture, public frame) {
this.width = frame.width;
this.height = frame.height;
this.originX = frame.width / 2;
this.originY = frame.height / 2;
}
init(x = 0, y = 0) {
const angle = random(TAU);
const force = random(2, 6);
this.x = x;
this.y = y;
this.alpha = 1;
this.alive = true;
this.theta = angle;
this.vx = Math.sin(angle) * force;
this.vy = Math.cos(angle) * force;
this.rotation = Math.atan2(this.vy, this.vx);
this.drag = random(0.82, 0.97);
this.scale = random(0.1, 1);
this.wander = random(0.5, 1.0);
this.matrix = { a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0 };
return this;
}
update() {
const matrix = this.matrix;
this.x += this.vx;
this.y += this.vy;
this.vx *= this.drag;
this.vy *= this.drag;
this.theta += random(-0.5, 0.5) * this.wander;
this.vx += Math.sin(this.theta) * 0.1;
this.vy += Math.cos(this.theta) * 0.1;
this.rotation = Math.atan2(this.vy, this.vx);
this.alpha *= 0.98;
this.scale *= 0.985;
this.alive = this.scale > 0.06 && this.alpha > 0.06;
const cos = Math.cos(this.rotation) * this.scale;
const sin = Math.sin(this.rotation) * this.scale;
matrix.a = cos;
matrix.b = sin;
matrix.c = -sin;
matrix.d = cos;
matrix.tx = this.x - ((this.originX * matrix.a) + (this.originY * matrix.c));
matrix.ty = this.y - ((this.originX * matrix.b) + (this.originY * matrix.d));
return this;
}
draw(context) {
const m = this.matrix;
const f = this.frame;
context.globalAlpha = this.alpha;
context.setTransform(m.a, m.b, m.c, m.d, m.tx, m.ty);
context.drawImage(this.texture, f.x, f.y, f.width, f.height, 0, 0, this.width, this.height);
return this;
}
}
//
// APP
// ===========================================================================
class App {
pool = [];
particles = [];
pointer = {
x: -9999,
y: -9999
};
buffer = document.createElement("canvas");
bufferContext = this.buffer.getContext("2d");
supportsFilters = (typeof this.bufferContext.filter !== "undefined");
constructor(options) {
Object.assign(this, options);
this.context = this.view.getContext("2d", { alpha: false });
}
pointerMove = event => {
event.preventDefault();
const pointer = event.targetTouches ? event.targetTouches[0] : event;
this.pointer.x = pointer.clientX;
this.pointer.y = pointer.clientY;
for (let i = 0; i < random(2, 7); i++) {
this.spawn(this.pointer.x, this.pointer.y);
}
}
resize = event => {
this.width = this.buffer.width = this.view.width = window.innerWidth;
this.height = this.buffer.height = this.view.height = window.innerHeight;
}
render = time => {
const context = this.context;
const particles = this.particles;
const bufferContext = this.bufferContext;
context.fillStyle = this.backgroundColor;
context.fillRect(0, 0, this.width, this.height);
bufferContext.globalAlpha = 1;
bufferContext.setTransform(1,0,0,1,0,0);
bufferContext.clearRect(0, 0, this.width, this.height);
bufferContext.globalCompositeOperation = this.blendMode;
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
if (particle.alive) {
particle.update();
} else {
this.pool.push(particle);
removeItems(particles, i, 1);
}
}
for (let particle of particles) {
particle.draw(bufferContext);
}
if (this.supportsFilters) {
if (this.useBlurFilter) {
context.filter = `blur(${this.filterBlur}px)`;
}
context.drawImage(this.buffer, 0, 0);
if (this.useContrastFilter) {
context.filter = `drop-shadow(4px 4px 4px rgba(0,0,0,1)) contrast(${this.filterContrast}%)`;
}
}
context.drawImage(this.buffer, 0, 0);
context.filter = "none";
quickSettings.setValue("Particles", `
Active = ${this.particles.length}<br>
Cached = ${this.pool.length}`
);
requestAnimationFrame(this.render);
}
spawn(x, y) {
let particle;
if (this.particles.length > this.maxParticles) {
particle = this.particles.shift();
} else if (this.pool.length) {
particle = this.pool.pop();
} else {
particle = new Particle(this.texture, sample(this.frames));
}
particle.init(x, y);
this.particles.push(particle);
return this;
}
start() {
this.resize();
this.render();
this.view.style.visibility = "visible";
if (window.PointerEvent) {
window.addEventListener("pointermove", this.pointerMove);
} else {
window.addEventListener("mousemove", this.pointerMove);
window.addEventListener("touchmove", this.pointerMove);
}
window.addEventListener("resize", this.resize);
requestAnimationFrame(this.render);
return this;
}
}
//
// CREATE FRAMES
// ===========================================================================
function createFrames(numFrames, width, height) {
const frames = [];
for (let i = 0; i < numFrames; i++) {
frames.push({
x: width * i,
y: 0,
width: width,
height: height
});
}
return frames;
}
//
// REMOVE ITEMS
// ===========================================================================
function removeItems(array, startIndex, removeCount) {
const length = array.length;
if (startIndex >= length || removeCount === 0) {
return
}
removeCount = (startIndex + removeCount > length ? length - startIndex : removeCount)
const len = length - removeCount
for (let i = startIndex; i < len; ++i) {
array[i] = array[i + removeCount]
}
array.length = len
}
//
// RANDOM
// ===========================================================================
function random(min, max) {
if (max == null) { max = min; min = 0; }
if (min > max) { var tmp = min; min = max; max = tmp; }
return min + (max - min) * Math.random();
}
function sample(array) {
return array[(Math.random() * array.length)|0];
}
//
// QUICK SETTINGS
// ===========================================================================
const app = new App({
view: document.querySelector("#view"),
texture: document.querySelector("#star-texture"),
frames: createFrames(5, 80, 80),
maxParticles: 2000,
backgroundColor: "#111111",
blendMode: "lighter",
filterBlur: 50,
filterContrast: 300,
useBlurFilter: true,
useContrastFilter: true
});
const blendModes = [
{ label: "Source Over", value: "source-over" },
{ label: "Source In", value: "source-in" },
{ label: "Source Out", value: "source-out" },
{ label: "Source Atop", value: "source-atop" },
{ label: "Destination Over", value: "destination-over" },
{ label: "Destination In", value: "destination-in" },
{ label: "Destination Out", value: "destination-out" },
{ label: "Destination Atop", value: "destination-atop" },
{ label: "Lighter", value: "lighter" },
{ label: "Copy", value: "copy" },
{ label: "Xor", value: "xor" },
{ label: "Multiply", value: "multiply" },
{ label: "Screen", value: "screen" },
{ label: "Overlay", value: "overlay" },
{ label: "Darken", value: "darken" },
{ label: "Lighten", value: "lighten" },
{ label: "Color Dodge", value: "color-dodge" },
{ label: "Color Burn", value: "color-burn" },
{ label: "Hard Light", value: "hard-light" },
{ label: "Soft Light", value: "soft-light" },
{ label: "Difference", value: "difference" },
{ label: "Exclusion", value: "exclusion" },
{ label: "Hue", value: "hue" },
{ label: "Saturation", value: "saturation" },
{ label: "Color", value: "color" },
{ label: "Luminosity", value: "luminosity" },
];
const blendModeNames = blendModes.map(blendMode => blendMode.label);
const blendModeIndex = blendModes.findIndex(blendMode => blendMode.value === app.blendMode);
const container = document.querySelector("#quick-settings");
const quickSettings = QuickSettings.create(0, 0, "Settings", container)
.addHTML("Support", `Supports Filters: ${app.supportsFilters}`)
.hideTitle("Support")
.addHTML("Particles", "")
.addBoolean("Blur Filter", app.useBlurFilter, value => {
app.useBlurFilter = value;
if (value) {
quickSettings.showControl("Blur Radius")
} else {
quickSettings.hideControl("Blur Radius")
}
})
.addRange("Blur Radius", 0, 200, app.filterBlur, 1, value => app.filterBlur = value)
.addBoolean("Contrast Filter", app.useContrastFilter, value => {
app.useContrastFilter = value;
if (value) {
quickSettings.showControl("Contrast")
} else {
quickSettings.hideControl("Contrast")
}
})
.addRange("Contrast", 0, 400, app.filterContrast, 1, value => app.filterContrast = value)
.addDropDown("Blend Mode", blendModeNames, item => app.blendMode = blendModes[item.index].value)
.addColor("Background", app.backgroundColor, color => app.backgroundColor = color)
.setValue("Blend Mode", blendModeIndex || 0)
window.addEventListener("load", app.start());
window.focus();
log("APP", app);
Also see: Tab Triggers