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 class="voronoi"></canvas>
<div class="time"></div>
html,
body {
height: 100%;
overflow: hidden;
font-family: sans-serif;
}
canvas.voronoi {
display: block;
}
.time {
position: absolute;
top: 8px;
left: 12px;
font-size: 11px;
letter-spacing: 0.05em;
color: #fff;
text-shadow: 0 0 4px #000
}
console.clear();
const config = {
pointDensity: 2
};
// DOM references
const canvasNode = document.querySelector('.voronoi');
const timeNode = document.querySelector('.time');
// Reference to canvas context
const context = canvasNode.getContext('2d');
// Timing helper
let _startTime;
function startTime() {
_startTime = Date.now();
}
function endTime() {
const totalTime = Date.now() - _startTime;
timeNode.textContent = `${totalTime}ms`;
}
startTime();
endTime();
// Helper to resize canvas to viewport dimensions.
function resizeCanvas() {
// Not high-DPI
canvasNode.width = window.innerWidth;
canvasNode.height = window.innerHeight;
}
// Helper to generate random colors, inspired by the colors seen on bubbles or oil slicks.
// See: https://codepen.io/MillerTime/pen/NXxxma
const colorSampler = (function GradientSamplerFactory() {
// The instance to be returned.
const sampler = {};
// Gradient color stops in RGB format.
const colors = [
{ r: 144, g: 18, b: 96 },
{ r: 12, g: 43, b: 179 },
{ r: 0, g: 196, b: 247 },
{ r: 190, g: 255, b: 255 },
{ r: 255, g: 232, b: 0 },
{ r: 255, g: 103, b: 0 },
{ r: 191, g: 26, b: 156 },
{ r: 0, g: 79, b: 229 },
{ r: 0, g: 196, b: 9 }
];
const colorCount = colors.length;
const colorSpans = colorCount - 1;
const spanSize = 1 / colorSpans;
// Helper to interpolate between two numbers
function interpolate(a, b, p) {
return (b - a) * p + a;
};
sampler.sample = function sample(position) {
// Normalize position to 0..1 scale (inclusive of 0, exlusive of 1).
position -= position | 0;
if (position < 0) position = 1 - position * -1;
const startIndex = position * colorSpans | 0;
const startColor = colors[startIndex];
const endColor = colors[startIndex + 1];
// Compute relative position between two chosen color stops.
const innerPosition = (position - (startIndex / colorSpans)) / spanSize;
const r = interpolate(startColor.r, endColor.r, innerPosition) | 0;
const g = interpolate(startColor.g, endColor.g, innerPosition) | 0;
const b = interpolate(startColor.b, endColor.b, innerPosition) | 0;
return { r, g, b };
};
return sampler;
})();
// Helper to shade a point color based on distance.
function getPointColorAtDistance(point, distance, maxDistance) {
const { r, g, b } = point.color;
const scale = 1 - Math.min(1, distance / maxDistance);
return {
r: r * scale | 0,
g: g * scale | 0,
b: b * scale | 0
};
}
// Helper to generate a random 2D point instance.
function makeRandomPoint() {
return {
x: Math.floor(Math.random() * canvasNode.width),
y: Math.floor(Math.random() * canvasNode.height),
color: colorSampler.sample(Math.random())
};
}
// Helper to find the point in a set of points which is closest to a given coordinate.
// Returns an object with two properties: `point`, and `distance`.
function findNearestPoint(x, y, points) {
let winningPoint = null;
let winningDistance = Infinity;
for (let p of points) {
// Pythagorean Theorem
const a = p.x - x;
const b = p.y - y;
const distance = Math.sqrt(a * a + b * b);
if (distance < winningDistance) {
winningPoint = p;
winningDistance = distance;
}
}
return {
point: winningPoint,
distance: winningDistance
};
}
// The brute-force algorithm.
function drawVoronoi() {
startTime();
// Compute some helpful numbers.
const width = canvasNode.width;
const height = canvasNode.height;
const canvasArea = width * height;
// Generate points for cells.
const pointCount = Math.floor(canvasArea / 20000 * config.pointDensity);
const points = [];
for (let i=0; i<pointCount; i++) {
points.push(makeRandomPoint());
}
// For each pixel, find the nearest point, and compute color from that point.
// Distance where darkest color is used.
const maxDistance = Math.sqrt(canvasArea) / 4;
// Pixel buffer
const pixelData = [];
for (let i=0; i<canvasArea; i++) {
const x = i % width;
const y = i / width | 0;
const nearest = findNearestPoint(x, y, points);
const color = getPointColorAtDistance(nearest.point, nearest.distance, maxDistance);
pixelData.push(color.r);
pixelData.push(color.g);
pixelData.push(color.b);
pixelData.push(255); // Alpha always full
}
// Draw pixels to canvas.
const imgData = new ImageData(
new Uint8ClampedArray(pixelData),
width,
height
);
context.putImageData(imgData, 0, 0);
endTime();
}
// Init
resizeCanvas();
drawVoronoi();
window.addEventListener('click', () => {
resizeCanvas();
drawVoronoi();
});
Also see: Tab Triggers