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.
import Two from 'https://cdn.skypack.dev/two.js@latest';
var two = new Two({
type: Two.Types.svg,
fullscreen: true,
autostart: true
}).appendTo(document.body);
// The cursor, matched with your mouse
var mouse = new Two.Vector();
mouse.radius = 4;
mouse.radiusSquared = Math.pow(mouse.radius, 2);
mouse.dragging = false;
mouse.intersection = null;
mouse.selected = new Two.Circle(0, 0, 2);
mouse.selected.stroke = '#00AEFF';
mouse.selected.scale = 2;
mouse.selected.visible = false;
var content = two.makeGroup(); // Everything that is drawn black
var interaction = two.makeGroup(); // All the blue / pink interactive elements
// The blue path highlight
var selection = new Two.Path();
selection.stroke = '#00AEFF';
selection.noFill();
selection.automatic = false;
selection.visible = false;
interaction.add(selection);
// The pink control handles
var controls = new Two.Group();
controls.left = new Two.Circle(0, 0, 2);
controls.right = new Two.Circle(0, 0, 2);
controls.line = new Two.Path([
new Two.Anchor(),
new Two.Anchor(),
new Two.Anchor()
]);
controls.left.translation.bind(Two.Events.Types.change, updateControlHandles);
controls.right.translation.bind(Two.Events.Types.change, updateControlHandles);
controls.add(controls.line, controls.left, controls.right);
controls.stroke = '#FF00AE';
controls.anchor = null;
interaction.add(controls);
// A list of all the editable anchor points
var points = new Two.Points();
points.size = 4;
points.stroke = '#00AEFF';
interaction.add(points, mouse.selected);
var path; // Used to reference the currently selected path
var domElement = two.renderer.domElement;
domElement.addEventListener('mousedown', mousedown, false);
domElement.addEventListener('dblclick', doubleclick, false);
domElement.addEventListener('mousemove', mousemove, false);
function create() {
path = new Two.Path();
path.linewidth = 2;
path.noFill();
path.automatic = false;
points.vertices = path.vertices;
selection.vertices = path.vertices;
content.add(path);
}
function add(x, y) {
var anchor = new Two.Anchor(x, y, 0, 0, 0, 0);
anchor.command = Two.Commands[path.vertices.length > 0 ? 'curve' : 'move'];
path.vertices.push(anchor);
controls.anchor = anchor;
controls.left.translation.copy(anchor);
controls.right.translation.copy(anchor);
return anchor;
}
function remove(i) {
mouse.selected.visible = false;
path.vertices.splice(i, 1);
}
function close(x, y) {
controls.anchor = null;
path.closed = true;
deselect();
path = null;
}
function select() {
points.visible = true;
selection.visible = true;
}
function deselect() {
points.visible = false;
selection.visible = false;
}
function updateControlHandles() {
// Update the pink control handles based on whatever the current
// path's details are — keeps things in sync
if (!controls.anchor) {
return;
}
controls.line.vertices[0].copy(controls.left.translation);
controls.line.vertices[1].copy(controls.anchor);
controls.line.vertices[2].copy(controls.right.translation);
}
/**
* Browser interactions handle below
*/
function mousedown(e) {
if (mouse.intersection) {
mouse.dragging = true;
if (mouse.intersection.id === 0) {
// Hack to emulate closing, but actually using
// one last point to simulate the left control handle
// of the first point.
var spoof = path.vertices[0];
add(spoof.x, spoof.y);
}
} else {
if (!path) {
create();
add(e.clientX, e.clientY);
select();
} else {
add(e.clientX, e.clientY);
}
}
window.addEventListener('mousemove', drag, false);
window.addEventListener('mouseup', mouseup, false);
}
function doubleclick() {
if (!path) {
return;
}
var first = path.vertices[0];
if (!first.controls.left.isZero() || !first.controls.right.isZero()) {
// Hack to emulate closing, but actually using
// one last point to simulate the left control handle
// of the first point.
var last = add(first.x, first.y);
last.controls.left.copy(first.controls.left);
}
close();
}
function mousemove(e) {
// Calculate what object is intersecting the mouse
// only when we're not already doing some other interaction
if (mouse.dragging) {
return;
}
var x = mouse.x;
var y = mouse.y;
mouse.set(e.clientX, e.clientY);
mouse.intersection = null;
for (var i = 0; i < points.vertices.length; i++) {
var point = points.vertices[i];
var d = point.distanceToSquared(mouse);
if (d <= mouse.radiusSquared) {
mouse.selected.visible = true;
mouse.selected.position.copy(point);
mouse.intersection = {
object: mouse.selected,
id: i
};
}
}
if (!mouse.intersection) {
mouse.selected.visible = false;
}
}
function drag(e) {
mouse.set(e.clientX, e.clientY);
// Like mousemove, but on the window and only occuring when
// we've called `mousedown` effectively creating a drag event
if (mouse.dragging) {
if (controls.anchor) {
var anchor = selection.vertices[selection.vertices.length - 1];
anchor.controls.left.copy(mouse).sub(anchor);
anchor.controls.right.clear();
anchor.trigger(Two.Events.Types.change);
controls.visible = true;
controls.left.translation.copy(anchor.controls.left).add(anchor);
controls.right.translation.copy(anchor.controls.right).add(anchor);
} else {
// Move an existing point
mouse.dragging = 1;
var object = mouse.intersection.object;
object.translation.copy(mouse);
}
} else if (controls.anchor) {
// Move a just created anchor point
var anchor = selection.vertices[selection.vertices.length - 1];
anchor.controls.right.copy(mouse).sub(anchor);
anchor.controls.left.copy(anchor.controls.right).rotate(Math.PI);
anchor.trigger(Two.Events.Types.change);
controls.visible = true;
controls.left.translation.copy(anchor.controls.left).add(anchor);
controls.right.translation.copy(anchor.controls.right).add(anchor);
}
}
function mouseup() {
if (mouse.intersection) {
if (mouse.dragging === true) {
// Close or remove a point from a path
if (mouse.intersection.id > 0) {
remove(mouse.intersection.id);
} else {
if (controls.anchor) {
// If there was a modification of control points then
// update the path accordingly.
var anchor = path.vertices[path.vertices.length - 1];
anchor.controls.left.copy(controls.left.translation).sub(anchor);
anchor.controls.right.copy(controls.right.translation).sub(anchor);
anchor.trigger(Two.Events.Types.change);
}
close();
}
}
} else if (controls.anchor) {
// Set the control point from the anchor to the corresponding path
var anchor = path.vertices[path.vertices.length - 1];
anchor.controls.left.copy(controls.left.translation).sub(anchor);
anchor.controls.right.copy(controls.right.translation).sub(anchor);
anchor.trigger(Two.Events.Types.change);
}
// Reset listeners and context aware variables.
window.removeEventListener('mousemove', drag, false);
window.removeEventListener('mouseup', mouseup, false);
mouse.dragging = false;
mouse.intersection = null;
selection.closed = false;
controls.visible = false;
}
Also see: Tab Triggers