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 URL's 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 it's URL and the proper URL extention.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<div id="main-container">
<canvas id="canvas"></canvas>
</div>
* { margin: 0; padding: 0;}
#main-container, canvas {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
#canvas {
display: block;
background-color: #2E282F;
}
// Init vars
var $mainCont, cv, ctx, osCv, osCtx, windowSize, mouse, gradient;
var particles = [];
var ribbonDistance = 150;
var particlesNB = 250;
// Constructeur scene
function Scene() {}
Scene.prototype.resize = function() {
windowSize = {
w: window.innerWidth,
h: window.innerHeight
};
cv.width = windowSize.w;
cv.height = windowSize.h;
osCv.width = windowSize.w;
osCv.height = windowSize.h;
};
// Init scene
Scene.prototype.init = function() {
// Création et initialisation du canvas
this.createOffScreenCanvas();
// Récupération du conteneur principal
$mainCont = document.getElementById('main-container');
// Récupération de la balise canvas
cv = document.getElementById('canvas');
// Déclaration du contexte : 2d
ctx = cv.getContext('2d');
// Position du curseur de base
mouse = {
x: window.innerWidth/2,
y: window.innerHeight/2
};
// On associe l'évenement onresize à resize
window.onresize = this.resize;
// On associe le mouvement de la souris sur le canvas à onMouseMove
canvas.onmousemove = onMouseMove;
this.resize();
makeParticles();
draw();
};
// Création des canvas offscreen pour une meilleure perf
Scene.prototype.createOffScreenCanvas = function() {
// Canvas du contexte
osCv = document.createElement('canvas');
osCtx = osCv.getContext('2d');
};
Scene.prototype.render = function() {
ctx.clearRect(0, 0, cv.width, cv.height);
//osCtx.clearRect(0, 0, osCv.width, osCv.height);
drawParticles();
ctx.drawImage(osCv, 0, 0, windowSize.w, windowSize.h, 0, 0, windowSize.w, windowSize.h);
};
function draw() {
requestAnimationFrame(draw);
scene.render();
}
function onMouseMove(e) {
mouse.x = e.pageX;
mouse.y = e.pageY;
}
// Constructeur Particle
function Particle() {
//location sur le canvas
this.location = {x: Math.random()*windowSize.w, y: Math.random()*windowSize.h};
//radius - 0
this.radius = 0;
//vitesse
this.speed = 3;
// angle de direction
this.angle = Math.random()*360;
//couleurs
var a = Math.random();
this.colors = ["rgba(214, 187, 209,"+a+")", "rgba(173, 126, 145, "+a+")", "#836982", "rgba(131, 105, 130, "+a+")", "rgba(46, 40, 47, "+a+")"];
this.color = this.colors[Math.floor(Math.random() * this.colors.length)]
};
function createParticle() {
new Particle()
}
function makeParticles() {
// On crée les particules
for(var i = 0; i < particlesNB; i++)
{
particles.push( new Particle());
}
}
function drawParticles() {
//on repaint le bg en réduidant l'opacité
osCtx.globalCompositeOperation = "source-over";
osCtx.fillStyle = "rgba(1, 20, 41, 0.008)";
osCtx.fillRect(0, 0, windowSize.w, windowSize.h);
osCtx.globalCompositeOperation = "lighter";
for(var i = 0; i < particles.length; i++)
{
var p = particles[i];
osCtx.fillStyle = "white";
osCtx.fillRect(p.location.x, p.location.y, p.radius, p.radius);
distX = p.location.x - mouse.x;
distY = p.location.y - mouse.y;
mouseDist = Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));
// On bouge les particules
// Effet bandeau
// Si le curseur est proche
if (mouseDist <= 100) {
for(var n = 0; n < particles.length; n++)
{
var p2 = particles[n];
// calcul de la distance entre 2 particules
var yd = p2.location.y - p.location.y;
var xd = p2.location.x - p.location.x;
var distance = xd*xd + yd*yd;
var angle = Math.atan2(distY, distX);
var dirX = Math.cos(angle) * distance - distX;
var dirY = Math.sin(angle) * distance - dirY;
// dessine un ligne entre les particules
if(distance < ribbonDistance*ribbonDistance)
{
osCtx.beginPath();
osCtx.lineWidth = 1;
osCtx.moveTo(p.location.x, p.location.y);
osCtx.lineTo(p2.location.x, p2.location.y);
//osCtx.lineTo(mouse.x, mouse.y);
osCtx.strokeStyle = p.color;
osCtx.stroke();
//The ribbons appear now.
}
}
p.location.x = p.location.x - p.speed*2*Math.cos(angle);
p.location.y = p.location.y - p.speed*2*Math.sin(angle);
}
if (mouseDist >= 100) {
p.location.x = p.location.x + p.speed*Math.cos(p.angle*Math.PI/180);
p.location.y = p.location.y + p.speed*Math.sin(p.angle*Math.PI/180);
}
// Quand la particule arrive au bord de la fenetre
if(p.location.x < 0) p.location.x = windowSize.w;
if(p.location.x > windowSize.w) p.location.x = 0;
if(p.location.y < 0) p.location.y = windowSize.h;
if(p.location.y > windowSize.h) p.location.y = 0;
}
};
// On init la scene
var scene = new Scene();
window.onload = scene.init.bind(scene);
Also see: Tab Triggers