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);
var mouse = new Two.Vector();
var rect;
var path = new Two.Star(0, 0, two.height * 0.0625, two.height * 0.33, 9);
path.scale = new Two.Vector(1, 1);
path.position.set(two.width / 2, two.height / 2);
path.closed = true;
path.curved = true;
path.fill = new Two.LinearGradient(0, 0, 0, 1, [
new Two.Stop(0, 'orange'),
new Two.Stop(1, 'red'),
]);
path.noStroke();
var box = new Two.Rectangle(0, 0, 0, 0);
box.stroke = '#00AEFF';
box.noFill();
var endpoints = new Two.Points(box.vertices);
endpoints.size = 6;
endpoints.fill = '#00AEFF';
endpoints.noStroke();
var stage = new Two.Group();
var ui = new Two.Group();
var text = new Two.Text(`Operation: none`, 0, 0, {
size: 17,
baseline: 'bottom',
});
stage.add(path);
ui.add(box, endpoints);
two.add(stage, ui, text);
two.bind('update', update).bind('resize', resize);
resize();
var domElement = two.renderer.domElement;
domElement.addEventListener('pointerdown', pointerdown, false);
domElement.addEventListener('pointermove', pointermove, false);
domElement.addEventListener('pointerup', pointerup, false);
function resize() {
text.position.x = two.width / 2;
text.position.y = two.height - text.size;
}
function update(frameCount) {
var minX = Infinity;
var minY = Infinity;
var maxX = -Infinity;
var maxY = -Infinity;
for (var i = 0; i < 1; i += 0.01) {
var v = path.getPointAt(i);
minX = Math.min(minX, v.x * path.scale.x);
maxX = Math.max(maxX, v.x * path.scale.x);
minY = Math.min(minY, v.y * path.scale.y);
maxY = Math.max(maxY, v.y * path.scale.y);
}
box.width = maxX - minX;
box.height = maxY - minY;
ui.position = path.position;
ui.rotation = path.rotation;
}
//
var dragging = false,
scaling = false,
rotating = false,
start = new Two.Vector(),
initialPosition = new Two.Vector(),
initialRotation = 0;
function pointerdown(e) {
var rect = box.getBoundingClientRect();
mouse.x = e.clientX;
mouse.y = e.clientY;
dragging = true;
rotating = atCorner(box, mouse);
if (rotating) {
initialRotation = path.rotation;
// Store initial distance for proper rotation
start.set(e.clientX - path.position.x, e.clientY - path.position.y);
}
scaling = !rotating && atCorner(box, mouse, 25);
if (scaling) {
mouse.scale = path.scale.clone();
mouse.corner = scaling;
// Store initial values for scaling
start.copy(mouse);
mouse.initialWidth = box.width;
mouse.initialHeight = box.height;
// Store path's original position
initialPosition.copy(path.position);
}
// For positioning
if (!rotating && !scaling && contains(rect, mouse)) {
mouse.isPositioning = true;
} else {
mouse.isPositioning = false;
}
}
function pointermove(e) {
var rect = box.getBoundingClientRect();
var dx = e.clientX - mouse.x;
var dy = e.clientY - mouse.y;
var isRotating = atCorner(box, mouse);
var isScaling = atCorner(box, mouse, 25);
var isPositioning = !isRotating && !isScaling && contains(rect, mouse);
// Update current mouse position
mouse.x = e.clientX;
mouse.y = e.clientY;
// Update cursor style and operation text
if (rotating || isRotating) {
two.renderer.domElement.style.cursor = 'alias';
text.value = `Operation: rotate`;
} else if (scaling || isScaling) {
two.renderer.domElement.style.cursor = scaling
? scaling.name
: 'ns-resize';
text.value = `Operation: scale`;
} else if (isPositioning) {
two.renderer.domElement.style.cursor = dragging ? 'grabbing' : 'grab';
text.value = `Operation: position`;
} else {
two.renderer.domElement.style.cursor = 'default';
text.value = `Operation: none`;
}
if (!dragging) {
return;
}
if (rotating) {
text.value = 'Operation: rotating';
// Calculate angle from center to current mouse position
const currentAngle = Math.atan2(
e.clientY - path.position.y,
e.clientX - path.position.x
);
// Calculate angle from center to starting position
const startAngle = Math.atan2(start.y, start.x);
// Apply rotation as difference between angles
path.rotation = initialRotation + (currentAngle - startAngle);
} else if (scaling) {
text.value = 'Operation: scaling';
// Calculate scale factors based on mouse movement
let deltaX = mouse.x - start.x;
let deltaY = mouse.y - start.y;
// Adjust for shape rotation
if (path.rotation !== 0) {
const cos = Math.cos(-path.rotation);
const sin = Math.sin(-path.rotation);
const rotatedDeltaX = deltaX * cos - deltaY * sin;
const rotatedDeltaY = deltaX * sin + deltaY * cos;
deltaX = rotatedDeltaX;
deltaY = rotatedDeltaY;
}
// Calculate new dimensions
let newWidth, newHeight;
let scaleX, scaleY;
// Different scaling logic based on which corner is being dragged
if (mouse.corner.name === 'se-resize') {
newWidth = mouse.initialWidth + deltaX;
newHeight = mouse.initialHeight + deltaY;
} else if (mouse.corner.name === 'sw-resize') {
newWidth = mouse.initialWidth - deltaX;
newHeight = mouse.initialHeight + deltaY;
} else if (mouse.corner.name === 'ne-resize') {
newWidth = mouse.initialWidth + deltaX;
newHeight = mouse.initialHeight - deltaY;
} else if (mouse.corner.name === 'nw-resize') {
newWidth = mouse.initialWidth - deltaX;
newHeight = mouse.initialHeight - deltaY;
}
// Calculate scale factors
scaleX = newWidth / mouse.initialWidth;
scaleY = newHeight / mouse.initialHeight;
// Apply aspect ratio constraint with Shift key
if (e.shiftKey) {
const ratio = Math.min(Math.abs(scaleX), Math.abs(scaleY));
scaleX = scaleX >= 0 ? ratio : -ratio;
scaleY = scaleY >= 0 ? ratio : -ratio;
}
// Apply the new scale
path.scale.x = mouse.scale.x * scaleX;
path.scale.y = mouse.scale.y * scaleY;
// Adjust position to handle anchored scaling from corners
const anchorOffsetX = (mouse.initialWidth * (scaleX - 1)) / 2;
const anchorOffsetY = (mouse.initialHeight * (scaleY - 1)) / 2;
// Calculate position adjustment based on corner
let adjustX = 0,
adjustY = 0;
if (mouse.corner.name === 'nw-resize') {
adjustX = -anchorOffsetX;
adjustY = -anchorOffsetY;
} else if (mouse.corner.name === 'ne-resize') {
adjustX = anchorOffsetX;
adjustY = -anchorOffsetY;
} else if (mouse.corner.name === 'sw-resize') {
adjustX = -anchorOffsetX;
adjustY = anchorOffsetY;
} else if (mouse.corner.name === 'se-resize') {
adjustX = anchorOffsetX;
adjustY = anchorOffsetY;
}
// Apply position adjustment with rotation considered
if (path.rotation !== 0) {
const cos = Math.cos(path.rotation);
const sin = Math.sin(path.rotation);
const rotatedX = adjustX * cos - adjustY * sin;
const rotatedY = adjustX * sin + adjustY * cos;
path.position.x = initialPosition.x + rotatedX;
path.position.y = initialPosition.y + rotatedY;
} else {
path.position.x = initialPosition.x + adjustX;
path.position.y = initialPosition.y + adjustY;
}
// Scale from center with Alt key
if (e.altKey) {
path.position.x = initialPosition.x;
path.position.y = initialPosition.y;
}
// Add modifier keys info
if (e.shiftKey) {
text.value += ' (constrained)';
}
if (e.altKey) {
text.value += ' (from center)';
}
} else if (mouse.isPositioning) {
text.value = 'Operation: positioning';
two.renderer.domElement.style.cursor = 'grabbing';
path.position.add(dx, dy);
}
}
function pointerup() {
dragging = false;
scaling = false;
rotating = false;
mouse.isPositioning = false;
two.renderer.domElement.style.cursor = 'default';
}
//
function contains(rect, point) {
return (
point.x > rect.left &&
point.x < rect.right &&
point.y > rect.top &&
point.y < rect.bottom
);
}
function atCorner(object, point, limit) {
var v;
if (typeof limit !== 'number') {
limit = 10;
}
limit *= limit;
var matrix = Two.Utils.getComputedMatrix(object);
v = object.vertices[0];
var tl = matrix.multiply(v.x, v.y, 1);
v = object.vertices[1];
var tr = matrix.multiply(v.x, v.y, 1);
v = object.vertices[2];
var bl = matrix.multiply(v.x, v.y, 1);
v = object.vertices[3];
var br = matrix.multiply(v.x, v.y, 1);
var dbs = Two.Vector.distanceBetweenSquared;
if (dbs(point, { x: tl[0], y: tl[1] }) < limit) {
return { name: 'nw-resize', point: object.vertices[0] };
} else if (dbs(point, { x: tr[0], y: tr[1] }) < limit) {
return { name: 'ne-resize', point: object.vertices[1] };
} else if (dbs(point, { x: bl[0], y: bl[1] }) < limit) {
return { name: 'se-resize', point: object.vertices[2] };
} else if (dbs(point, { x: br[0], y: br[1] }) < limit) {
return { name: 'sw-resize', point: object.vertices[3] };
} else {
return false;
}
}
function getOppositeCorner(cornerName) {
switch (cornerName) {
case 'nw-resize':
return 'se-resize';
case 'ne-resize':
return 'sw-resize';
case 'sw-resize':
return 'ne-resize';
case 'se-resize':
return 'nw-resize';
default:
return null;
}
}
// Helper function to rotate a point around origin
function rotatePoint(point, angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return {
x: point.x * cos - point.y * sin,
y: point.x * sin + point.y * cos,
};
}
Also see: Tab Triggers