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.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Particle System</title>
<style>
body,html {
margin:0;
padding:0;
}
canvas {
background-color: black;
}
</style>
</head>
<body>
<canvas></canvas>
</body>
</html>
"use strict";
var maxParticles = 200,
particleSize = 10,
emissionRate = 2,
objectSize = 3; // drawSize of emitter/field
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function Particle(point, velocity, acceleration) {
this.position = point || new Vector(0, 0);
this.velocity = velocity || new Vector(0, 0);
this.acceleration = acceleration || new Vector(0, 0);
}
Particle.prototype.move = function () {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
};
function Field(point, mass) {
this.position = point;
this.setMass(mass);
}
Field.prototype.setMass = function(mass) {
this.mass = mass || 100;
this.drawColor = mass < 0 ? "#f00" : "#0f0";
}
function Vector(x, y) {
this.x = x || 0;
this.y = y || 0;
}
Vector.prototype.add = function(vector) {
this.x += vector.x;
this.y += vector.y;
}
Vector.prototype.getMagnitude = function () {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
Vector.prototype.getAngle = function () {
return Math.atan2(this.y,this.x);
};
Vector.fromAngle = function (angle, magnitude) {
return new Vector(magnitude * Math.cos(angle), magnitude * Math.sin(angle));
};
function Emitter(point, velocity, spread) {
this.position = point; // Vector
this.velocity = velocity; // Vector
this.spread = spread || Math.PI / 32; // possible angles = velocity +/- spread
this.drawColor = "#999"; // So we can tell them apart from Fields later
}
Emitter.prototype.emitParticle = function() {
// Use an angle randomized over the spread so we have more of a "spray"
var angle = this.velocity.getAngle() + this.spread - (Math.random() * this.spread * 2);
// The magnitude of the emitter's velocity
var magnitude = this.velocity.getMagnitude();
// The emitter's position
var position = new Vector(this.position.x, this.position.y);
// New velocity based off of the calculated angle and magnitude
var velocity = Vector.fromAngle(angle, magnitude);
// return our new Particle!
return new Particle(position,velocity);
};
function addNewParticles() {
// if we're at our max, stop emitting.
if (particles.length > maxParticles) return;
// for each emitter
for (var i = 0; i < emitters.length; i++) {
// emit [emissionRate] particles and store them in our particles array
for (var j = 0; j < emissionRate; j++) {
particles.push(emitters[i].emitParticle());
}
}
}
function plotParticles(boundsX, boundsY) {
// a new array to hold particles within our bounds
var currentParticles = [];
for (var i = 0; i < particles.length; i++) {
var particle = particles[i];
var pos = particle.position;
// If we're out of bounds, drop this particle and move on to the next
if (pos.x < 0 || pos.x > boundsX || pos.y < 0 || pos.y > boundsY) continue;
// Move our particles
particle.move();
// Add this particle to the list of current particles
currentParticles.push(particle);
}
// Update our global particles reference
particles = currentParticles;
}
function drawParticles() {
//ctx.fillStyle = 'rgb(0,0,255)';
for (var i = 0; i < particles.length; i++) {
var position = particles[i].position;
var rGrad = ctx.createRadialGradient(position.x,position.y,0,position.x,position.y,5);
rGrad.addColorStop(0,'rgba(252, 194, 18, 1)');
rGrad.addColorStop(1,'rgba(252, 194, 18, 0)');
ctx.fillStyle = rGrad;
ctx.fillRect(0,0,canvas.width,canvas.height);
//ctx.fillRect(position.x, position.y, particleSize, particleSize);
}
}
var particles = [];
var midX = canvas.width / 2;
var midY = canvas.height / 2;
// Add one emitter located at `{ x : 100, y : 230}` from the origin (top left)
// that emits at a velocity of `2` shooting out from the right (angle `0`)
var emitters = [new Emitter(new Vector(midX - 100, midY), Vector.fromAngle(0, 2))];
function loop() {
clear();
update();
draw();
queue();
}
function clear() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function update() {
addNewParticles();
plotParticles(canvas.width, canvas.height);
}
function draw() {
drawParticles();
}
function queue() {
window.requestAnimationFrame(loop);
}
loop();
Also see: Tab Triggers