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"></canvas>
*{
margin: 0;
padding: 0;
}
body{
overflow: hidden;
}
function randomInt(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
}
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var width = window.innerWidth;
var height = window.innerHeight;
canvas.width = width;
canvas.height = height;
function clearCanvas(){
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, width, height);
}
clearCanvas();
var t = 0; //counter variable
var colors = ['#4f91f9', '#a7f94f', '#f94f4f', '#f9f74f', '#8930ff', '#fc4edf', '#ff9c51']; //color array for random color setting
var nodeSize = 2.5; //node size (radius)
var speed = 2; //base speed (added to each node random speed)
var nodeAmount = 100; //node amount, the more the slower
var nodes = []; //node array
var lineWidth = 1; //node connection line width in pixels
var nodePositionArray = []; //node position array for drawing the lines
var drawLineThreshold = 150; //the threshold for drawing the lines, the more the slower
//generate x random of nodes with random position and push them to the array
for(var i = 0; i < nodeAmount; i++){
var node = new Node(randomInt(0, width), randomInt(0, height), i);
nodes.push(node);
}
//circle function (draws circle given x, y and radius)
function circle(x,y,radius){
ctx.beginPath();
ctx.arc(x,y,radius,0,2*Math.PI);
ctx.closePath();
}
//line function (draws line given a set of two points(as x1,y1,x2,y2))
function line(x1,y1,x2,y2){
ctx.lineWidth = lineWidth;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
//calculate the distance between two points
function distance(x1,x2,y1,y2){
var xDist = x2-x1;
var yDist = y2-y1;
return Math.sqrt(xDist * xDist + yDist * yDist);
}
//range a numbers given a value and two ranges
function map_range(value, low1, high1, low2, high2) {
return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
}
//check if n is even
function isEven(n) {
return n == parseFloat(n)? !(n%2) : void 0;
}
//convert from hex to rgb
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
//set them nodes up! (for initializing the nodePositionArray)
for(var i = 0; i < nodes.length; i++){
nodes[i].move();
nodes[i].draw();
}
//loop function
function loop(){
//reset canvas
clearCanvas();
//draw the lines
drawLines();
//move each node and draw it
for(var i = 0; i < nodes.length; i++){
nodes[i].move();
nodes[i].draw();
}
//repeat loop
requestAnimationFrame(loop);
//increase counter
t++;
}
//run the loop
loop();
//node constructor function
function Node(x, y, id){
this.x = x;
this.y = y;
this.id = id;
//if the node id is even, start moving them in the opposite direction
if(isEven(id)){
this.speedX = randomInt(1, 100)/100 + speed;
this.speedY = randomInt(1, 100)/100 + speed;
}else{
this.speedX = randomInt(1, 100)/100 - speed;
this.speedY = randomInt(1, 100)/100 - speed;
}
this.size = nodeSize;
//set random color from array
this.color = colors[Math.floor(Math.random() * colors.length)];
this.move = function(){
this.y += this.speedY;
this.x += this.speedX;
//make them bounce
if(this.y < 1 || this.y > height-nodeSize*2){
this.speedY = -this.speedY;
}
if(this.x < 1 || this.x > width-nodeSize*2){
this.speedX = -this.speedX;
}
//push the position and color to the array for drawing the lines
nodePositionArray[this.id] = [this.x, this.y, this.color];
};
//draw the nodes
this.draw = function(){
ctx.fillStyle = this.color;
circle(this.x, this.y, nodeSize);
ctx.fill();
};
}
function drawLines(){
for(var i = 0; i < nodePositionArray.length - 1; i++){
//get the origin point
var x1 = nodePositionArray[i][0];
var y1 = nodePositionArray[i][1];
//this sub-loop is made to avoid drawing the nodes twice, which leads to consume more cpu and spoils the opacity effect
for(var j = 0; j < nodePositionArray.length - (i+1); j++){
//get the destination point
var x2 = nodePositionArray[j+i+1][0];
var y2 = nodePositionArray[j+i+1][1];
//calculate distance between the origin and target points
var dist = distance(x1,x2,y1,y2);
//if distance is greater than the threshold, draw the lines
if(dist<drawLineThreshold){
var finalOpacity = map_range(dist, 0, drawLineThreshold, 1, 0);
var rgbValues = hexToRgb(nodePositionArray[i][2]);
var color = 'rgba('+rgbValues.r+','+rgbValues.g+','+rgbValues.b+','+finalOpacity+')';
ctx.strokeStyle = color;
line(x1,y1,x2,y2);
}
}
}
}
//resize event listener
window.addEventListener('resize', function(){
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
clearCanvas();
//reset the nodes
nodes = [];
//generate nodes again
for(var i = 0; i < nodeAmount; i++){
var node = new Node(randomInt(0, width), randomInt(0, height), i);
nodes.push(node);
}
//set them up
for(var i = 0; i < nodes.length; i++){
nodes[i].move();
nodes[i].draw();
}
});
Also see: Tab Triggers