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.
<body>
<canvas></canvas>
<!--
* Stronger forces are red.
* Weaker are green.
* lines disappear when particles are too far apart for the forces to act
* Click to add more particles > forces continuously flick on-and-off and mostly form a triangular lattice, but with tears where there appears to be, but isn't, a force triangle. Changing of the colors show how the force shockwaves travel through the system after a new particle has been added
Life's Not Complete Without Art.
__________________
/\ ______________ \
/::\ \ZZZZZZZZZZZZ/\ \
/:/\.\ \ /:/\:\ \
/:/Z/\:\ \ /:/Z/\:\ \
/:/Z/__\:\ \____/:/Z/ \:\ \
/:/Z/____\:\ \___\/Z/ \:\ \
\:\ \ZZZZZ\:\ \ZZ/\ \ \:\ \
\:\ \ \:\ \ \:\ \ \:\ \
\:\ \ \:\ \_\;\_\_____\;\ \
\:\ \ \:\_________________\
\:\ \ /:/ZZZZZZZZZZZZZZZZZ/
\:\ \ /:/Z/ \:\ \ /:/Z/
\:\ \/:/Z/ \:\ \/:/Z/
\:\/:/Z/________\;\/:/Z/
\::/Z/_______tmr__\/Z/
\/ZZZZZZZZZZZZZZZZZ/
--!>
body{
margin:0;
width:100%;
background: hsla(0,0%,0%,1);
overflow:hidden;
}
canvas{
width:60%;
height:60%;
}
var w = 500, //width
h = 500, //height
parts = [], //particle array
count_fr = 100, //start counter at (#particles)
max_parts = 500, //max number of particles
fs = 0.09, //fade speed > time to draw and fade lines depending on strength of force (higher # = increase speed)
cur_frm = 0; //current frame
var c = document.getElementsByTagName ('canvas')[0]; //get canvas(c) / c[array]
c.width = w;
c.height = h;
var $ = c.getContext ('2d'); // $ var for getContext
init ();
function random (min, max) { //random min/max
return (Math.random() * (max - min) + min);
}
function part_cd () { //particle_countdown > based on count_from #.
addPart (random(0, h), random(0, h));
if (parts.length < count_fr) setTimeout (part_cd, 50);
}
function init () { //start particle countdown; begin animation sequence; listen for mouse click; animate
part_cd();
setInterval (animate, 1000 / 50);
c.addEventListener ('mousedown', onMouseDown, false);
animate ();
}
function onMouseDown (e) { //addParticle() on mouse down
addPart(e.offsetX, e.offsetY);
}
function addPart(x, y) { //add particle function
parts.push ({
position: {
x: x || random(0, w),
y: y || random (0, h)
},
force : {
x: 0,
y: 0
},
upd : {
x: 0,
y: 0
}
});
}
function animate () { //animation sequence
//while there are less particles on the canvas than the max allowed, shift 'forces'
while (parts.length > max_parts) parts.shift();
cur_frm++;
$.save ();
$.fillStyle = "rgba(0,0,0,0.3)"; //styles
$.fillRect (0, 0, w, h);
$.fillStyle = 'rgba(0,0,0,0)';
$.restore();
var force = {x : 0, y: 0};
for (var i = 0; i < parts.length; i++) {
var p1 = parts[i];
for (var j = i + 1; j < parts.length; j++) {
var p2 = parts[j];
// length (determines force strength)
force.x = p2.position.x - p1.position.x;
force.y = p2.position.y - p1.position.y;
var magnitude = mag (force);
var actingDistance = 50-magnitude;
if ((actingDistance > 0) && (magnitude > 0)) {
var red = 0; //red value threshold for strong forces
var green = 120; //green value threshold for weaker forces
var color = green - actingDistance * (green / 2); //the fading of the weaker (green) forces > the initial color value of the connectors (forces)
//changing of colors based on above 'color' value
if (color < 0) color = 0; //red if the color value is less than 0
if (color > 120) color = 120; //green if color value is greater than 120
$.save ();
$.strokeStyle = 'hsla(' + color + ', 100%, 50%, 1)'; //draw connectors based on 'color' value
$.strokeWidth = 2;
$.beginPath (); //begin path to particle1-x,y and particle2-x,y positions
$.moveTo (p1.position.x, p1.position.y);
$.lineTo (p2.position.x, p2.position.y);
$.stroke(); //draw
$.closePath(); //connect paths
$.restore (); //restore canvas
//the 'forces' > particle connector positions
force.x *= fs * actingDistance / magnitude;
force.y *= fs * actingDistance / magnitude;
p1.force.x -= force.x;
p1.force.y -= force.y;
p2.force.x += force.x;
p2.force.y += force.y;
}
}
draw (p1);
upd (p1);
}
}
function mag (vector) { //force magnitude value
return Math.sqrt (vector.x * vector.x + vector.y * vector.y);
}
//update particle based on force
function upd (particle) {
with (particle) {
upd.x += force.x;
upd.y += force.y;
upd.x *= 0.8;
upd.y *= 0.8;
position.x += upd.x;
position.y += upd.y;
force.x = 0;
force.y = 0;
//force thresholds
if (position.x > w - 26) { //position x based on threshold condition; if position value is > width-26, then this condition value becomes the new position value. This is our max-outer boundary.
position.x = w - 26;
}
if (position.x < 21) { //position x based on threshold condition; however, if position value is < 21, then this condition value becomes the new position value. This is our 'preferred' outer boundary.
position.x = 21;
}
if (position.y > h - 26) { //same as above, but applied to y positions.
position.y = h - 26;
}
if (position.y < 21) {
position.y = 21;
}
}
}
//draw particles
function draw (particle) {
$.save ();
$.translate (particle.position.x, particle.position.y);
$.fillStyle = "hsla("+cur_frm%360 +",50%,50%,1)";
$.fillRect (0, 0, 5, 5);
$.restore ();
}
Also see: Tab Triggers