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 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 id="instructions">
Press SPACEBAR / TAP screen to randomize
</div>
html, body {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: Helvetica, Calibri, Roboto, Open Sans, sans-serif;
-webkit-backface-visibility: hidden;
background: black;
color: #999;
}
canvas {
position: fixed;
z-index: 1;
}
.dg.ac {
z-index: 3 !important;
}
#instructions {
position: fixed;
z-index: 2;
bottom : 10px;
left : 50%;
transform : translateX(-50%);
background : rgba(0,0,0,0.6);
padding : 3px 10px;
font-family: helvetica neue, arial, sans-serif;
font-size: 16px;
font-weight: 300;
letter-spacing: 2px;
color : #fff;
}
const colors = [
'#a7fcc7',
'#90d0fb',
'#6970f9',
'#cb73f9',
'#d977f9',
'#75fba7',
'#52b5f9',
'#1420f5',
'#ad24f6',
'#c42af6',
'#4ba16b',
'#34749f',
'#0c149d',
'#6e179d',
'#7d1b9d',
'#e03b51',
'#ffffff',
'#00ffff',
'#ff00f4',
];
const colorsBright = [
'#a7fcc7',
'#90d0fb',
'#6970f9',
'#cb73f9',
'#d977f9',
];
const colorsDark = [
'#4ba16b',
'#34749f',
'#0c149d',
'#6e179d',
'#7d1b9d',
];
const vars = getRandomVars();
console.log('GRID SPACING:', vars.gridSpacing);
var canvas = document.createElement('canvas');
document.body.append(canvas);
var ctx = canvas.getContext('2d');
var W = canvas.width = window.innerWidth;
var H = canvas.height = window.innerHeight;
// var x = Math.floor(Math.random() * W);
// var y = Math.floor(Math.random() * H);
const raf = window.requestAnimationFrame;
let polys = [];
let numRows = H / (vars.gridSpacing);
let numCols = W / (vars.gridSpacing);
console.log('numRows:', numRows);
let rowIndex = 0;
let colIndex = 0;
window.addEventListener('resize', handleResize);
document.body.onkeyup = function(e) {
if (e.keyCode === 32) {
randomizeVars();
}
};
canvas.addEventListener('click', randomizeVars);
function handleResize() {
W = canvas.width = window.innerWidth;
H = canvas.height = window.innerHeight;
numRows = H / (vars.gridSpacing);
numCols = W / (vars.gridSpacing);
createPolys();
}
function getRandomVars() {
return {
sides: 3 + Math.floor(Math.random() * 3),
speed: Math.random() * 0.08,
speedIncrement: 0.011, // 0.001 - (Math.random() * (0.001 * 2)),
radius: 10 + (Math.random() * 50),
gridSpacing: 20 + (Math.random() * 20),
lineWidth: 0.5 + Math.random() * 3,
color1: colorsBright[Math.floor(Math.random() * colorsBright.length)],
color2: colorsDark[Math.floor(Math.random() * colorsDark.length)],
animateColor1: true,
animateColor2: true,
rotationOffsetRow: 20,
flipOffsetRow: Math.random() < 0.5,
rotationOffsetCol: 30,
flipOffsetCol: Math.random() < 0.5,
scaleFactorMin: Math.random() * 0.25,
scaleStartAngle: Math.random() < 0.5,
}
};
function randomizeVars() {
window.cancelAnimationFrame(color1Raf);
window.cancelAnimationFrame(color2Raf);
const newVars = getRandomVars();
vars.sides = newVars.sides;
vars.speed = newVars.speed;
vars.speedIncrement = newVars.speedIncrement;
// vars.minRadius = newVars.minRadius;
// vars.maxRadius = radiusIncrement = newVars.maxRadius;
vars.glowColor = newVars.glowColor;
vars.animateGlowColor = newVars.animateGlowColor;
vars.glowSize = newVars.glowSize;
vars.strokeColor = newVars.strokeColor;
vars.animateStrokeColor = newVars.animateStrokeColor;
vars.lineWidth = newVars.lineWidth;
vars.text = newVars.text;
vars.textPadding = newVars.textPadding;
vars.textAlign = newVars.textAlign;
vars.textBgColor = newVars.textBgColor;
vars.fontColor = newVars.fontColor;
vars.fontSize = newVars.fontSize;
vars.color1 = newVars.color1;
vars.color2 = newVars.color2;
vars.animateColor1 = newVars.animateColor1;
vars.animateColor2 = newVars.animateColor2;
vars.radius = newVars.radius;
vars.rotationOffsetRow = newVars.rotationOffsetRow;
vars.rotationOffsetCol = newVars.rotationOffsetCol;
vars.flipOffsetRow = newVars.flipOffsetRow;
vars.flipOffsetCol = newVars.flipOffsetCol;
vars.scaleFactorMin = newVars.scaleFactorMin;
vars.scaleStartAngle = newVars.scaleStartAngle;
vars.gridSpacing = newVars.gridSpacing;
if (vars.animateColor1) {
animColor1();
}
if (vars.animateColor2) {
animColor2();
}
handleResize();
}
// dat.gui controls
const gui = new dat.GUI({ width: 350 });
const polyFolder = gui.addFolder('Polygon Drawing');
const controlSides = polyFolder.add(vars, 'sides', 3, 15).step(1).listen();
const controlRadius = polyFolder.add(vars, 'radius', 3, 80).listen();
const controlGridSpacing = polyFolder.add(vars, 'gridSpacing', 1, 100).listen();
const speedFolder = gui.addFolder('Rotation Speed');
speedFolder.add(vars, 'speed', 0.001, 0.09).listen();
speedFolder.add(vars, 'speedIncrement', 0.001, 0.09).listen();
speedFolder.add(vars, 'rotationOffsetRow', 0, 90).step(0.01).listen();
speedFolder.add(vars, 'rotationOffsetCol', 0, 90).step(0.01).listen();
speedFolder.add(vars, 'flipOffsetRow').listen();
speedFolder.add(vars, 'flipOffsetCol').listen();
const scaleFolder = gui.addFolder('Scaling');
scaleFolder.add(vars, 'scaleFactorMin', 0, 1).listen();
scaleFolder.add(vars, 'scaleStartAngle').listen();
const colorFolder = gui.addFolder('Colors');
colorFolder.addColor(vars, 'color1').listen();
colorFolder.addColor(vars, 'color2').listen();
const controlAnimateColor1 = colorFolder.add(vars, 'animateColor1').listen();
const controlAnimateColor2 = colorFolder.add(vars, 'animateColor2').listen();
colorFolder.add(vars, 'lineWidth', 0, 15).listen();
gui.close();
polyFolder.open();
speedFolder.open();
scaleFolder.open();
colorFolder.open();
controlGridSpacing.onChange(value => {
handleResize();
});
controlAnimateColor1.onChange(value => {
window.cancelAnimationFrame(color1Raf);
if (value) {
animColor1();
}
});
controlAnimateColor2.onChange(value => {
window.cancelAnimationFrame(color2Raf);
if (value) {
animColor2();
}
});
const defaultPolyOptions = {
x: 0,
y: 0,
sides: vars.sides,
radius: 10,
rotation: 0,
startAngle: 0,
anticlockwise: false,
fill: '#00FF00',
stroke: '#FF0000',
};
// Polygon class
function Polygon() {}
function addPoly(p_options) {
const options = Object.assign({}, defaultPolyOptions, p_options);
var poly = new Polygon;
poly.x = options.x;
poly.y = options.y;
poly.sides = options.sides;
poly.radius = options.radius;
poly.rotation = options.rotation;
poly.startAngle = options.startAngle;
poly.fill = options.fill;
poly.stroke = options.stroke;
polys.push(poly);
}
function createPolys() {
polys = [];
const rowOffset = W / numCols;
const colOffset = H / numRows;
// maxRadius = radius;
// rotationOffsetRow = vars.speedIncrement * 30;
// rotationOffsetCol = vars.speedIncrement * 20;
for (var i = 0; i < numCols; i++) {
for (var m = 0; m < numRows; m++) {
const startAngle = 360; // starts at 360 instead of 0 so that flipOffset doesn't create polys that are too big
// console.log(i, m, startAngle);
addPoly({
x: i * rowOffset,
y: m * colOffset,
rowIndex: m,
colIndex: i,
startAngle,
fill: 'transparent',
});
}
}
}
function addPoly(p_options) {
const options = Object.assign({}, defaultPolyOptions, p_options);
var poly = new Polygon;
poly.x = options.x;
poly.y = options.y;
poly.sides = options.sides;
poly.radius = options.radius;
poly.rotation = options.rotation;
poly.startAngle = options.startAngle;
poly.fill = options.fill;
poly.stroke = options.stroke;
poly.rowIndex = options.rowIndex;
poly.colIndex = options.colIndex;
polys.push(poly);
}
// Draw and animate polygons
function draw() {
const startColor = hexToRgb(vars.color1);
const targetColor = hexToRgb(vars.color2);
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
for (var i = 0; i < polys.length; i++) {
const poly = polys[i];
// rotate offset
flipRow = vars.flipOffsetRow ? -1 : 1;
flipCol = vars.flipOffsetCol ? -1 : 1;
rotationOffsetRow = vars.speedIncrement * vars.rotationOffsetRow * flipRow;
rotationOffsetCol = vars.speedIncrement * vars.rotationOffsetCol * flipCol;
rotationOffset = (rotationOffsetRow * poly.rowIndex) + (rotationOffsetCol * poly.colIndex);
poly.rotationOffset = rotationOffset;
const colorPercent = Math.abs(180 - radians_to_degrees(poly.startAngle + rotationOffset) % 360) / 180;
var r = lerp(startColor.r, targetColor.r, colorPercent);
var g = lerp(startColor.g, targetColor.g, colorPercent);
var b = lerp(startColor.b, targetColor.b, colorPercent);
poly.colorPercent = colorPercent;
poly.strokeColor = rgbToHex(r, g, b);
drawPolygon(poly);
poly.startAngle += vars.speed; // + speedModifier; // - (vars.speedIncrement * i);
}
}
function radians_to_degrees(radians) {
var pi = Math.PI;
return radians * (180/pi);
}
function drawPolygon(poly) {
const {
x,
y,
sides,
// radius,
startAngle,
anticlockwise,
stroke,
fill,
strokeColor,
rotationOffset,
colorPercent,
} = poly;
// do not draw invalid polygon
if (sides < 3) return;
ctx.beginPath();
ctx.strokeStyle = strokeColor; // vars.strokeColor;
ctx.lineWidth = vars.lineWidth;
ctx.fillStyle = fill;
ctx.shadowColor = vars.glowColor;
ctx.shadowBlur = vars.glowSize;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
var a = (Math.PI * 2) / vars.sides;
a = anticlockwise ? -a : a;
ctx.save();
ctx.translate(x + (vars.radius * 0.25), y + (vars.radius * 0.25));
// rotate offset
// rotationOffsetRow = vars.speedIncrement * 30;
// rotationOffsetCol = vars.speedIncrement * 20;
// rotationOffset = (rotationOffsetRow * poly.rowIndex) + (rotationOffsetCol * poly.colIndex);
ctx.rotate(startAngle + rotationOffset);
const rotateScale = vars.scaleFactorMin + ((1 - vars.scaleFactorMin) * colorPercent);
const startAngleModifier = vars.scaleStartAngle ? rotateScale : 1;
ctx.moveTo(vars.radius * startAngleModifier, 0);
// ctx.drawImage(img, x/sides, y/sides, radius, radius);
for (var i = 1; i < vars.sides; i++) {
ctx.lineTo(vars.radius * rotateScale * Math.cos(a * i), vars.radius * rotateScale * Math.sin(a * i));
}
ctx.closePath();
ctx.stroke();
ctx.fill();
ctx.restore();
}
function animate() {
draw();
raf(animate);
}
// Color Tweening
let targetColor1;
let color1StartRGB;
let color1TargetRGB;
const color1AnimDuration = 90;
let color1AnimIndex = 0;
let color1Raf = null;
function animColor1() {
targetColor1 = colors[Math.floor(Math.random() * colors.length)];
while (targetColor1 === vars.color1) {
targetColor1 = colors[Math.floor(Math.random() * colors.length)];
}
color1StartRGB = hexToRgb(vars.color1);
color1TargetRGB = hexToRgb(targetColor1);
color1AnimIndex = 0;
color1Raf = raf(stepColor1Animation);
}
function stepColor1Animation() {
color1AnimIndex += 1;
const progress = color1AnimIndex / color1AnimDuration;
var r = lerp(color1StartRGB.r, color1TargetRGB.r, progress);
var g = lerp(color1StartRGB.g, color1TargetRGB.g, progress);
var b = lerp(color1StartRGB.b, color1TargetRGB.b, progress);
vars.color1 = rgbToHex(r, g, b);
if (progress < 1) {
color1Raf = raf(stepColor1Animation);
} else {
if (vars.animateColor1) {
animColor1();
}
}
}
let targetColor2;
let color2StartRGB;
let color2TargetRGB;
const color2AnimDuration = 90;
let color2AnimIndex = 0;
let color2Raf = null;
function animColor2() {
targetColor2 = colors[Math.floor(Math.random() * colors.length)];
while (targetColor2 === vars.color2) {
targetColor2 = colors[Math.floor(Math.random() * colors.length)];
}
color2StartRGB = hexToRgb(vars.color2);
color2TargetRGB = hexToRgb(targetColor2);
color2AnimIndex = 0;
color2Raf = raf(stepColor2Animation);
}
function stepColor2Animation() {
color2AnimIndex += 1;
const progress = color2AnimIndex / color2AnimDuration;
var r = lerp(color2StartRGB.r, color2TargetRGB.r, progress);
var g = lerp(color2StartRGB.g, color2TargetRGB.g, progress);
var b = lerp(color2StartRGB.b, color2TargetRGB.b, progress);
vars.color2 = rgbToHex(r, g, b);
if (progress < 1) {
color2Raf = raf(stepColor2Animation);
} else {
if (vars.animateColor2) {
animColor2();
}
}
}
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function lerp(start, end, progress) {
progress = progress < 0 ? 0 : progress;
progress = progress > 1 ? 1 : progress;
return Math.round(start + (end - start) * progress);
}
// Instantiate polygons
createPolys();
animate();
if (vars.animateColor1) {
animColor1();
}
if (vars.animateColor2) {
animColor2();
}
Also see: Tab Triggers