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 class="ui">
<p class="zoom"><span class="zoom zoomin">+</span><span class="zoom zoomout">-</span></p>
<p class="zoomlevel"><span class="percent">100</span> % - (<span class="width"></span>px)(<span class="height"></span>px)</p>
<p>Dead: <span class="dead">0</span></p>
<p>Alive: <span class="alive">0</span></p>
<p>Drawn: <span class="drawn">0</span></p>
<p><span class="fps">0</span> FPS</p>
<a class="save" href="" download="capture.png">Save</a>
</div>
* {
border: none;
margin: 0;
}
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
}
body {
//background: radial-gradient(#555, #111);
}
canvas {
background: white;
background: radial-gradient(#FFF, #DDD);
//background: radial-gradient(hsl(40, 80%, 60%), hsl(0, 50%, 40%));
//filter: blur(1px) contrast(5);
//transform: scale(0.1);
transform-origin: 0 0;
//border: solid .8em green;
}
.ui {
display: none;
position: fixed;
z-index: 5;
bottom: 0;
left: 0;
width: 120px;
padding: 10px;
background: rgba(255,255,255,0.7);
p {
//color: white;
font-size: 11px;
font-weight: 700;
&.zoom {
margin-bottom: 5px;
span {
margin-right: 5px;
border: solid 1px #777;
cursor: pointer;
border-radius: 2px;
&.zoomin {
padding: 2px 5px;
}
&.zoomout {
padding: 2px 8px;
}
&:hover {
background: black;
color: white;
}
}
}
}
span.zoom {
}
}
/**
* @author Alex Andrix <alex@alexandrix.com>
* @since 2019-02-05
*/
var App = {};
App.setup = function() {
var canvas = document.createElement('canvas');
var maxWidth = 9933, maxHeight = 14043;
var quality = 0.15;
this.filename = "brownie";
this.canvas = canvas;
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.ctx = this.canvas.getContext('2d');
this.width = this.canvas.width;
this.height = this.canvas.height;
this.dataToImageRatio = this.width / 1000;// Adimensional space [-500, 500]
// Blend mode /!\ Don't use if you can! Mega source of lag, proportional to canvas size, and depends on which mode
//this.ctx.globalCompositeOperation = 'darker';
//this.ctx.globalCompositeOperation = 'lighter';
this.ctx.imageSmoothingEnabled = false;
this.ctx.webkitImageSmoothingEnabled = false;
this.ctx.msImageSmoothingEnabled = false;
this.xC = this.width / 2;
this.yC = this.height / 2;
document.getElementsByTagName('body')[0].appendChild(canvas);
this.stepCount = 0;
this.particles = [];
this.lifespan = 2000;
this.popPerBirth = 1;
this.maxPop = 10;
this.birthFreq = 10;
// Counters for UI
this.drawnInLastFrame = 0;
this.deathCount = 0;
this.initDraw();
};
App.evolve = function() {
var time1 = performance.now();
this.stepCount++;
if (this.stepCount % this.birthFreq == 0 && (this.particles.length + this.popPerBirth) < this.maxPop) {
this.birth();
}
App.move();
App.draw();
var time2 = performance.now();
// Update UI
document.getElementsByClassName('dead')[0].textContent = this.deathCount;
document.getElementsByClassName('alive')[0].textContent = this.particles.length;
document.getElementsByClassName('fps')[0].textContent = Math.floor(1000 / (time2 - time1));
document.getElementsByClassName('drawn')[0].textContent = this.drawnInLastFrame;
};
App.birth = function() {
var x, y, v2 = Math.sqrt(2);
x = -500 + 1000 * Math.random();
y = v2 * (-500 + 1000 * Math.random());
/*
// Birth on edges
var rand = Math.random();
// Left side
if (rand < 0.25) { x = -500; y = v2 * (-500 + 1000 * Math.random()); }
// Right side
else if (rand < 0.5) { x = 500; y = v2 * (-500 + 1000 * Math.random()); }
// Top side
else if (rand < 0.75) { x = -500 + 1000 * Math.random(); y = v2 * (-500); }
// Bottom side
else { x = -500 + 1000 * Math.random(); y = v2 * (500); }
// Birth at center
if (Math.random() < 1/6) { x = 0; y = 0; }
*/
var particle = {
x: x, y: y,
xLast: x, yLast: y,
xSpeed: 0, ySpeed: 0,
xAcc: 0, yAcc: 0,
age: 0,
mark: Math.random(),
attractor: {
x: x,
y: y,
xSpeed: 0,
ySpeed: 0
},
name: 'seed-' + Math.ceil(10000000 * Math.random())
};
this.particles.push(particle);
};
App.kill = function(particleName) {
var newArray = _.reject(this.particles, function(seed) {
return (seed.name == particleName);
});
this.particles = _.cloneDeep(newArray);
};
App.move = function() {
for (var i = 0; i < this.particles.length; i++) {
// Get particle
var p = this.particles[i];
// Save last position
p.xLast = p.x; p.yLast = p.y;
// Brownian viscous for attractor
var broVisc = .9;
// Brownian jump amplitude based on distance to center (no actually the overall nice look depends on uniformity in space)
var r = Math.sqrt(p.x*p.x + p.y*p.y);
var amp = 25 * Math.random(),//(1 - Math.exp(-Math.pow(r/400, 1.5))) * Math.random(),
dir = 2 * Math.PI * Math.random();
var xAccA = amp * Math.cos(dir),
yAccA = amp * Math.sin(dir);
p.attractor.xSpeed += xAccA; p.attractor.ySpeed += yAccA;
p.attractor.xSpeed *= broVisc; p.attractor.ySpeed *= broVisc;
p.attractor.x += 0.1 * p.attractor.xSpeed;
p.attractor.y += 0.1 * p.attractor.ySpeed;
// Particle is linked to its attractor with stiffness and visc (I tried gravitation but it really doesn't do much)
var k = .1,// Wow, so nervous man 30 is huge
visc = .99;
var dx = p.x - p.attractor.x,
dy = p.y - p.attractor.y;
var xAccP = -k * dx,
yAccP = -k * dy;
p.xAcc = xAccP; p.yAcc = yAccP;
p.xSpeed += xAccP; p.ySpeed += yAccP;
p.xSpeed *= visc; p.ySpeed *= visc;
p.x += 0.1 * p.xSpeed; p.y += 0.1 * p.ySpeed;
// Get older
p.age++;
// Kill if too old
if (p.age > this.lifespan) {
this.kill(p.name);
this.deathCount++;
}
}
};
App.initDraw = function() {
// White layer
this.ctx.beginPath();
this.ctx.rect(0, 0, this.width, this.height);
this.ctx.fillStyle = 'white';
this.ctx.fill();
this.ctx.closePath();
};
App.draw = function() {
this.drawnInLastFrame = 0;
if (!this.particles.length) return false;
for (var i = 0; i < this.particles.length; i++) {
// Draw particle
var p = this.particles[i];
var speedP = Math.sqrt(p.xSpeed * p.xSpeed + p.ySpeed * p.ySpeed);
var h, s, l, a;
h = 200;//240 + 70 * Math.sin(this.stepCount/230);
//h = 180 + this.stepCount/10;// You can try a linear drift it's cool too
s = 70;
//l = 30 + 0.17 * (-0.5 + Math.random()) * speedP;// Magic HSL trick
var speedDir = segmentAngleRad(0, 0, p.xSpeed, p.ySpeed, false);
var accDir = segmentAngleRad(0, 0, p.xAcc, p.yAcc, false);
var accLight = Math.pow(1 + Math.cos(accDir + Math.PI/2), 3);
var speedLight = Math.pow(
0.5 + 0.5 * Math.cos(2 * (speedDir + Math.PI/3)), 2);
//l = 10 + .5 * accLight * speedLight;
//l = 90 - 30 * speedLight;
l = 85 - 60 * speedLight;
h = 15 - 15 * speedLight;
s = 70 + 30 * speedLight;
a = 0.9;
this.ctx.beginPath();
var b4 = this.dataXYtoCanvasXY(p.xLast, p.yLast),
aftah = this.dataXYtoCanvasXY(p.x, p.y);
this.ctx.moveTo(b4.x, b4.y);
this.ctx.lineTo(aftah.x, aftah.y);
//this.ctx.arc(aftah.x, aftah.y, 1, 0, 2 * Math.PI, false);
this.ctx.strokeStyle = 'hsla(' + h + ', ' + s + '%, ' + l + '%, ' + a + ')';
//this.ctx.lineWidth = 1.2 * Math.floor(p.mark * 3) * this.dataToImageRatio;
//this.ctx.lineWidth = 1 * this.dataToImageRatio;
this.ctx.lineWidth = (1 + speedLight * 10) * this.dataToImageRatio;
this.ctx.stroke();
this.ctx.closePath();
// Attractor
/*
this.ctx.beginPath();
h += 30;
a *= 0.1;
this.ctx.fillStyle = 'hsla(' + h + ', ' + s + '%, ' + l + '%, ' + a + ')';
var r = Math.floor(p.mark * 3) * this.dataToImageRatio;
this.ctx.arc(p.attractor.x, p.attractor.y, r, 0, 2 * Math.PI, false);
this.ctx.fill();
this.ctx.closePath();
*/
// UI counter
this.drawnInLastFrame++;
}
};
App.dataXYtoCanvasXY = function(dataX, dataY) {
var zoom = .9;
return {
x: this.xC + zoom * dataX * this.dataToImageRatio,
y: this.yC + zoom * dataY * this.dataToImageRatio
};
};
App.ui = function() {
var $canvas = document.querySelector('canvas');
var $zoomPercent = document.querySelector('.zoomlevel .percent');
var $zoomWidth = document.querySelector('.zoomlevel .width');
var $zoomHeight = document.querySelector('.zoomlevel .height');
var scale = 1;
var updateUIzoom = function() {
$zoomPercent.innerHTML = Math.round(scale * 100);
$zoomWidth.innerHTML = App.width;
$zoomHeight.innerHTML = App.height;
};
var zoomIn = function() {
scale *= 2;
$canvas.style.transform = 'scale(' + scale + ')';
updateUIzoom();
};
var zoomOut = function() {
scale /= 2;
$canvas.style.transform = 'scale(' + scale + ')';
updateUIzoom();
};
// Zoom in on click
document.querySelector('.zoomin').addEventListener('click', zoomIn);
// Zoom out on click
document.querySelector('.zoomout').addEventListener('click', zoomOut);
// Preforce zoom
//zoomOut();zoomOut();//zoomOut();zoomOut();
document.querySelector('a.save').addEventListener('click', function(ev) {
this.href = App.canvas.toDataURL();
var date = new Date(),
timestamp = date.getTime();
this.download = App.filename + '-' + App.width + '-' + App.height + '-' + timestamp + ".png";
}, false);
};
document.addEventListener('DOMContentLoaded', function() {
App.setup();
App.ui();
App.draw();
var frame = function() {
App.evolve();
requestAnimationFrame(frame);
};
frame();
});
/**
* Some old util I wrote, might not even use it.
*
* @param {Number} Xstart X value of the segment starting point
* @param {Number} Ystart Y value of the segment starting point
* @param {Number} Xtarget X value of the segment target point
* @param {Number} Ytarget Y value of the segment target point
* @param {Boolean} realOrWeb true if Real (Y towards top), false if Web (Y towards bottom)
* @returns {Number} Angle between 0 and 2PI
*/
segmentAngleRad = function(Xstart, Ystart, Xtarget, Ytarget, realOrWeb) {
var result;// Will range between 0 and 2PI
if (Xstart == Xtarget) {
if (Ystart == Ytarget) {
result = 0;
} else if (Ystart < Ytarget) {
result = Math.PI/2;
} else if (Ystart > Ytarget) {
result = 3*Math.PI/2;
} else {}
} else if (Xstart < Xtarget) {
result = Math.atan((Ytarget - Ystart)/(Xtarget - Xstart));
} else if (Xstart > Xtarget) {
result = Math.PI + Math.atan((Ytarget - Ystart)/(Xtarget - Xstart));
}
result = (result + 2*Math.PI)%(2*Math.PI);
if (!realOrWeb) {
result = 2*Math.PI - result;
}
return result;
}
Also see: Tab Triggers