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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body, html {
background-color: rgb(220, 220, 220);
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden; /* Prevent scrolling */
display: flex;
justify-content: center;
}
p {
font-size: 1.5em;
font-family: Arial, Helvetica, sans-serif;
}
#canvas-container {
width: 1000px;
height: 600px;
display: inline-block;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<script src="sketch.js" defer></script>
</head>
<body>
<div id="canvas-container">
<p style="display:block">Instructions: Move the image/mouse left and right across the vertical line.</p>
<p> Inspired by the 2006 animated film Pale Cocoon.</p>
</div>
</body>
</html>
const numRects = 13;
//canvas//
const canvasWidth = 1000;
const canvasHeight = 600;
const leftMargin = 20;
//rectangle//
const rectWidth = 200;
const rectSpacerHeight = 20;
const rectMultiplier = 1;
const rectSpacerHeightTotal = ((numRects - 1) * rectSpacerHeight);
const rectHeight = (canvasHeight - rectSpacerHeightTotal) / numRects;
const rectSpacerHalfHeight = rectSpacerHeight / 2;
const rectHalfWidth = rectWidth / 2;
const rectHalfHeight = rectHeight / 2;
//leftRect//
const leftRectHeight = rectHeight * 0.75;
const leftRectWidth = rectWidth * 0.50;
const leftRectWidthRemainder = rectWidth - leftRectWidth;
const leftRectMargin = rectWidth * 0.05;
//rightRect//
const rightRectHeight = rectHeight * 0.75;
const rightRectWidth = rectWidth * 0.30;
const rightRectWidthRemainder = rectWidth - rightRectWidth;
const rightRectMargin = rectWidth * 0.1;
//ellipse//
const ellipseDiameter = rectHeight * 0.5;
const ellipseRadius = ellipseDiameter / 2;
const ellipseSpacer = ellipseDiameter * 2;
//image//
const imageWidth = 312; //312
const imageHalfWidth = imageWidth / 2;
const imageHeight = 242; //242
const imageSpacer = imageWidth / 2 + 20;
let img;
//speeds//
const extendSpeed = 0.05;
const retractSpeed = 0.2;
//aesthetic//
const cornerRound = 2;
function Rectangle(initialX, initialY) {
this.initialX = initialX;
this.initialY = initialY;
this.x = this.initialX;
this.y = this.initialY;
this.prevRect = null;
this.nextRect = null;
//comment up these calcs below
this.leftRect = new SubRectangle(this.initialX - (leftRectWidth / 2) + leftRectMargin, this.initialY); //centers sub rectable in large one
this.rightRect = new SubRectangle(this.initialX + (leftRectWidth / 2) - leftRectMargin + rightRectMargin, this.initialY); //centers sub rectable in large one
this.rightAnchorLine = new Line(this);
this.connectorLine = new Line(this);
}
Rectangle.prototype.rectHalfWidth = function(rectWidth) {
this.initialX = leftMargin + rectWidth / 2;
}
function SubRectangle(initialX, initalY) {
this.initialX = initialX; //definitely need this <- don't change
this.initialY = initalY;
this.x = this.initialX;
this.y = this.initialY;
}
function Line(rectangle) {
//Line class should have initialX and Y properties that are passed instead of the rectangle probably
this.rectangle = rectangle;
}
Line.prototype.updateSelectedLinePos = function() {
//right anchor to cursor line, subtract out ellipseRadius to make flush with left side of ellipse
line(this.rectangle.x + rectHalfWidth, this.rectangle.y, mouseX - imageHalfWidth, mouseY);
}
Rectangle.prototype.updateSelectedRectPos = function() {
//rectangle fly to mouse
let rectMouseOffset = (rectWidth / 2) + imageSpacer;
this.x = lerp(this.x, mouseX - rectMouseOffset, extendSpeed); //the second x position must be adusted to account for the size of the rectangle and image spacer
this.y = lerp(this.y, mouseY, 1);
//left rectangle fly to mouse
let leftRectMouseOffset = ((rectWidth / 2) + imageSpacer + (leftRectWidth / 2) - leftRectMargin);
this.leftRect.x = lerp(this.leftRect.x, mouseX - leftRectMouseOffset, extendSpeed);
this.leftRect.y = lerp(this.leftRect.y, mouseY, 1);
//right rectangle fly to mouse
let rightRectMouseOffset = imageSpacer + //move this far to the left from mouseX
(rightRectWidth / 2) + //gets right side of rightRect lined up with right side of container rectangle
rectWidth - //right side of rightRect flush with left side of rect
rightRectWidth - //left of leftRect flush with left side of rect
leftRectMargin - //left of leftRect flush with left side of rightRect
leftRectWidth - //right side of leftRect flush with left side of rightRect
rightRectMargin; //move to the right by this rightRectMargin
this.rightRect.x = lerp(this.rightRect.x, mouseX - rightRectMouseOffset, extendSpeed); //probably need to subtract out stuff on mouseX
this.rightRect.y = lerp(this.rightRect.y, mouseY, 1);
lastToDraw = this;
return lastToDraw;
}
Rectangle.prototype.updateDeselectedRectPos = function() {
//retract rectangle to its starting position
this.x = lerp(this.x, this.initialX, retractSpeed);
this.y = lerp(this.y, this.initialY, retractSpeed);
rect(this.x, this.y, rectWidth * rectMultiplier, rectHeight * rectMultiplier, cornerRound);
//retract left rectangle to its starting position
this.leftRect.x = lerp(this.leftRect.x, this.leftRect.initialX, retractSpeed);
this.leftRect.y = lerp(this.leftRect.y, this.leftRect.initialY, retractSpeed);
rect(this.leftRect.x, this.leftRect.y, leftRectWidth, leftRectHeight, cornerRound);
//retract right rectangle to its starting position
this.rightRect.x = lerp(this.rightRect.x, this.rightRect.initialX, retractSpeed);
this.rightRect.y = lerp(this.rightRect.y, this.rightRect.initialY, retractSpeed);
rect(this.rightRect.x, this.rightRect.y, rightRectWidth, rightRectHeight, cornerRound);
}
Rectangle.prototype.updatePopoutRectPos = function() {
// handle the popout of adjacent rectangles
if (this.prevRect) {
this.prevRect.x = lerp(this.prevRect.x, rectWidth * 2, extendSpeed);
}
if (this.nextRect) {
this.nextRect.x = lerp(this.nextRect.x, rectWidth * 2, extendSpeed);
}
}
let rectArr = [];
for (let i = 0; i < numRects; i++) {
let rectInitialX = leftMargin + rectWidth / 2; //margin to left of rect + half rect width to compensate for rect mode
let rectInitialY = i * rectHeight + //start each rect this far down on the y-axis ie: 0, 200, 400, etc.
rectHalfHeight + //push all rects down to compensate vertically for rectMode
(i != 0 ? rectSpacerHeight * i : 0) //add in vertical spacer height
let currRect = new Rectangle(rectInitialX, rectInitialY)
rectArr.push(currRect);
//sets the prevRect and nextRect of all Rectangles except the first and last which default to null.
if (i > 0) {
rectArr[i - 1].nextRect = currRect;
currRect.prevRect = rectArr[i - 1];
}
}
function setup() {
let canvas = createCanvas(canvasWidth, canvasHeight);
canvas.parent('canvas-container');
img = createImg('https://assets.editor.p5js.org/5c3a7b27bb105c001f5a489c/f5f595d4-f0e5-486c-9e2d-94b0d385b269.png');
img.hide();
}
function draw() {
background(220, 220, 220);
// Draw 1px black border
stroke(0); // Set stroke color to black
noFill(); // Don't fill the rectangle
rect(500, 300, width - 1, height - 1);
noCursor();
rectMode(CENTER);
fill(220, 220, 220);
let lastToDraw = null;
line(canvasWidth / 2 + 200 - imageWidth/2,0,canvasWidth / 2 + 200 - imageWidth/2,canvasHeight);
for (let i = 0; i < rectArr.length; i++) {
//updates the position of the rect based on where the mouse is at any given point in time
const maxCursorRangeAboveRectCenterY = rectArr[i].initialY - rectHalfHeight - rectSpacerHalfHeight;
const maxCursorRangeBelowRectCenterY = rectArr[i].initialY + rectHalfHeight + rectSpacerHalfHeight;
//If cursor y position within this range, fly rectangle to cursor
if (mouseY >= maxCursorRangeAboveRectCenterY && mouseY < maxCursorRangeBelowRectCenterY && mouseX < canvasWidth / 2 + 200 /*&& mouseIsPressed*/) {
lastToDraw = rectArr[i].updateSelectedRectPos();
rectArr[i].rightAnchorLine.updateSelectedLinePos();
} else if (mouseY >= maxCursorRangeAboveRectCenterY && mouseY < maxCursorRangeBelowRectCenterY && mouseX < canvasWidth / 2 + 200 /*&& !mouseIsPressed*/) {
rectArr[i].updateDeselectedRectPos();
} else {
rectArr[i].updateDeselectedRectPos();
}
//left anchor line, subtract out rectHalfWidth to make line flush with left side of rectangle
line(0, rectArr[i].initialY, rectArr[i].x - rectHalfWidth, rectArr[i].y);
}
//drawing rectangle here ensure it stays "always on top" of the other rectangles
if (lastToDraw) {
lastToDraw.updateSelectedRectPos();
rect(lastToDraw.x, lastToDraw.y, rectWidth * rectMultiplier, rectHeight * rectMultiplier, cornerRound);
rect(lastToDraw.leftRect.x, lastToDraw.leftRect.y, leftRectWidth, leftRectHeight);
rect(lastToDraw.rightRect.x, lastToDraw.rightRect.y, rightRectWidth, rightRectHeight);
}
strokeWeight(2);
rect(mouseX, mouseY, imageWidth, imageHeight);
strokeWeight(1);
imageMode(CENTER);
image(img, mouseX, mouseY, 300, 230);
}
Also see: Tab Triggers