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>
body, html {
margin: 0;
}
canvas {
display: block;
cursor: pointer;
}
/*
Johan Karlsson, 2019
https://twitter.com/DonKarlssonSan
MIT License, see Details View
Similar triangles (side splitting theorem):
http://www.malinc.se/math/geometry/similartrianglesen.php
https://en.wikipedia.org/wiki/Delaunay_triangulation
https://en.wikipedia.org/wiki/Bowyer%E2%80%93Watson_algorithm
https://en.wikipedia.org/wiki/Circumscribed_circle
*/
class Triangle {
constructor(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
}
vertexes() {
return [this.a, this.b, this.c];
}
edges() {
return [
[this.a, this.b],
[this.b, this.c],
[this.c, this.a]
];
}
sharesAVertexWith(triangle) {
// TODO: optimize me please!
for(let i = 0; i < 3; i++) {
for(let j = 0; j < 3; j++) {
let v = this.vertexes()[i];
let vv = triangle.vertexes()[j];
if(v.equals(vv)) {
return true;
}
}
}
return false;
}
hasEdge(edge) {
for(let i = 0; i < 3; i++) {
let e = this.edges()[i];
if(e[0].equals(edge[0]) && e[1].equals(edge[1]) ||
e[1].equals(edge[0]) && e[0].equals(edge[1])) {
return true;
}
}
return false;
}
circumcenter() {
let d = 2 * (this.a.x * (this.b.y - this.c.y) +
this.b.x * (this.c.y - this.a.y) +
this.c.x * (this.a.y - this.b.y));
let x = 1 / d * ((this.a.x * this.a.x + this.a.y * this.a.y) * (this.b.y - this.c.y) +
(this.b.x * this.b.x + this.b.y * this.b.y) * (this.c.y - this.a.y) +
(this.c.x * this.c.x + this.c.y * this.c.y) * (this.a.y - this.b.y));
let y = 1 / d * ((this.a.x * this.a.x + this.a.y * this.a.y) * (this.c.x - this.b.x) +
(this.b.x * this.b.x + this.b.y * this.b.y) * (this.a.x - this.c.x) +
(this.c.x * this.c.x + this.c.y * this.c.y) * (this.b.x - this.a.x));
return new Vector(x, y);
}
circumradius() {
return this.circumcenter().sub(this.a).getLength();
}
pointIsInsideCircumcircle(point) {
let circumcenter = this.circumcenter();
let circumradius = circumcenter.sub(this.a).getLength();
let dist = point.sub(circumcenter).getLength();
return dist < circumradius;
}
draw() {
ctx.strokeStyle = "black";
ctx.lineWidth = Math.random() * 3 + 1;
ctx.beginPath();
ctx.lineTo(this.a.x, this.a.y);
ctx.lineTo(this.b.x, this.b.y);
ctx.lineTo(this.c.x, this.c.y);
ctx.closePath();
ctx.stroke();
// Similar triangles, see link at the top
let nrOfPoints = Math.round(Math.random() * 9 + 2);
let points1 = this.getPoints(this.c, this.a, nrOfPoints);
let points2 = this.getPoints(this.c, this.b, nrOfPoints);
for(let i = 0; i < nrOfPoints; i++) {
ctx.beginPath();
ctx.moveTo(points1[i].x, points1[i].y);
ctx.lineTo(points2[i].x, points2[i].y);
ctx.stroke();
}
}
getPoints(p1, p2, nrOfPoints) {
let points = [];
let delta = p1.sub(p2).div(nrOfPoints+1);
for(let i = 1; i < nrOfPoints+1; i++) {
let currentPos = p2.add(delta.mult(i));
points.push(currentPos);
}
return points;
}
}
let canvas;
let ctx;
let w, h;
function setup() {
canvas = document.querySelector("#canvas");
ctx = canvas.getContext("2d");
reset();
window.addEventListener("resize", reset);
canvas.addEventListener("click", draw);
}
function reset() {
w = canvas.width = window.innerWidth;
h = canvas.height = window.innerHeight;
ctx.lineCap = "round";
ctx.lineJoin = "round";
draw();
}
function getRandomPoints() {
let pointList = [];
let divisor = Math.random() * 5000 + 5000;
let nrOfPoints = w * h / divisor;
for(let i = 0; i < nrOfPoints; i++) {
pointList.push(new Vector(
Math.random() * w * 0.95 + w * 0.025,
Math.random() * h * 0.95 + h * 0.025
));
}
return pointList;
}
function bowyerWatson (superTriangle, pointList) {
// pointList is a set of coordinates defining the
// points to be triangulated
let triangulation = [];
// add super-triangle to triangulation
// must be large enough to completely contain all
// the points in pointList
triangulation.push(superTriangle);
// add all the points one at a time to the triangulation
pointList.forEach(point => {
let badTriangles = [];
// first find all the triangles that are no
// longer valid due to the insertion
triangulation.forEach(triangle => {
if(triangle.pointIsInsideCircumcircle(point)) {
badTriangles.push(triangle);
}
});
let polygon = [];
// find the boundary of the polygonal hole
badTriangles.forEach(triangle => {
triangle.edges().forEach(edge => {
let edgeIsShared = false;
badTriangles.forEach(otherTriangle => {
if(triangle !== otherTriangle && otherTriangle.hasEdge(edge)) {
edgeIsShared = true;
}
});
if(!edgeIsShared) {
//edge is not shared by any other
// triangles in badTriangles
polygon.push(edge);
}
});
});
// remove them from the data structure
badTriangles.forEach(triangle => {
let index = triangulation.indexOf(triangle);
if (index > -1) {
triangulation.splice(index, 1);
}
});
// re-triangulate the polygonal hole
polygon.forEach(edge => {
//form a triangle from edge to point
let newTri = new Triangle(edge[0], edge[1], point);
triangulation.push(newTri);
});
});
// done inserting points, now clean up
let i = triangulation.length;
while(i--) {
let triangle = triangulation[i];
if(triangle.sharesAVertexWith(superTriangle)) {
//remove triangle from triangulation
let index = triangulation.indexOf(triangle);
if (index > -1) {
triangulation.splice(index, 1);
}
}
}
return triangulation;
}
function draw() {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, w, h);
ctx.lineCap = "round";
ctx.lineJoin = "round";
let pointList = getRandomPoints();
let superTriangle = new Triangle(
new Vector(-w * 2, h * 2),
new Vector(w * 2, h * 2),
new Vector(w / 2, -h * 2)
);
let triangles = bowyerWatson(superTriangle, pointList);
triangles.forEach(t => t.draw());
}
setup();
Also see: Tab Triggers