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.
<svg id="svg" viewBox="0 0 800 400">
<polygon id="star" points="461,220 498,335 400,264 302,335 339,220 242,149 362,148 400,34 438,148 558,149" />
<circle id="dot" r="8" fill="red" />
</svg>
body {
background: #e8e9e8;
font-family:sans-serif;
color:#555;
}
#svg {
position: fixed;
top: 20px;
left: 0;
width: 100%;
height: 100%;
}
#star {
stroke: #29e;
stroke-width: 5;
stroke-linejoin: round;
fill: none;
}
//play with the xVelocity and yVelocity values - it'll always land on a point of the star (unless it's beyond the snapDistance or snapToStar is false)
var xVelocity = 310,
yVelocity = 120,
snapToStar = true,
snapDistance = 1000,
starPoints = pointsToArray(document.getElementById("star")),
dot = document.getElementById("dot");
gsap.to(dot, {
inertia: {
x: {
velocity: xVelocity,
end: snap
},
y: {
velocity: yVelocity,
end: snap
},
linkedProps: "x,y" //<- this is the key that makes x and y get combined into an object and passed to the "end" function
}
});
/*
The object that gets passed as the only parameter to the "end" function will have the properties are listed in linkedProps. So, for example, if linkedProps is "x,y", then an object like {x:100, y:140} gets passed to the function as a parameter. Those values are the natural ending values, but of course your function should return a similar object with the new values you want the end values to be, like return {x:200, y:300}.
*/
//finds the closest point in the starPoints array to the one provided (within the snapDistance).
function snap(p) {
var i = starPoints.length,
closest = 0,
minDist = 999999999,
x, y, point, dist;
if (!snapToStar) {
return p;
}
while (--i > -1) {
point = starPoints[i];
x = point.x - p.x;
y = point.y - p.y;
dist = x * x + y * y;
if (dist < minDist) {
closest = i;
minDist = dist;
}
}
return (minDist <= snapDistance * snapDistance) ? starPoints[closest] : p;
}
//returns all the SVGPoints in a <polygon> or <polyline> as an array. (note: not all browsers support [].slice.call(element.points) which is why we need this function)
function pointsToArray(element) {
var points = element.points,
result = [],
i;
for (i = 0; i < points.numberOfItems; i++) {
result[i] = points.getItem(i);
}
return result;
}
//or if you'd rather enable tracking on the x/y properties and have ThrowPropsPlugin
//just take over in the middle of some other motion which we're simulating here with
//a simple x/y tween, here's an example of that...
/*
InertiaPlugin.track(dot, "x,y");
gsap.to(dot, {duration: 3, x: 420, y: 190});
//halfway through the tween, take over...
gsap.delayedCall(1.5, function() {
gsap.to(dot, {
inertia:{
x: {velocity: "auto", end: snap},
y: {velocity: "auto", end: snap},
linkedProps: "x,y"
}
});
});
*/
Also see: Tab Triggers