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.
<main>
<div class="content">
<svg id="canvas"></svg>
</div>
<div class="sidebar">
<div class="row">
<div class="col-xs-12">
<h3>Start Doodling...</h3>
</div>
</div>
<div class="row control">
<div class="col-xs-12">
<button id="clear" class="btn btn-default">Clear</button>
</div>
</div>
<!--<div class="row control">
<div class="col-xs-12">
<hr>
<div class="row slider-row">
<div class="col-xs-12">
<div class="pull-left">Tolerance</div>
<div class="pull-left">
<input id="slider" type="range" min="0" max="10" value="10" step="1" />
</div>
<div id="smoothing" class="pull-left">5.4</div>
</div>
</div>
</div>
</div>-->
<!--<div class="row control">
<div class="col-xs-12">
<table class="table-condensed">
<thead>
<tr>
<th>Path</th>
<th>Initial</th>
<th>Reduced</th>
</tr>
</thead>
<tbody id="table">
</tbody>
</table>
</div>
</div>-->
</div>
</main>
html, body {
height: 100%;
width: 100%;
}
body {
overflow: hidden;
user-select: none;
}
main {
visibility: hidden;
height: 100%;
}
#canvas {
height: 100%;
width: 100%;
cursor: crosshair;
}
.slider-row {
margin-top: 10px;
div {
padding-right: 10px;
}
input {
min-width: 150px;
}
}
path, polyline {
fill: none;
stroke-width: 1;
stroke-linecap: round;
stroke-miterlimit: 10;
}
.control {
}
table {
margin-top: 15px;
}
.table-condensed > tbody >tr > td,
.table-condensed > tbody >tr > th,
.table-condensed > thead >tr > td,
.table-condensed > thead >tr > th {
padding: 1px 4px;
}
.sidebar {
border: 1px solid #eee;
position: absolute;
height: 100%;
z-index: 1;
top: 0;
left: 0;
padding: 5px 15px;
background: white;
&.drawing {
pointer-events: none;
}
.row:first-child {
margin-bottom: 6px;
}
}
.content {
position: relative;
height: 100%;
}
hr {
margin-bottom: 0;
}
/////////
.sidebar {
height: initial;
border: none;
background: transparent;
}
console.clear();
TweenLite.defaultEase = Power1.easeInOut;
var xmlns = "http://www.w3.org/2000/svg";
var log = console.log.bind(console);
var select = document.querySelector.bind(document);
var sidebar = select(".sidebar");
var slider = select("#slider");
var canvas = select("#canvas");
var clear = select("#clear");
var table = select("#table");
var smooth = select("#smoothing");
function demo() {
var count = 0;
var paths = [];
var path = null;
var shape = new Spline({ stroke: "#cc0066", strokeWidth: 3 });
var rebuild = _.debounce(rebuildTimeline, 150);
//var timeline = new TimelineMax({ repeat: -1, yoyo: true });
//var tolerance = getTolerance();
var timeline = new TimelineMax({ repeat: 0, yoyo: true });
var tolerance = 20;
clear.addEventListener("click", clearDoodles);
//slider.addEventListener("input", updateTolerance);
canvas.addEventListener("mousedown", startDrawing);
TweenLite.set("main", { autoAlpha: 1 });
// START DRAWING =========================================================
function startDrawing(event) {
timeline.pause(0);
shape.hide(true);
count++;
path = new Polyline({ stroke: "#cc0066" }, count);
path.last = Point.parse(event);
paths.push(path);
sidebar.classList.add("drawing");
canvas.addEventListener("mousemove", updateDrawing);
canvas.addEventListener("mouseup", stopDrawing);
canvas.addEventListener("mouseleave", stopDrawing);
}
// UPDATE DRAWING ========================================================
function updateDrawing(event) {
path.addPoint(Point.parse(event));
}
// STOP DRAWING ==========================================================
function stopDrawing(event) {
sidebar.classList.remove("drawing");
canvas.removeEventListener("mousemove", updateDrawing);
canvas.removeEventListener("mouseup", stopDrawing);
canvas.removeEventListener("mouseleave", stopDrawing);
path.simplify(tolerance);
shape.solve(path.reduced);
animate();
}
// ANIMATE ===============================================================
function animate() {
path.hide();
shape.show();
if (count === 1) {
shape.update();
timeline.set(shape, { attr: { d: shape.path }});
} else {
//timeline.to(shape, 1, { morphSVG: shape.path }).play();
timeline.to(shape, 1, { morphSVG: {shape: shape.path, shapeIndex: 0 }}).play();
}
}
// REBUILD TIMELINE ======================================================
function rebuildTimeline() {
timeline.pause(0).clear();
shape.hide();
tolerance = getTolerance();
_.reduce(paths, (tween, path, index) => {
path.simplify(tolerance);
shape.solve(path.reduced);
if (!index) {
shape.update();
timeline.set(shape, { attr: { d: shape.path }});
} else {
timeline.to(shape, 1, { morphSVG: shape.path }).play();
}
return tween;
}, timeline);
shape.show();
timeline.play(0);
}
// GET TOLERANCE =========================================================
function getTolerance() {
var value = slider.value;
smooth.textContent = value;
return value;
}
// UPDATE TOLERANCE ======================================================
function updateTolerance() {
tolerance = getTolerance();
timeline.pause();
if (paths.length) rebuild();
}
// CLEAR DOODLES =========================================================
function clearDoodles() {
timeline.pause(0).clear();
shape.clear();
paths.forEach(path => path.destroy());
paths = [];
count = 0;
//table.innerHTML = "";
}
}
//
// POINT
// ========================================================================
class Point {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
static parse(event) {
return new Point(event.clientX, event.clientY);
}
}
//
// VECTOR
// ========================================================================
class Vector {
constructor(x = 0, y = 0) {
var point = x.x && x.y ? x : { x, y };
this.x = point.x;
this.y = point.y;
}
get magnitude() { return Math.sqrt(this.x * this.x + this.y * this.y); }
set magnitude(m) {
var uv = this.normalize();
this.x = uv.x * m;
this.y = uv.y * m;
}
static fromPoints(p1, p2) {
return new Vector(p2.x - p1.x, p2.y - p1.y);
}
cross(vector) {
return this.x * vector.y - this.y * vector.x;
}
dot(vector) {
return this.x * vector.x + this.y * vector.y;
}
add(vector) {
return new Vector(this.x + vector.x, this.y + vector.y);
}
subtract(vector) {
return new Vector(this.x - vector.x, this.y - vector.y);
}
multiply(scalar) {
return new Vector(this.x * scalar, this.y * scalar);
}
divide(scalar) {
return new Vector(this.x / scalar, this.y / scalar);
}
normalize() {
var v = new Vector();
var m = this.magnitude;
v.x = this.x / m;
v.y = this.y / m;
return v;
}
unit() {
return this.divide(this.magnitude);
}
perp() {
return new Vector(-this.y, this.x)
}
perpendicular(vector) {
return this.subtract(this.project(vector));
}
project(vector) {
var percent = this.dot(vector) / vector.dot(vector);
return vector.multiply(percent);
}
reflect(axis) {
var vdot = this.dot(axis);
var ldot = axis.dot(axis);
var ratio = vdot / ldot;
var v = new Vector();
v.x = 2 * ratio * axis.x - this.x;
v.y = 2 * ratio * axis.y - this.y;
return v;
}
}
//
// SPLINE
// ========================================================================
class Spline {
constructor(config = {}) {
this.group = createSVG("g", canvas, { autoAlpha: 0 });
this.node = createSVG("path", canvas, config);
this[0] = this.node;
this.length = 1;
this.paths = [];
}
get path() { return this.paths[this.paths.length - 1].data; }
hide(showGroup = false) {
TweenLite.set(this.node, { autoAlpha: 0 });
TweenLite.set(this.group, { autoAlpha: showGroup ? 1 : 0 });
return this;
}
show() {
TweenLite.set(this.node, { autoAlpha: 1 });
TweenLite.set(this.group, { autoAlpha: 0 });
return this;
}
set(vars) {
TweenLite.set(this.node, vars);
return this;
}
clear() {
var group = this.group;
while (group.lastChild) {
group.removeChild(group.lastChild);
}
this.paths = [];
this.hide();
return this;
}
addPath(data) {
var paths = this.paths;
var props = { stroke: "#ddd", attr: { d: data }};
var node = createSVG("path", this.group, props);
var alpha = 0.95;
paths.push({ node, data });
var i = paths.length;
while (i--) {
TweenLite.set(paths[i].node, { autoAlpha: alpha });
alpha = Math.max(alpha - 0.3, 0);
}
return this;
}
update() {
this.node.setAttribute("d", this.path);
return this;
}
solve(data) {
var size = data.length;
var last = size - 4;
var path = `M${data[0]},${data[1]}`;
for (var i = 0; i < size - 2; i +=2) {
var x0 = i ? data[i - 2] : data[0];
var y0 = i ? data[i - 1] : data[1];
var x1 = data[i + 0];
var y1 = data[i + 1];
var x2 = data[i + 2];
var y2 = data[i + 3];
var x3 = i !== last ? data[i + 4] : x2;
var y3 = i !== last ? data[i + 5] : y2;
var cp1x = (-x0 + 6 * x1 + x2) / 6;
var cp1y = (-y0 + 6 * y1 + y2) / 6;
var cp2x = (x1 + 6 * x2 - x3) / 6;
var cp2y = (y1 + 6 * y2 - y3) / 6;
path += `C ${cp1x},${cp1y} ${cp2x},${cp2y} ${x2},${y2}`;
}
this.addPath(path);
return path;
}
}
//
// POLYLINE
// ========================================================================
class Polyline {
constructor(config = {}, count) {
this[0] = this.node = createSVG("polyline", canvas);
// Table values
this.data = {};
//if (count) this.data = createRow(count);
this.length = 1;
this.last = null;
this.points = [];
this.initial = [];
this.reduced = [];
this.set(config);
}
addPoint(point) {
if (this.last.x !== point.x || this.last.y !== point.y) {
this.last = point;
this.points.push(point);
this.initial.push(point.x);
this.initial.push(point.y);
this.data.initial = this.points.length;
this.node.setAttribute("points", this.initial);
}
return this;
}
destroy() {
canvas.removeChild(this.node);
this.last = null;
this.points = null;
this.initial = null;
this.reduced = null;
}
hide() {
this.set({ autoAlpha: 0 });
return this;
}
show() {
this.set({ autoAlpha: 1 });
return this;
}
updatePoints() {
this.data.reduced = this.reduced.length / 2;
this.node.setAttribute("points", this.reduced);
return this;
}
set(vars) {
TweenLite.set(this.node, vars);
return this;
}
simplify(tolerance = 10) {
var points = this.points;
var length = points.length;
if (length < 3) {
if (!length) {
this.addPoint({ x: this.last.x, y: this.last.y + 0.2 });
}
this.addPoint({ x: this.last.x + 0.2, y: this.last.y });
this.reduced = _.slice(this.initial);
this.updatePoints();
return;
}
function acceptPoint() {
acceptedPoint = previousPoint;
result.push(acceptedPoint);
cache = [];
}
var previousPoint = points[0];
var acceptedPoint = points[0];
var currentPoint = points[1];
var currentVector = Vector.fromPoints(previousPoint, currentPoint);
var previousVector;
var result = [points[0]];
var cache = [];
for (var i = 2; i < length; i++) {
previousPoint = currentPoint;
currentPoint = points[i];
previousVector = currentVector;
currentVector = Vector.fromPoints(previousPoint, currentPoint);
if (previousVector.dot(currentVector) < 0) {
acceptPoint();
} else {
var candidate = Vector.fromPoints(acceptedPoint, currentPoint);
var lastVector = Vector.fromPoints(acceptedPoint, previousPoint)
cache.push(lastVector);
for (var j = 0; j < cache.length; j++) {
var perp = cache[j].perpendicular(candidate);
if (perp.magnitude > tolerance) {
acceptPoint();
break;
}
}
}
}
result.push(points[points.length - 1]);
this.reduced = _.reduce(result, (path, point) => {
path.push(point.x);
path.push(point.y);
return path;
}, []);
this.updatePoints();
return this;
}
}
// CREATE ROW ============================================================
function createRow(id) {
var row = createElement("tr", table);
_.times(3, i => createElement("td", row, !i ? id : "--"));
return {
set initial(value) { row.children[1].textContent = value; },
set reduced(value) { row.children[2].textContent = value; }
};
}
// CREATE SVG ============================================================
function createSVG(type, parent, config) {
var node = document.createElementNS(xmlns, type);
parent.appendChild(node);
if (config) TweenLite.set(node, config);
return node;
}
// CREATE ELEMENT ========================================================
function createElement(type, parent, text) {
var node = document.createElement(type);
if (text) node.innerHTML = text;
parent.appendChild(node);
return node;
}
// Start it up...
demo();
Also see: Tab Triggers