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.
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<canvas id=canvas></canvas>
<div class="label">Thor's Cape</div>
<div class="howtofly">CLICK AND DRAG POLYGON ON CAPE WITH MOUSE</div>
<!-- AmieDD Twitter -->
<div class="twitter">
<a href="https://twitter.com/amiedoubled" target="_blank"></a>
<i class="fa fa-twitter fa-lg"></i>
</div>
body {
background:url("https://c1.staticflickr.com/5/4873/32211025928_626a71b97d_b.jpg")
no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
.label {
position: absolute;
top: 0;
left: 0;
padding: 10px 15px;
color: #b80000;
font-size: 15px;
font-family: 'Arial Black', sans-serif;
background-color: rgba(0, 0, 0, .15);
}
.howtofly {
position: absolute;
bottom: 0%;
left: 0;
padding: 5px 15px;
color: #b80000;
font-size: 13px;
font-family: 'Arial Black', sans-serif;
background-color: rgba(0, 0, 0, .15);
}
canvas{display:block;}
//Ragdoll physic simulation similar to Thor's Cape for Thor God of Thunder Game I worked on.
//www.amiedd.com
//October 2018
var ctx = canvas.getContext("2d");
var nodes = [];
var links = [];
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function dist(x,y){
return Math.sqrt(x*x+y*y);
}
var time = Date.now();
var dt = 1/60;
var TWO_PI = 2 * Math.PI;
var width = 21;
var height = 22;
var nodeCount = width * height;
var delta = Math.min((canvas.width-30)/width, (canvas.height-20)/height);
var dx = delta;
var dy = delta;
var max_dist = delta;
var min_dist = 5;
var iterations = 25;
for (i = 0; i < nodeCount; i++) {
var node = {
x: canvas.width/2 - width/2 * dx + Math.floor(i%width) * dx,
y: canvas.height/2 - height/2 * dy + Math.floor(i/width) * dy,
radius: 1,
last_x: canvas.width/3 - width/3 * dx + Math.floor(i%width) * dx,
last_y: canvas.height/2 - height/2 * dx + Math.floor(i/width) * dx + Math.random() * dy,
static: false
};
nodes.push(node);
}
for(i = 0; i < width; i++){
}
nodes[0].static = true;
nodes[Math.floor(width/2)].static = true;
nodes[Math.floor(width/2)].static = true;
nodes[Math.floor(width/2) + Math.floor(width/2)].static = true;
nodes[width-1].static = true;
function create_links(){
//X links in the polygon for cape
for (i = 0; i < (nodes.length-1); i++) {
if(((i+1)%width) > 0){
var link = {
first: i,
second: i+1
};
links.push(link);
}
}
//Y links
for (i = 0; i < (nodes.length-width); i++) {
var link = {
first: i,
second: i+width
};
links.push(link);
}
}
canvas.addEventListener('mousemove', mouse_track);
canvas.addEventListener('mousedown', mouse_down);
canvas.addEventListener('mouseup', mouse_up);
var active_node;
var mouse_pos_x;
var mouse_pos_y;
var drag = false;
function mouse_track(event) {
mouse_pos_x = event.clientX;
mouse_pos_y = event.clientY;
if(drag){
if(active_node != -1){
nodes[active_node].x = mouse_pos_x;
nodes[active_node].y = mouse_pos_y;
nodes[active_node].last_x = mouse_pos_x;
nodes[active_node].last_y = mouse_pos_y;
}
}
}
function mouse_down(event) {
drag = true;
mouse_pos_x = event.clientX;
mouse_pos_y = event.clientY;
active_node = -1;
for(l = 0; l < nodes.length; l++){
if(dist(mouse_pos_x - nodes[l].x, mouse_pos_y - nodes[l].y) < 20){
active_node = l;
if(active_node != -1){
nodes[active_node].x = mouse_pos_x;
nodes[active_node].y = mouse_pos_y;
nodes[active_node].last_x = mouse_pos_x;
nodes[active_node].last_y = mouse_pos_y;
}
break;
}
}
}
function mouse_up(event) {
drag = false;
active_node = -1;
}
function resolve_constraints(){
var first;
var second;
var dx;
var dy;
var d;
var diff;
var translateX;
var translateY;
for(i = 0; i < iterations; i++){
for(l = 0; l < links.length; l++){
first = nodes[links[l].first];
second = nodes[links[l].second];
dx = first.x - second.x;
dy = first.y - second.y;
d = Math.sqrt(dx*dx+dy*dy);
if(d > max_dist){
diff = (max_dist - d)/d;
translateX = dx * 0.5 * diff;
translateY = dy * 0.5 * diff;
if(!first.static){
if(links[l].first!= active_node){
first.x += translateX;
first.y += translateY;
}
}
if(!second.static){
if(links[l].second!= active_node){
second.x -= translateX;
second.y -= translateY;
}
}
}
}
}
}
function move(dt, alpha) {
var x_vel;
var y_vel;
var next_x;
var next_y;
var accX = 0;
var accY = 9.8;
for(i = 0; i < nodes.length; i++){
if(i != active_node){
x_vel = nodes[i].x - nodes[i].last_x + accX * dt;
y_vel = nodes[i].y - nodes[i].last_y + accY * dt;
next_x = nodes[i].x + x_vel;
next_y = nodes[i].y + y_vel;
nodes[i].last_x = nodes[i].x;
nodes[i].last_y = nodes[i].y;
if(!nodes[i].static){
nodes[i].x = next_x;
nodes[i].y = next_y;
}
}
}
for(i = 0; i < nodes.length; i++){
if(nodes[i].x < 10){
nodes[i].x = 10;
}
if(nodes[i].y < 10){
nodes[i].y = 10;
}
if(nodes[i].x > canvas.width-10){
nodes[i].x = canvas.width-10;
}
if(nodes[i].y > canvas.height-10){
nodes[i].y = canvas.height-10;
}
}
}
var fps;
create_links();
var newtime;
var frametime;
var accumulator = 0;
var alpha;
function draw() {
//game physics time step logic
newtime = Date.now();
frametime = newtime - time;
fps = 1000 / (frametime);
time = newtime;
//Pause cape movement when no activity
if (fps > 10) {
accumulator += frametime/1000;
while ( accumulator >= dt ){
resolve_constraints();
move(dt);
accumulator -= dt;
}
alpha = accumulator / dt;
}
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.fillStyle = "rgba(204,14,29, 0.7)";
for (i = 0; i < nodes.length; i++) {
ctx.beginPath();
ctx.arc(nodes[i].x, nodes[i].y, nodes[i].radius, 0, TWO_PI);
ctx.fill();
}
ctx.strokeStyle = "rgba(162,20,26,1)";
for(l = 0; l < links.length; l++){
ctx.beginPath();
ctx.moveTo(nodes[links[l].first].x, nodes[links[l].first].y);
ctx.lineTo(nodes[links[l].second].x, nodes[links[l].second].y);
ctx.stroke();
}
window.requestAnimationFrame(draw);
}
window.requestAnimationFrame(draw);
Also see: Tab Triggers