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.
<section class="phone-content">
<phone-container>
<svg id="phone-svg" viewBox="0 0 1335 2132">
<filter id="drop-shadow" x="-100%" y="-100%" width="300%" height="300%">
<feGaussianBlur id="shadow-blur" in="SourceAlpha" stdDeviation="50"/>
<feOffset dx="0" dy="0" result="offsetblur"/>
<feFlood id="shadow-color" flood-color="#fff"/>
<feComposite in2="offsetblur" operator="in"/>
<feMerge>
<feMergeNode/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
<mask id="phone-mask" maskUnits="userSpaceOnUse">
<path id="phone-mask-path" d="M390.001733,1932.5 C390.001733,1932.5 565.999988,1931.5 586.05712,1931.5 C606.114252,1931.5 617.023773,1915.5 620.828278,1915.5 C660.021292,1915.5 735.169897,1915.5 737.998019,1915.5 C740.826141,1915.5 746.051941,1931.5 778.812117,1931.5 C811.572292,1931.5 971.998267,1932.5 971.998267,1932.5 C1043.24228,1932.5 1106,1869.76062 1106,1797.49805 L1106,338.501954 C1106,255.587048 1040.06342,200.5 978.998267,200.5 L903.563583,200.5 C877.936549,200.5 860.246162,215.5 860.246162,215.5 L797.920589,215.5 C797.920589,215.5 776.386439,200.5 760.492377,200.5 C744.598316,200.5 379.001733,200.5 379.001733,200.5 C307.757721,200.5 250,260.459622 251,340.704544 L251,604.642372 L258,630.93573 L258,745.743147 L251,769.688698 L251,865.914447 L258,887.652675 L258,1128.96869 L251,1148.60567 L252,1791.49805 C252,1878.48584 318.756326,1932.5 390.001733,1932.5 Z" />
</mask>
<path id="phone-outline" filter="url(#drop-shadow)" d="M390.001733,1932.5 C390.001733,1932.5 565.999988,1931.5 586.05712,1931.5 C606.114252,1931.5 617.023773,1915.5 620.828278,1915.5 C660.021292,1915.5 735.169897,1915.5 737.998019,1915.5 C740.826141,1915.5 746.051941,1931.5 778.812117,1931.5 C811.572292,1931.5 971.998267,1932.5 971.998267,1932.5 C1043.24228,1932.5 1106,1869.76062 1106,1797.49805 L1106,338.501954 C1106,255.587048 1040.06342,200.5 978.998267,200.5 L903.563583,200.5 C877.936549,200.5 860.246162,215.5 860.246162,215.5 L797.920589,215.5 C797.920589,215.5 776.386439,200.5 760.492377,200.5 C744.598316,200.5 379.001733,200.5 379.001733,200.5 C307.757721,200.5 250,260.459622 251,340.704544 L251,604.642372 L258,630.93573 L258,745.743147 L251,769.688698 L251,865.914447 L258,887.652675 L258,1128.96869 L251,1148.60567 L252,1791.49805 C252,1878.48584 318.756326,1932.5 390.001733,1932.5 Z" />
<g>
<rect id="background" fill="#b8c2de" width="100%" height="100%" mask="url(#phone-mask)" />
<g id="ripple-layer" mask="url(#phone-mask)"></g>
</g>
<image x="1" y="1" width="1333px" height="2130px" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/phone.png?v=1" />
</svg>
</phone-container>
</section>
<section class="color-content">
<color-sidebar>
<color-picker></color-picker>
</color-sidebar>
</section>
$colorSize: 40px;
$phoneSize: 90vmin;
$phoneWidth: 1335px;
$phoneHeight: 2132px;
$sidebarWidth: 62px;
$aspectRatio: $phoneWidth / $phoneHeight;
* {
box-sizing: border-box;
}
body {
overflow-x: hidden;
}
.color-content {
}
color-sidebar {
display: block;
position: absolute;
top: 0;
left: 0;
padding: 10px;
z-index: 2;
touch-action: none;
}
color-picker {
display: block;
position: relative;
}
color-item,
color-draggable {
display: block;
border-radius: 50%;
visibility: hidden;
opacity: 0;
box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.26);
cursor: move;
cursor: -webkit-grab;
cursor: grab;
}
color-item {
position: relative;
margin: 10px 10px 0 10px;
width: $colorSize;
height: $colorSize;
min-width: $colorSize;
min-height: $colorSize;
}
color-draggable {
top: 0;
left: 0;
width: 100%;
height: 100%;
position: absolute;
}
color-draggable.dragging {
cursor: -webkit-grabbing;
cursor: grabbing;
}
.phone-content {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding-left: $sidebarWidth / 2;
background: linear-gradient(white, #f2f2f2);
}
phone-container {
display: block;
position: relative;
width: $phoneSize * $aspectRatio;
padding-top: 1 / $aspectRatio * 100%;
visibility: hidden;
opacity: 0;
}
#phone-svg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: visible !important;
}
#phone-mask-path {
fill: #fff;
stroke: #fff;
stroke-width: 4;
}
#phone-outline {
fill: white;
stroke: white;
stroke-width: 3;
vector-effect: non-scaling-stroke;
opacity: 0;
stroke: rgba(0,0,0,0.2);
}
console.clear();
const log = console.log.bind(console);
const svgns = "http://www.w3.org/2000/svg";
SVGElement.prototype.getTransformToElement = SVGElement.prototype.getTransformToElement || function(toElement) {
return toElement.getScreenCTM().inverse().multiply(this.getScreenCTM());
};
//
// RIPPLE
// ===========================================================================
class Ripple {
element = document.createElementNS(svgns, "circle");
constructor(x, y, public color) {
const dx = x - offsetX;
const dy = y - offsetY;
const element = this.element;
const radius = this.getRadius(dx, dy, phoneRect.width, phoneRect.height);
rippleLayer.appendChild(element);
element.setAttribute("r", 5);
TweenLite.set(element, {
transformOrigin: "center",
fill: color,
x: x,
y: y
});
TweenLite.to(element, rippleDuration, {
scale: radius / 2,
ease: Power1.easeIn,
callbackScope: this,
onComplete: this.removeRipple
});
}
getRadius(x, y, w, h) {
const cx = w / 2;
const cy = h / 2;
const dx = x < cx ? w - x : x;
const dy = y < cy ? h - y : y;
return Math.sqrt(dx * dx + dy * dy);
}
removeRipple() {
TweenLite.set(background, { fill: this.color });
rippleLayer.removeChild(this.element);
}
}
//
// COLOR ITEM
// ===========================================================================
class ColorItem {
itemElement = document.createElement("color-item");
dragElement = document.createElement("color-draggable");
draggable = new Draggable(this.dragElement, {
trigger: this.itemElement,
onPress: this.onPress,
onDragStart: this.onDragStart,
onDrag: this.onDrag,
onRelease: this.onRelease,
callbackScope: this,
cursor: grab,
});
constructor(public color) {
const itemElement = this.itemElement;
const dragElement = this.dragElement;
colorPicker.appendChild(itemElement);
itemElement.appendChild(dragElement);
TweenLite.set(dragElement, {
autoAlpha: 0,
scale: 0.25,
});
TweenLite.set([itemElement, dragElement], {
backgroundColor: color
});
}
hitTest(x, y) {
let inside = false;
if (phoneRect.contains(x, y)) {
if (phonePoly.contains(x, y)) {
inside = true;
}
}
return inside;
}
onPress() {
this.itemElement.style.setProperty("z-index", Draggable.zIndex);
this.dragElement.style.setProperty("cursor", grabbing);
TweenLite.set(this.dragElement, {
autoAlpha: 1,
scale: 1,
x: 0,
y: 0
});
TweenLite.to(shadowColor, 0.2, { attr: { "flood-color": this.color }});
TweenLite.to(phoneOutline, 0.2, { stroke: this.color });
outlineAnimation.reverse();
this.draggable.update();
}
onDragStart() {
TweenLite.to(this.dragElement, 0.25, {
autoAlpha: 1,
scale: 1.5,
ease: Power1.easeInOut
});
}
onRelease() {
const x = this.draggable.pointerX - window.pageXOffset;
const y = this.draggable.pointerY - window.pageYOffset;
const point = toLocal(x, y);
const inside = this.hitTest(point.x, point.y);
let duration = 0.2;
if (inside) {
const ripple = new Ripple(point.x, point.y, this.color);
duration = 0;
}
outlineAnimation.reverse();
TweenLite.to(this.dragElement, duration, {
autoAlpha: 0,
scale: 0.25,
ease: Power1.easeIn
});
}
onDrag() {
const x = this.draggable.pointerX - window.pageXOffset;
const y = this.draggable.pointerY - window.pageYOffset;
const point = toLocal(x, y);
const inside = this.hitTest(point.x, point.y);
outlineAnimation[inside ? "play" : "reverse"]();
}
}
//
// RECTANGLE
// ===========================================================================
class Rectangle {
constructor(public x = 0, public y = 0, public width = 0, public height = 0) {
}
contains(x, y) {
if (this.width <= 0 || this.height <= 0) {
return false;
}
if (x >= this.x && x < this.x + this.width) {
if (y >= this.y && y < this.y + this.height) {
return true;
}
}
return false;
}
}
//
// POLYGON
// ===========================================================================
class Polygon {
constructor(public points) {
}
contains(x, y) {
let inside = false;
const points = this.points;
const size = points.length / 2;
// use raycasting to test hits
// https://github.com/substack/point-in-polygon/blob/master/index.js
for (let i = 0, j = size - 1; i < size; j = i++) {
const xi = points[i * 2];
const yi = points[(i * 2) + 1];
const xj = points[j * 2];
const yj = points[(j * 2) + 1];
const intersect = ((yi > y) !== (yj > y)) && (x < ((xj - xi) * ((y - yi) / (yj - yi))) + xi);
if (intersect) {
inside = !inside;
}
}
return inside;
}
}
//
// GLOBAL TO LOCAL
// ===========================================================================
function globalToLocal(element, svg) {
const pt = svg.createSVGPoint();
return (x, y) => {
pt.x = x;
pt.y = y;
const globalPoint = pt.matrixTransform(svg.getScreenCTM().inverse());
const localMatrix = element.getTransformToElement(svg).inverse();
return globalPoint.matrixTransform(localMatrix);
}
}
//
// INIT
// ===========================================================================
const colors = [
"#b8c2de",
"#537ebf",
"#244191",
"#ac286e",
"#f17bb0",
"#e8405d",
"#f9a977",
"#f9f06e",
"#a7d492",
"#88d3e1",
];
// const rippleDuration = 0.35;
const rippleDuration = 0.5;
const { grab, grabbing } = getCursors();
const svg = document.querySelector("#phone-svg");
const background = document.querySelector("#background");
const colorPicker = document.querySelector("color-picker");
const rippleLayer = document.querySelector("#ripple-layer");
const phoneOutline = document.querySelector("#phone-outline");
const shadowColor = document.querySelector("#shadow-color");
const shadowBlur = document.querySelector("#shadow-blur");
// Convert path data to polygon points
const pathData = MorphSVGPlugin.pathDataToBezier(phoneOutline);
const contour = simplify(getContour(pathData, 50), 5, false);
const points = contour.reduce((res, pt) => {
res.push(roundTo(pt.x, 3));
res.push(roundTo(pt.y, 3));
return res;
}, []);
const box1 = phoneOutline.getBBox();
const box2 = background.getBBox();
// Shapes for hit testing
const phoneRect = new Rectangle(box1.x, box1.y, box1.width, box1.height);
const colorRect = new Rectangle(box2.x, box2.y, box2.width, box2.height);
const phonePoly = new Polygon(points);
const toLocal = globalToLocal(background, svg);
const offsetX = phoneRect.x - colorRect.x;
const offsetY = phoneRect.y - colorRect.y;
const outlineAnimation = new TimelineLite()
.to(phoneOutline, rippleDuration * 0.75, { opacity: 1, ease: Sine.easeInOut })
.reverse();
const colorItems = colors.map(color => new ColorItem(color));
TweenLite.set("color-item", { autoAlpha: 1 });
TweenLite.set("phone-container", { autoAlpha: 0, scale: 0.5 });
window.addEventListener("load", onLoad);
window.focus();
function onLoad() {
TweenLite.to("phone-container", 0.55, { autoAlpha: 1, scale: 1 });
}
function roundTo(value, place = 0, base = 10) {
const p = Math.pow(base, place);
return Math.round(value * p) / p;
}
function getCursors() {
const element = document.createElement("color-draggable");
document.body.appendChild(element);
const style = window.getComputedStyle(element, null);
const grab = style.getPropertyValue("cursor");
element.classList.add("dragging");
const grabbing = style.getPropertyValue("cursor");
document.body.removeChild(element);
return { grab, grabbing };
}
Also see: Tab Triggers