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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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.
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.16/p5.min.js"></script>
html,
body {
margin: 0;
padding: 0;
}
var hexPoints_xlarge = null;
var hexSize = 60;
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(1);
ellipseMode(CENTER);
// Get the 1st array of hexPoints
hexPoints_xlarge = makeGrid(hexSize);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
hexPoints_xlarge = makeGrid(hexSize);
}
function draw() {
// Paint it black
background(color(0));
fill(color(255));
noStroke();
// Go thru them drawing hexes
var hPoint = null;
for (var i = 0; i < hexPoints_xlarge.length; i++) {
hPoint = hexPoints_xlarge[i];
fill(color(255, 255, 255));
drawHex(hPoint.x, hPoint.y, hexSize+1); // 1px larger to hide edges
}
}
function drawHex(x, y, radius) {
// Work out the outside 6 points.
var NEx = -radius * sin(radians(-30));
var NEy = -radius * cos(radians(-30));
var Ex = -radius * sin(radians(-90));
var Ey = -radius * cos(radians(-90));
var SEx = -radius * sin(radians(-150));
var SEy = -radius * cos(radians(-150));
var SWx = -radius * sin(radians(-210));
var SWy = -radius * cos(radians(-210));
var Wx = -radius * sin(radians(-270));
var Wy = -radius * cos(radians(-270));
var NWx = -radius * sin(radians(-330));
var NWy = -radius * cos(radians(-330));
// Now we need to work out the mid points
// between the NE and E points, and so on
// so we can slit the whole thing into 4
// bits
var NEEx = lerp(NEx, Ex, 0.5);
var NEEy = lerp(NEy, Ey, 0.5);
var SEEx = lerp(SEx, Ex, 0.5);
var SEEy = lerp(SEy, Ey, 0.5);
var NWWx = lerp(NWx, Wx, 0.5);
var NWWy = lerp(NWy, Wy, 0.5);
var SWWx = lerp(SWx, Wx, 0.5);
var SWWy = lerp(SWy, Wy, 0.5);
// This is where we work out the colours. lerpColor really
// doesn't seem to be working for me in p5js otherwise
// I'd be using HSB to find nice colour palettes. Until
// then, adapt the code below to fancy up the colours.
var r1 = int(random(0, 255));
var g1 = int(random(0, 255));
var b1 = int(random(0, 255));
var r2 = int(random(0, 255));
var g2 = int(random(0, 255));
var b2 = int(random(0, 255));
var c1 = color(r1, g1, b1);
var c4 = color(r2, g2, b2);
var c2 = lerpColor(c1, c4, 0.33);
var c3 = lerpColor(c1, c4, 0.66);
/*
var c1 = color(int(random(0,100)), 80, 100);
var c4 = color(int(random(0,100)),20,100);
var c2 = lerpColor(c1, c4, 0.33);
var c3 = lerpColor(c1, c4, 0.66);
*/
fill(c1);
push();
translate((windowWidth / 2) + x, (windowHeight / 2) + y);
var newAngle = int(random(0, 6)) * 60;
rotate(radians(newAngle));
// Now draw the 4 different slices of the hex
stroke(c1);
fill(c1);
beginShape();
vertex(NEx, NEy);
vertex(NEEx, NEEy);
vertex(NWWx, NWWy);
vertex(NWx, NWy);
vertex(NEx, NEy);
endShape();
stroke(c2);
fill(c2);
beginShape();
vertex(NEEx, NEEy);
vertex(Ex, Ey);
vertex(Wx, Wy);
vertex(NWWx, NWWy);
vertex(NEEx, NEEy);
endShape();
stroke(c3);
fill(c3);
beginShape();
vertex(Ex, Ey);
vertex(SEEx, SEEy);
vertex(SWWx, SWWy);
vertex(Wx, Wy);
vertex(Ex, Ey);
endShape();
stroke(c4);
fill(c4);
beginShape();
vertex(SEEx, SEEy);
vertex(SEx, SEy);
vertex(SWx, SWy);
vertex(SWWx, SWWy);
vertex(SEEx, SEEy);
endShape();
pop();
}
function makeGrid(radius) {
// This is going to hold the array of hexPoints that we return
var hexPoints = [];
// first we can work out the total width of a hexagon
// this will be twice the radius
var hex_width = radius * 2;
// Now we need to work out the height, to do that we
// need to move the second point straight up and then
// rotate it
var NEx = -radius * sin(radians(-30));
var NEy = -radius * cos(radians(-30));
var hex_height = abs(NEy * 2);
var leftover_width = (windowWidth / 2) - (hex_width / 2);
var number_of_hexes = ceil(leftover_width / hex_width);
var start_x_offset = -(number_of_hexes * hex_width) - (hex_width / 2);
var leftover_height = (windowHeight / 2) - (hex_height / 2);
number_of_hexes = ceil(leftover_height / (hex_height));
var start_y_offset = -(number_of_hexes * hex_height) - (hex_height / 2);
var counter = 0;
for (var y = start_y_offset; y <= abs(start_y_offset) + (hex_height); y += (hex_height / 2)) {
var shift_hex = false;
if (counter % 2 == 0) {
shift_hex = true;
}
for (var x = start_x_offset; x <= abs(start_x_offset); x += hex_width * 1.5) {
var newX = x;
if (shift_hex) {
newX += hex_width * 0.75;
}
hexPoints.push(new HexPoint(newX, y));
}
counter++;
}
return hexPoints;
}
// This is just a point that's the center of a hex
function HexPoint(x, y) {
this.x = x;
this.y = y;
}
Also see: Tab Triggers