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="canvas">Your browser doesn't support canvas</canvas>
<!-- SVG Goo filter from: https://css-tricks.com/gooey-effect/ -->
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="shadowed-goo">
<feGaussianBlur in="SourceGraphic" result="blur" stdDeviation="10" />
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 18 -7" result="goo" />
<feGaussianBlur in="goo" stdDeviation="3" result="shadow" />
<feColorMatrix in="shadow" mode="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -0.2" result="shadow" />
<feOffset in="shadow" dx="1" dy="1" result="shadow" />
<feBlend in2="shadow" in="goo" result="goo" />
<feBlend in2="goo" in="SourceGraphic" result="mix" />
</filter>
<filter id="goo">
<feGaussianBlur in="SourceGraphic" result="blur" stdDeviation="10" />
<feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 18 -7" result="goo" />
<feBlend in2="goo" in="SourceGraphic" result="mix" />
</filter>
</defs>
</svg>
html, body{
margin:0;
padding:0;
background-color:hsl(195, 100%, 7%);
}
#canvas{
margin:0 auto;
display:block;
filter:url('#shadowed-goo');
}
svg{
display:none;
}
/**
* Constants
*/
const TWO_PI = Math.PI * 2;
/**
* Application Class
*/
class Application {
/**
* Application constructor
*/
constructor() {
this.canvas = document.getElementById("canvas");
this.context = this.canvas.getContext("2d");
this.width = this.canvas.width = window.innerWidth;
this.height = this.canvas.height = window.innerHeight;
this.center = {
x: this.width / 2,
y: this.height / 2
};
this.circleContainers = [];
//Resize listener for the canvas to fill browser window dynamically
window.addEventListener('resize', () => this.resizeCanvas(), false);
}
/**
* Simple resize function. Reinitializes everything on the canvas while changing the width/height
*/
resizeCanvas() {
this.width = this.canvas.width = window.innerWidth;
this.height = this.canvas.height = window.innerHeight;
this.center = {
x: this.width / 2,
y: this.height / 2
};
//Empty the previous container and fill it again with new CircleContainer objects
this.circleContainers = [];
this.initializeCircleContainers();
}
/**
* Create a number of CircleContainer objects based on the numberOfContainers variable
* @return void
*/
initializeCircleContainers() {
for (let x = 0; x < this.width + 100; x += 100) {
for (let y = 0; y < this.height + 100; y += 100) {
//Initialize a new instance of the CircleContainer class
let circleContainer = new CircleContainer(this.context, x, y);
//Let the CircleContainer initialize it's children
circleContainer.initializeCircles();
//Add the container to our array of CircleContainer objects
this.circleContainers.push(circleContainer);
}
}
}
/**
* Updates the application and every child of the application
* @return void
*/
update() {
for (let i = 0; i < this.circleContainers.length; i++) {
this.circleContainers[i].update();
}
}
/**
* Renders the application and every child of the application
* @return void
*/
render() {
//Clear the entire canvas every render
this.context.clearRect(0, 0, this.width, this.height);
//Trigger the render function on every child element
for (let i = 0; i < this.circleContainers.length; i++) {
this.circleContainers[i].render();
}
}
/**
* Update and render the application at least 60 times a second
* @return void
*/
loop() {
this.update();
this.render();
window.requestAnimationFrame(() => this.loop());
}
}
/**
* CircleContainer Class
*/
class CircleContainer {
/**
* CircleContainer constructor
* @param context - The context from the canvas object of the Application
* @param x
* @param y
*/
constructor(context, x, y) {
this.context = context;
this.position = {x, y};
this.numberOfCircles = 19;
this.circles = [];
this.baseRadius = 20;
this.bounceRadius = 150;
this.singleSlice = TWO_PI / this.numberOfCircles;
}
/**
* Create a number of Circle objects based on the numberOfCircles variable
* @return void
*/
initializeCircles() {
for (let i = 0; i < this.numberOfCircles; i++) {
this.circles.push(new Circle(this.position.x, this.position.y + Math.random(), this.baseRadius, this.bounceRadius, i * this.singleSlice));
}
}
/**
* Try to update the application at least 60 times a second
* @return void
*/
update() {
for (let i = 0; i < this.numberOfCircles; i++) {
this.circles[i].update(this.context);
}
}
/**
* Try to render the application at least 60 times a second
* @return void
*/
render() {
for (let i = 0; i < this.numberOfCircles; i++) {
this.circles[i].render(this.context);
}
}
}
/**
* Circle Class
*/
class Circle {
/**
* Circle constructor
* @param x - The horizontal position of this circle
* @param y - The vertical position of this circle
* @param baseRadius
* @param bounceRadius
* @param angleCircle
*/
constructor(x, y, baseRadius, bounceRadius, angleCircle) {
this.basePosition = {x, y};
this.position = {x, y};
this.speed = 0.01;
this.baseSize = 10;
this.size = 10;
this.angle = (x + y);
this.baseRadius = baseRadius;
this.bounceRadius = bounceRadius;
this.angleCircle = angleCircle;
}
/**
* Update the position of this object
* @return void
*/
update() {
this.position.x = this.basePosition.x + Math.cos(this.angleCircle) * (Math.sin(this.angle + this.angleCircle) * this.bounceRadius + this.baseRadius);
this.position.y = this.basePosition.y + Math.sin(this.angleCircle) * (Math.sin(this.angle + this.angleCircle) * this.bounceRadius + this.baseRadius);
this.size = Math.cos(this.angle) * 8 + this.baseSize;
this.angle += this.speed;
}
/**
* Renders this Circle object on the canvas
* @param context - The context from the canvas object of the Application
* @return void
*/
render(context) {
context.fillStyle = "hsl(195, 100%, "+this.size * 4+"%)";
context.beginPath();
context.arc(this.position.x, this.position.y, this.size, 0, TWO_PI);
context.fill();
}
}
/**
* Onload function is executed whenever the page is done loading, initializes the application
*/
window.onload = function () {
//Create a new instance of the application
const application = new Application();
//Initialize the CircleContainer objects
application.initializeCircleContainers();
//Start the initial loop function for the first time
application.loop();
};
Also see: Tab Triggers