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.
<canvas id='cf'>Your browser doesn't support canvas</canvas>
<canvas id='ci'>Your browser doesn't support canvas</canvas>
<div id='shift'><div id='scrollbar'>
<h3>RequestAnimation<u>F</u>rame vs Set<u>I</u>nterval</h3>
<button id='btnF'>Stop <u>F</u>rame</button>
<button id='btnR'>Reset all</button>
<button id='btnI'>Stop <u>I</u>nterval</button>
<table><tr><td width="213">
<p>F<u>C</u> ~ Frame count = <b id='f1'></b></p>
<p>F<u>S</u> ~ FPS = <b id='f2'></b></p>
<p>F<u>A</u> ~ Avarage MS = <b id='f3'></b></p>
<p>F<u>L</u> ~ Circle Count = <b id='f4'></b></p>
<p>F<u>X</u> ~ Loss = <b id='f5'>???</b></p>
<p><u>F</u> score = <b id='f6'>???</b></p>
<hr>
<p>F<u>N</u> ~ Native MS = <b id='f7'></b></p>
</td><td width="233">
<p>I<u>C</u> ~ Execution count = <b id='i1'></b></p>
<p>I<u>S</u> ~ FPS = <b id='i2'></b></p>
<p>I<u>A</u> ~ Avarage MS = <b id='i3'></b></p>
<p>I<u>L</u> ~ Circle Count = <b id='i4'></b></p>
<p>I<u>X</u> ~ Loss = <b id='i5'>???</b></p>
<p><u>I</u> score = <b id='i6'>???</b></p>
<hr>
<p> </p>
</td></tr></table>
<p>The target fps is set to <i>60</i> as the RequestAnimationFrame default value can't be changed. SetInterval might get better result when current tab facus is lost.</p>
<ul>
<li><u>C</u> - count of draw calls.</li>
<br>
<li><u>S</u> - relative frame rate for each context.</li>
<br>
<li><u>A</u> - the avarage delay between two draw calls.</li>
<br>
<li><u>L</u> - amount of objects (circles) on each context.</li>
<br>
<li><u>X</u> - lower loss means better performance.</li>
<br>
<li>score - calculated relative to other context's variable <u>L</u> and it's own loss value.</li>
</ul>
<div style='height:200px'></div><p> </p>
</div></div>
html, body {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
font-family: Consolas;
font-weight: 100;
color: white;
background-color: #000;
width: 100%;
height: 100%;
margin: 0px;
padding:0px;
border: 0;
/* Disable scrollbars */
overflow: hidden;
/* No floating content on sides */
display: block;
}
#shift {
margin-left:auto;
margin-right:auto;
width:500px;
padding-left: 20px;
height:100%;
}
#scrollbar {
max-height:100%;overflow:auto;
}
#cf {
float:left;
}
#ci {
float:right;
}
table, td, tr {
margin: 0;
padding:0;
border:0;
}
h3 {
font-size:16px;
margin-top:0;
padding-top:15px;
padding-bottom:0;
}
p {
font-size:14px;
}
u
{
text-decoration: none;
padding:1px;
color:orange;
}
b
{
padding:2px;
border-bottom:1px solid cyan;
color:cyan;
font-weight: normal;
}
i
{
color: #6ff546;
}
button:hover { border-color:black;
background-color:#226;}
button{
font-family: Consolas;
background: black;
color:white;
border: 2px solid white;
padding:6px 14px;
cursor: pointer;
}
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-thumb {
background-color: #333;
}
////////////////////////////////////////////////////////////////////////////////////////
// NOT IMPORTANT CODE ( begin )
////////////////////////////////////////////////////////////////////////////////////////
var screenW = 0, screenH = 0;
var circles_f = [];var circles_i = [];
window.addEventListener('resize', onWindowResize, false);
var cef = document.getElementById('cf');
var cf = cef.getContext('2d');
var cei = document.getElementById('ci');
var ci = cei.getContext('2d');
function btnClick(id,callback=null){var e = document.getElementById(id);if(callback==null) e.click();e.onclick = function(){callback(e);};}
function setText(x,value){document.getElementById(x).innerHTML = value;}
function getMS() { return (new Date).getTime(); }
function onWindowResize()
{var shift_gap = 500;screenW = window.innerWidth;screenH = window.innerHeight;
cef.width = ( screenW - shift_gap ) / 2; cef.height = screenH;
cei.width = ( screenW - shift_gap ) / 2; cei.height = screenH; }
onWindowResize();
////////////////////////////////////////////////////////////////////////////////////////
// NOT IMPORTANT CODE ( end )
////////////////////////////////////////////////////////////////////////////////////////
///////////////////////
// Variables
var scoring_active = true;
var auto_addon = true;
var frame_rate = 60
var update_rate = 1000/frame_rate;
///////////////////////
// SetInterval
var setinterval_active = true;
var setinterval_id = setInterval(onTimer, update_rate);
var setinterval_exe_count = 0;
var setinterval_start = getMS();
var setinterval_ms_total = 0;
var setinterval_tps = NaN;
var setinterval_eps = NaN;
var setinterval_eps_last = NaN;
var setinterval_loss = NaN;
var setinterval_score = NaN;
function onTimer()
{
if( ! setinterval_active ) return;
setinterval_eps++;
setinterval_exe_count++;
var now = getMS();
var ms = now - setinterval_start;
setinterval_start = now;
setinterval_ms_total += ms;
if( setinterval_ms_total >= 1000 )
{
setinterval_tps = setinterval_ms_total / setinterval_eps;
setinterval_eps_last = setinterval_eps;
setinterval_loss = ( setinterval_eps_last / 60 ) * ( (update_rate * setinterval_eps) / setinterval_ms_total );
setinterval_score = ( setinterval_loss ) * ( circles_i.length );
if( auto_addon && setinterval_eps_last > circles_addon_min_fps )
{
var i = circles_addon;
while( i-- > 0 ) circles_i.push( generate() );
}
setinterval_ms_total = 0;
setinterval_eps = 0;
}
// --
draw(ci, cei.width, cei.height, circles_i);
// --
setText('i1', setinterval_exe_count);
setText('i2', setinterval_eps_last );
setText('i3', setinterval_tps.toFixed(2) );
setText('i4', circles_i.length );
setText('i5', ( 100 * (1-setinterval_loss) ).toFixed(2) + " %" );
setText('i6', scoring_active ? setinterval_score.toFixed(0) : "???" );
}
///////////////////////
// requestAnimationFrame
var frame_active = true;
var frame_exe_count = 0;
var frame_start = setinterval_start;
var frame_ms_total = 0;
var frame_tps = NaN;
var frame_native_ms_last = NaN;
var frame_native_ms_total = 0;
var frame_native_tps = NaN;
var frame_eps = 0;
var frame_eps_last = NaN;
var frame_loss = NaN;
var frame_score = NaN;
// --
function onFrame( native_now )
{
if( ! frame_active ) return;
if( isNaN( frame_native_ms_last ) ) frame_native_ms_last = native_now;
else {
var native_ms = native_now - frame_native_ms_last;
frame_native_ms_total += native_ms;
frame_native_ms_last = native_now;
}
frame_eps++;
frame_exe_count++;
var now = getMS();
var ms = now - frame_start;
var instantFPS = 1000 / ms;
frame_start = now;
frame_ms_total += ms;
//if( auto_addon && instantFPS > circles_addon_min_fps ) circles_f.push( generate() );
if( frame_ms_total >= 1000 )
{
frame_tps = frame_ms_total / frame_eps;
frame_native_tps = frame_native_ms_total / frame_eps;
frame_eps_last = frame_eps;
frame_loss = ( frame_eps_last / 60 ) * ( (update_rate * frame_eps) / frame_ms_total );
frame_score = ( frame_loss ) * ( circles_f.length );
if( auto_addon && frame_eps_last > circles_addon_min_fps )
{
var i = circles_addon;
while( i-- > 0 ) circles_f.push( generate() );
}
frame_ms_total = 0;
frame_native_ms_total = 0;
frame_eps = 0;
}
// --
draw(cf, cef.width, cef.height, circles_f);
// --
setText('f1', frame_exe_count);
setText('f2', frame_eps_last);
setText('f3', frame_tps.toFixed(2) );
setText('f4', circles_f.length );
setText('f5', ( 100 * (1-frame_loss) ).toFixed(2) + " %" );
setText('f6', scoring_active ? frame_score.toFixed(0) : "???" );
setText('f7', frame_native_tps.toFixed(2) );
// --
requestAnimationFrame( onFrame );
}
// --
requestAnimationFrame( onFrame );
///////////////////////
// circles
var circles_count = 128;
var circles_addon = 64;
var circles_addon_min_fps = 55;
for( var f = 0; f < circles_count; ++f ) circles_f[f] = generate();
for( var i = 0; i < circles_count; ++i ) circles_i[i] = generate();
function generate(){return [10,10,Math.random()*12+2,Math.random()*6+2,Math.random()*10+20,"rgba(" + parseInt(Math.random()*255) + "," + parseInt(Math.random()*255) + "," + parseInt(Math.random()*255) + "," + "0.8)"];}
function draw( ctx, w, h, circles = null )
{
ctx.clearRect(0,0,w,h);
if( circles == null ) return;
for( var i = 0; i < circles.length; ++i )
{
var k = circles[i];
k[0] += k[2]; k[1] += k[3];
if(k[0]+1.55*k[4]>w && k[2] > 0)k[2] = -k[2];
if(k[1]+1.05*k[4]>h && k[3] > 0)k[3] = -k[3];
if(k[0]-1.55*k[4]<0 && k[2] < 0)k[2] = -k[2];
if(k[1]-1.05*k[4]<0 && k[3] < 0)k[3] = -k[3];
ctx.fillStyle = k[5]; ctx.beginPath();
ctx.arc(k[0],k[1],k[4],0,6.283185,0,true);
ctx.fill();
}
}
///////////////////////
// buttons
function btnI_click(b){
//scoring_active = false;
b.innerHTML = ( setinterval_active ? "Start" : "Stop" ) + "<u> Interval</u>";
setinterval_active = !setinterval_active;
}
btnClick( 'btnI', btnI_click );
function btnF_click(b){
//scoring_active = false;
frame_active = !frame_active;
if( frame_active ) requestAnimationFrame( onFrame );
b.innerHTML = ( ! frame_active ? "Start" : "Stop" ) + "<u> Frame</u>";
}
btnClick( 'btnF', btnF_click );
btnClick( 'btnR', function(b) {
if(!frame_active) btnF_click(document.getElementById('btnF'));
if(!setinterval_active) btnI_click(document.getElementById('btnI'));
scoring_active = true;
setinterval_exe_count = 0;setinterval_start = getMS();setinterval_ms_total = 0;setinterval_tps = NaN;setinterval_eps = NaN;setinterval_eps_last = NaN;frame_exe_count = 0;frame_start = setinterval_start;frame_ms_total = 0;frame_tps = NaN;frame_eps = 0;frame_eps_last = NaN;circles_f = [];for( var f = 0; f < circles_count; ++f ) circles_f[f] = generate(); circles_i = []; for( var i = 0; i < circles_count; ++i ) circles_i[i] = generate();
} );
Also see: Tab Triggers