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.
<main>
<div id="container"></div>
</main>
body{
background: black;
overflow: hidden;
}
main {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
#container {
height: 300px;
width: 300px;
margin: 0 auto;
}
window.onload = function () {
'use strict';
var container = document.querySelector('#container'),
width = container.offsetWidth,
height = container.offsetHeight,
svg = Snap(width * 1.15, height * 1.15),
svgWidth = parseInt(svg.node.getAttribute('width')),
svgHeight = parseInt(svg.node.getAttribute('height'));
var MAX_FIREFLIES = 35,
FIREFLY_SPEED = 15000,
FIREFLY_BLINK_SPEED = 4000,
BASE_PATH_WIDTH = 275;
container.appendChild(svg.node);
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function FireFly(){
this.group = generateGroup();
this.path = this.group[0];
this.circle = this.group[1];
}
function generateTransform(){
var leftPosition = (svgWidth - BASE_PATH_WIDTH) / 2,
topPosition = (svgHeight - BASE_PATH_WIDTH) / 2;
return 't' + leftPosition + ',' + topPosition + ',r' + getRandomInt(1, 359);
}
function generateGroup(){
var group = svg.g(),
path = generatePath(),
coords = path.getPointAtLength(0),
circle = generateCircle(coords.x, coords.y);
group.add(path, circle);
group.transform(generateTransform());
return group;
}
var randomPath = [
'M1.453125,-0.55859375 C75.5625,76.515625 152.230469,34.4414062 152.230469,34.4414062 C152.230469,34.4414062 276.964844,-36.1484375 250.523438,67.8125 C224.082031,171.773438 261.673768,157.95441 250.523438,195.105469 C239.373107,232.256527 293.597656,283.253906 194.042969,255.996094 C94.4882812,228.738281 90.96875,224.03125 54.3398438,255.996094 C17.7109375,287.960937 -22.921875,252.710938 25.265625,195.105469 C73.453125,137.5 64.2304688,137.5 47.7460938,118.441406 C31.2617188,99.3828125 22.0820312,80.8085938 32.0078125,57.625 C41.9335938,34.4414062 129.597656,-36.703125 154.746094,91.6640625 C179.894531,220.03125 264.859375,262.847656 264.859375,262.847656',
'M0.5625,-0.078125 C0.5625,-0.078125 266.597656,77.9179687 261.859375,202.105469 C257.121094,326.292969 89.5748377,243.911913 53.8242188,192.550781 C18.0735998,141.189649 -26.9921875,-14.5625 89.265625,15.328125 C205.523438,45.21875 234.232847,74.6313182 251.5625,121.585938 C268.892153,168.540557 290.335938,281.546875 171.746094,238.808594 C53.15625,196.070312 240.042969,12.6601562 240.042969,12.6601562',
'M1.08203125,-0.66796875 C-17.9377166,-21.4041628 9.15625,64.2929688 72.9335937,64.2929688 C136.710937,64.2929688 87.8177529,-21.0716058 150.488281,12.1445312 C213.15881,45.3606683 293.046875,52.3984375 260.488281,163.699219 C227.929687,275 148.199223,260.893557 94.5625,254.144531 C40.9257767,247.395506 -4.25,191.734375 14.7109375,132.773438 C33.671875,73.8125 173.96875,86.4023438 173.96875,86.4023438 C173.96875,86.4023438 284.410156,108.921875 217.449219,167.699219 C150.488281,226.476562 74.671875,186.03125 74.671875,186.03125 C74.671875,186.03125 -8.80859375,153.144531 77.96875,119.773438 C164.746094,86.4023438 254.339844,262.070312 254.339844,262.070312'
];
function generatePath(){
var path = svg.path( randomPath[getRandomInt(0, randomPath.length - 1)] ).attr({
fill: 'none',
strokeWidth: '0'
});
return path;
}
function generateCircle(cx, cy){
var circle = svg.circle(5,5,2)
.attr({
fill: '#CFF09E',
stroke: '#ffffff',
strokeOpacity: 0.1,
strokeWidth: 5,
opacity: 0,
class: 'firefly',
zIndex: 1,
cx: cx,
cy: cy
});
return circle;
}
FireFly.prototype.delay = function(){
var self = this;
setTimeout(function(){
self.blink();
self.fly();
}, getRandomInt(1, 10) * 1000);
};
FireFly.prototype.blink = function(){
var self = this;
self.circle.animate({opacity: 1}, FIREFLY_BLINK_SPEED / 2, function(){
this.animate({opacity: 0}, FIREFLY_BLINK_SPEED / 4);
});
setTimeout(function(){self.blink()}, FIREFLY_BLINK_SPEED);
};
FireFly.prototype.fly = function(){
var self = this,
length = self.path.getTotalLength();
/* self.path.attr({
stroke: '#fff',
strokeWidth: 1,
fill: 'none',
"stroke-dasharray": length + " " + length,
"stroke-dashoffset": length
}).animate({"stroke-dashoffset": 10}, FIREFLY_SPEED, mina.linear);*/
Snap.animate(0, length, function( value ) {
var movePoint = self.path.getPointAtLength( value );
self.circle.attr({ cx: movePoint.x, cy: movePoint.y });
}, FIREFLY_SPEED, mina.linear);
setTimeout(function(){self.fly()}, FIREFLY_SPEED);
};
var fireFlies = [];
for (var i = 0, l = MAX_FIREFLIES; i < l; i++) {
fireFlies.push(new FireFly());
fireFlies[i].delay();
}
};
Also see: Tab Triggers