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="myCanvas"></canvas>
<!-- Some stuff you can do I guess? -->
<!--
1 / 2 - Toggle Squiggly Mode
q / w - Toggle "Faster Fade" Mode
a / s - Toggle "I Don't Even Know" Mode
z - Increase line width
x - Decrease line width
g - Probably don't press this
-->
body {
background-color: #171717;
}
#myCanvas {
background-color: #1f1f1f;
}
const LINE_DURATION = 2;
const LINE_WIDTH_START = 5;
$(document).ready(function() {
enableDrawingCanvas();
resizeCanvas(window.innerWidth, window.innerHeight);
$(document).keypress(function(event) {
if (event.key === "1") mode = 1;
if (event.key === "2") mode = 2;
if (event.key == "q") spread = 1;
if (event.key == "w") spread = 2;
if (event.key == "a") pathMode = 1;
if (event.key == "s") pathMode = 2;
if (event.key == "z") {
if (lineWidthStart < 100) lineWidthStart++;
}
if (event.key == "x") {
if (lineWidthStart > 1) lineWidthStart--;
}
if (event.key == "g") {
if (canvas === undefined)
enableDrawingCanvas();
var poopx = new Array();
var poopy = new Array();
function spawn() {
var gap = 10;
var rows = 10;
var cols = 3;
var canvas = $('#myCanvas');
var width = canvas.width();
var height = canvas.height();
var count = 0;
for (var i = (width / 2) - (gap * rows); i < (width / 2) + (gap * rows); i = i + gap) {
if (i % (gap * 2) === 0) {
for (var j = (height / 2) - (gap * cols); j < (height / 2) + (gap * cols); j = j + gap) {
poopx.push(i);
poopy.push(j);
}
} else {
for (var j = (height / 2) + (gap * cols) - gap; j > (height / 2) - (gap * cols) - gap; j = j - gap) {
poopx.push(i);
poopy.push(j);
}
}
}
}
asd();
function asd() {
if (poopx.length <= 0) {
spawn();
}
var x = poopx.pop();
var y = poopy.pop();
addPoint(x, y);
setTimeout(function() {
asd()
}, 10);
}
}
});
});
//////////////////////////
// Variable definitions //
//////////////////////////
var active = true;
var canvas;
var context;
var newWidth = 1000;
var newHeight = 800;
var mode = 1;
var pathMode = 1;
var spread = 2;
var lineColor = 'rgb(237, 184, 131)';
var lineDuration = LINE_DURATION;
var lineFadeLinger = 1;
var lineWidthStart = LINE_WIDTH_START;
var fadeDuration = 50;
var drawEveryFrame = 1; // Only adds a Point after these many 'mousemove' events
var clickCount = 0;
var frame = 0;
var flipNext = true;
var points = new Array();
///////////////////////
// Program functions //
///////////////////////
// Find canvas reference & enable listeners
function enableDrawingCanvas() {
if (canvas === undefined) {
canvas = document.getElementById('myCanvas');
context = canvas.getContext('2d');
enableListeners();
init();
}
}
// Initialize animation start
function init() {
draw();
}
// Draw current state
function draw() {
if (active) {
animatePoints();
window.requestAnimFrame(draw);
}
}
// Update mouse positions
function animatePoints() {
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
var duration = lineDuration * 1000 / 60;
var point, lastPoint;
if (pathMode === 2) {
context.beginPath();
}
for (var i = 0; i < points.length; i++) {
point = points[i];
if (points[i - 1] !== undefined) {
lastPoint = points[i - 1];
} else {
lastPoint = points[i];
}
point.lifetime += 1;
if (point.lifetime > duration) {
points.splice(i, 1);
continue;
}
// Begin drawing stuff!
var inc = (point.lifetime / duration); // 0 to 1 over lineDuration
var dec = 1 - inc;
var spreadRate;
if (spread === 1) {
spreadRate = lineWidthStart / (point.lifetime * 2);
} // Lerp Decrease
if (spread === 2) {
spreadRate = lineWidthStart * (1 - inc);
} // Linear Decrease
var fadeRate = dec;
//context.strokeStyle = lineColor;
context.lineJoin = "round";
context.lineWidth = spreadRate;
context.strokeStyle = 'rgb(' + Math.floor(255) + ',' +
Math.floor(200 - (255 * dec)) + ',' +
Math.floor(200 - (255 * inc)) + ')';
var distance = Point.distance(lastPoint, point);
var midpoint = Point.midPoint(lastPoint, point);
var angle = Point.angle(lastPoint, point);
if (pathMode === 1) {
context.beginPath();
}
if (mode === 1) {
context.arc(midpoint.x, midpoint.y, distance / 2, angle, (angle + Math.PI), point.flip);
}
if (mode === 2) {
context.moveTo(lastPoint.x, lastPoint.y);
context.lineTo(point.x, point.y);
}
if (pathMode === 1) {
context.stroke();
context.closePath();
}
}
if (pathMode === 2) {
context.stroke();
context.closePath();
}
//if (points.length > 0) { console.log(spreadRate + "|" + points.length + " points alive."); }
}
function addPoint(x, y) {
flipNext = !flipNext;
var point = new Point(x, y, 0, flipNext);
points.push(point);
}
//////////////////////////////
// Less Important functions //
//////////////////////////////
// RequestAnimFrame definition
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
// Update canvas dimensions based on input
function resizeCanvas(w, h) {
if (context !== undefined) {
context.canvas.width = w;
context.canvas.height = h;
newWidth = w;
newHeight = h;
}
}
// Listeners for mouse and touch events
function enableListeners() {
//********* Mouse Listeners *********//
$('#myCanvas').on('mousemove', function(e) {
if (frame === drawEveryFrame) {
addPoint(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
frame = 0;
}
frame++;
});
$('#myCanvas').on('mouseover', function(e) {});
$('#myCanvas').on('mouseleave', function(e) {});
//********* Touch Listeners *********//
$('#myCanvas').on('touchstart', function(e) {
var touch = e.touches[0];
});
$('#myCanvas').on('touchmove', function(e) {
var touch = e.touches[0];
});
$('#myCanvas').on('touchend', function(e) {});
}
// POINT CLASS
// Cartersian location of where mouse location
// was previously at.
// Used to draw arcs between Points.
var Point = class Point {
// Define class constructor
constructor(x, y, lifetime, flip) {
this.x = x;
this.y = y;
this.lifetime = lifetime;
this.flip = flip;
}
// Get the distance between a & b
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
// Get the mid point between a & b
static midPoint(a, b) {
const mx = a.x + (b.x - a.x) * 0.5;
const my = a.y + (b.y - a.y) * 0.5;
return new Point(mx, my);
}
// Get the angle between a & b
static angle(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.atan2(dy, dx);
}
// Simple getter for printing
get pos() {
return this.x + "," + this.y;
}
}
Also see: Tab Triggers